1

ベース R を使用して折れ線グラフを作成しようとしていますが、x 変数がカテゴリ変数であるため、折れ線グラフは自動的に箱ひげ図になります。このデフォルトをオーバーライドして、カテゴリカル x 値を含む折れ線グラフを作成する方法はありますか?

Macintosh 10.11.6 用の RStudio バージョン 1.0.153 を使用しています。

####My data####

x <- as.factor(c("apples", "oranges", "bananas"))

sweet <- c(0.732, 0.8257, 0.862)
tart <- c(0.9415, 0.9435, 0.943)

###Plotting###
par(family="serif",par(mar=c(1,1,1,1)),
    xpd=TRUE, 
    cex.lab=1.2, lwd=2, pty="m", font.axis=2)

plot(x, sweet, lty=1, col="red", type="l")
plot(x, tart, xlab = "Fruits", ylab = "Tastiness", add=T, lty=3, col="blue")

legend(1.2, 1.0, c("sweet", "tart"), 
xpd=TRUE, lty =c(1,3),col=c("red","blue"), 
           bty="o")

これが私の質問に対する解決策です(そしていくつか):

####My data####
x <- as.factor(c("apples", "oranges", "bananas"))

sweet <- c(0.732, 0.8257, 0.862) #sweet A
tart <- c(0.9415, 0.9435, 0.943) #tart A
sweet2 <- c(0.638, 0.6956, 0.730) #sweet B 
tart2 <- c(0.87045, 0.8798, 0.877) #tart B

###Plotting###
par(family="serif", #par(mar = c(5, 5, 4, 2)),
    xpd = T, mar = par()$mar + c(0,0,0,7),
    xpd=TRUE, 
    cex.lab=1.2, lwd=2, pty="m", font.axis=2)

plot(seq_along(x), xlab = "Fruits", ylab = "Tastiness", sweet, ylim=c(0.600, 1), lty=1, col="red", type="b", xaxt="n")

par(new=TRUE)
plot(seq_along(x),tart, ylim=c(0.600, 1), xlab = "Fruits", ylab = "Tastiness", type="b", add=TRUE, col="blue", xaxt="n")

par(new=TRUE)
plot(seq_along(x),sweet2, ylim=c(0.600, 1), xlab = "Fruits", ylab = "Tastiness", lty=2, type="b", add=TRUE, col="red", xaxt="n")

par(new=TRUE)
plot(seq_along(x),tart2, ylim=c(0.600, 1), xlab = "Fruits", ylab = "Tastiness", lty=2, type="b", add=TRUE, col="blue", xaxt="n")

axis(1, at=seq_along(x), labels=c("apples", "oranges", "bananas"))


legend(3.2, 1.0, c("sweet A", "sweet B", "tart A", "tart B"), 
       xpd=TRUE, lty =c(1,2,1,2),col=c("red","red", "blue", "blue"), 
       bty="o")

この例では、ylim をデータに見られる最小および最大の y 値に設定する必要があります。そうしないと、意味がある場合、別々のプロットの y 軸の値がごちゃ混ぜになります。

4

0 に答える 0