R Plot - Y轴和绘制值不相关

时间:2015-07-21 05:31:58

标签: r plot axis

我对R有点新,我试图绘制以下内容:

x <- c(1:13)
y <- c(19.77, 18.07, 20.55, 16.42, 18.47, 20.18, 22.91, 13.18, 18.07, 17.05, 13.45, 11.11, 22.52)
y2 <- c(26.93, 24.5, 25.73, 23.65, 20.50, 29.58, 18.8, 17.38, 12.2, 18.17, 12.55, 15.63, 26.07)
par(mar=c(5,7,5,3), pch=22, col="Black")
plot(x, y2, main="TITLE", xaxt = "n", xlab='', yaxt = "n", ylab = "", type="b", col="Blue")
par(new = TRUE)
plot(x, y, xaxt = "n", xlab='', yaxt = "n", ylab = "", type="b", col="cyan", pch=21)
range <- seq(0, 30, 1)
axis(1, at=x, labels=x)
axis(2, at=range, labels=range, las=1)
mtext(side = 1, text = "X", line = 3, col="Black")
mtext(side = 2, text = "Tiempo", line = 5, xpd=FALSE, col="Black")
# Legend.
legend(x= "bottomleft", y=0.92, legend=c("Exp1","Exp2"), col=c("cyan", "blue"), pch=c("_","_"))

如图所示:

My plot

但是,我觉得y轴与实际值不相关。例如,在数据(y和y2)中,我们可以看到其中一个最大值是26.93,但在数据中,根据图表,最高值约为23。

我一直在玩范围变量,但我仍然没有找到任何方法来纠正这个问题。谷歌一直没有帮助,因为我不确定我应该如何搜索这个问题。你们有没有人知道我做错了什么?

此外,我希望值的范围为0到30,而不是11到23.可能这与问题有关。

任何提示都很有价值!提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用matplot

matplot(x, cbind(y2,y), t='b', col = c("blue", "cyan"), pch = c(22, 21), lty = c(1,1), xaxt = "n", xlab="", yaxt = "n", ylab = "")
axis(1, at=x, labels=x)
axis(2, at=range, labels=range, las=1)
mtext(side = 1, text = "X", line = 3, col="Black")
mtext(side = 2, text = "Tiempo", line = 5, xpd=FALSE, col="Black")
legend(x= "bottomleft", y=0.92, legend=c("Exp1","Exp2"), col=c("cyan", "blue"), pch=c("_","_"))

enter image description here

答案 1 :(得分:1)

尽可能接近代码,你可以改变第5行和第7行:

plot(y2 ~ x,  main="TITLE", xaxt = "n", xlab='', yaxt = "n", ylab = "", type = "b", col = "Blue", ylim = c(0,30))
lines(y ~ x, xaxt = "n", xlab = '', yaxt = "n", ylab = "", type = "b", col = "cyan", pch = 21)

enter image description here