【发布时间】:2022-11-27 05:58:45
【问题描述】:
我知道不鼓励在单个图上绘制 2 个单独的 y 轴,这可能会造成混淆和误导。
有没有办法匹配 Y 轴的值?例如找到一种方法在图表中移动右 Y 轴数据(?) 例如,我希望左侧 Y 轴中的 40 与右侧 Y 轴中的 -1 匹配,因为这两个值对应于干旱条件类别的开始。
样本:
structure(list(Time = structure(c(9862, 9893, 9921, 9952, 9982,
10013, 10043, 10074, 10105, 10135, 10166, 10196, 10227, 10258,
10286, 10317, 10347, 10378, 10408, 10439), class = "Date"), Year = c(1997,
1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997,
1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998), VCI = c(48.7488482440362,
51.8662335250972, 54.4212125411374, 61.7338808190779, 63.9870065731148,
61.3375357670741, 62.6197335631611, 63.0950799644754, 61.6276947895731,
61.1298324406371, 64.4422427513358, 60.3823204404222, 60.5883337239537,
61.8918834440238, 59.1304135098709, 62.1668350554954, 61.9352586665065,
55.75795384214, 50.3371363875305, 52.5748728440737), TCI = c(53.7071192239754,
53.6178820221828, 57.7831310561669, 57.3996088686986, 49.8613200877384,
54.9673093834738, 42.4962626269047, 33.542249807155, 36.9526033996693,
46.0464770178552, 49.5240246297537, 49.6298842520857, 47.9889200846868,
40.3862301499032, 36.8605803231892, 38.8799158911488, 39.0120455451407,
45.9071510330717, 55.8730250709158, 60.4339176493461), SPEI = c(0.385767341805337,
-0.240467091114443, 0.218601001011986, 0.392296211626228, -0.0041472667529566,
0.36089672045203, -0.415596363086708, -0.694577131096395, -0.53422184521265,
0.372791671097943, 0.0714646484375678, 0.100567550879492, 0.484279813014397,
-0.478876226785371, -0.591222448288627, -0.473201395390211, -0.347352514594038,
-0.432571106796894, -0.259775061906046, 0.114961224539346)), row.names = c(NA,
20L), class = "data.frame")
这是代码:
## Plot first set of data and draw its axis
par(mar = c(5, 5, 4, 4))
#VCI index
plot(variables$Time, variables$VCI, pch=20, cex=.9, axes=FALSE, ylim=c(0,100), xlab="", ylab="",
type="l",col="Aquamarine3", main="Temporal trend - drought indices, growing season")
axis(2, ylim=c(0,100),col="black",las=1) ## las=1 makes horizontal labels
mtext("VCI and TCI",side=2,line=2.5)
box()
abline(h = 40, col = "black", lty = "dotted", lwd= 2)
## Allow a second plot on the same graph
par(new=TRUE)
#TCI index
plot(variables$Time, variables$TCI, pch=21, cex = 1.2, axes=FALSE, ylim=c(0,100), xlab="", ylab="",
type="l",col="Chocolate2", main="Temporal trend - drought indices, growing season")
axis(2, ylim=c(0,100),col="grey",las=1) ## las=1 makes horizontal labels
mtext("VCI and TCI",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(variables$Time, variables$SPEI, pch=15, cex=.4, xlab="", ylab="", ylim=c(-2.5,2.5),
axes=FALSE, type="l", col="darkorchid3")
abline(h = -1.5, col = "black", lty = "dashed", lwd= 2)
## a little farther out (line=4) to make room for labels
mtext("SPEI",side=4,line=2.5)
axis(4, ylim=c(-2.5, 2.5), col="black",col.axis="black",las=1)
par(new=TRUE)
## Draw the time axis
axis(1, spei.df$Time, format(spei.df$Time, "%Y"), 20)
mtext("Time",side=1,col="black",line=2.5)
## Add Legend
legend("topleft",legend=c("VCI","TCI", "SPEI"),
text.col=c("black","black", "black"), lty=1, lwd=2, col=c("Aquamarine3","Chocolate2", "darkorchid3"))
【问题讨论】:
-
您可以使用线性插值来计算第二轴的 y 限制。
-
我不明白这与我的需要有什么关系?
标签: r plot graph time-series