簡體   English   中英

繪制兩個x范圍不同的y軸

[英]Plot with two y axes with different x range

如現在所寫,這產生了在兩個y軸上都包含兩個數據集的圖,但沒有反映x范圍的差異。 y2的x范圍是1988年至2014年,y1的范圍是1969年至2014年。如何解決此問題?

df1<-data.frame(Year=c(1969,1975,1983,1988,1994,2003,2011),y1=c(412,700,1625,4521,8180,10563,25712))
df2<-data.frame(Year=c(1988:2014), y2=c(55,141,82,75,364,771,312,191,120,273,330,291,343,321,313,339,264,312,295,429,217,548,535,654,863,1379,1025))
par(mar= c(5,4,4,4))
plot(y1 ~ Year, data = df1, xlim=c(1969,2020), pch = 16, cex = 1.2, xlab = "year", ylab = "")
axis(2, ylim = c(0,27000))
mtext("y1", side = 2, line = 2.5)
par(new = T)
plot(y2 ~ Year, data = df2, pch = 1, cex = 1.2, xlab = "", ylab = "", axes = F, xlim = c(1988, 2014))
mtext("y2", side = 4, line = 2.5)
axis(4, ylim = c(0, 1500))

df2的年份范圍應僅從1988年到2014年,但在整個范圍內其繪制方式看起來像它。

您可以在plotrix包中使用twoord.plot函數。

library(plotrix)
twoord.plot(df1$Year, df1$y1, df2$Year, df2$y2, xlab="Year", lylim = range(df1$y1) + c(-1000, 1000), rylim = range(df2$y2) + c(-100, 100), type = "o", ylab = "y1", rylab = "y2")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM