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

R Plot - Y Axis and plotted values are not correlated

我对 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。

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

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

任何提示都是有价值的!提前致谢。

使用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("_","_"))

尽可能接近您的代码,您可以像这样更改第 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)