我正在尝试使用基数 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 轴值会混杂在一起。