2015-10-15 128 views
0

我最近問了一個關於如何用相同的data.frame生成2個y軸的多個圖的問題。該解決方案完美地工作(available here),但我似乎無法填寫第二個Y軸上的曲線下面的區域。 「多邊形」似乎是常用的解決方案,但是當我嘗試將它嵌套在我的for循環中時,我無法弄清楚如何適當選擇我的x和y座標。任何建議都會有用。R曲線下面的陰影區域

重複的樣品:

df6 <- structure(list(Year = c("2009", "2009", "2009", "2009", "2009", 
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009", 
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2012", "2012", 
"2012"), plot = c("FA6", "FA7", "FK1", "FK2", "FK3", "FO1", "FO2", 
"FO6", "GA2", "GA4", "GR1", "GR2", "HE2", "HE3", "LY1", "LY3", 
"LY8", "NM2", "NM3", "TH3", "TH5", "BR1", "BR8", "FA5", "FA6", 
"FA7", "FK1", "FK2", "FK3", "FO1", "FO2", "FO6", "GA2", "GA4", 
"GR1", "GR2", "HE2", "HE3", "LY1", "LY3", "LY8", "NM2", "NM3", 
"TH3", "TH5", "FA5", "FA6", "FA7", "FK1", "FK2", "FK3", "FO1", 
"FO2", "FO6", "GA2", "GA4", "GR1", "GR2", "HE2", "HE3", "LY1", 
"LY3", "LY8", "NM2", "NM3", "TH3", "TH5", "HE2", "HE3", "TH5" 
), AvgRW = c(0.628666666666667, 0.485027777777778, 0.479269230769231, 
0.826875, 0.633269230769231, 1.01830769230769, 1.34580555555556, 
1.13061764705882, 0.422375, 1.377625, 0.535375, 0.366384615384615, 
0.493119047619048, 0.300777777777778, 0.971923076923077, 1.02302941176471, 
1.47245833333333, 1.00654166666667, 0.56425, 1.66342857142857, 
1.28477586206897, 0.860666666666667, 2.10155130769231, 1.74626923076923, 
0.616148148148148, 0.42775, 0.402576923076923, 0.859333333333333, 
0.608961538461538, 1.28303846153846, 1.84344444444444, 1.52214705882353, 
0.425546875, 1.66179166666667, 0.647208333333333, 0.390461538461538, 
0.565892857142857, 0.237388888888889, 1.60419230769231, 1.16611764705882, 
1.95329166666667, 1.18795833333333, 0.655928571428571, 2.009, 
1.36198275862069, 2.61165384615385, 0.873296296296296, 0.596, 
0.485884615384615, 1.13633333333333, 0.684461538461538, 1.30946153846154, 
1.69747222222222, 1.64197058823529, 0.40740625, 1.40716666666667, 
0.641625, 0.428576923076923, 0.729011904761905, 0.376222222222222, 
1.52984615384615, 1.15317647058824, 1.66183333333333, 1.17904166666667, 
0.604857142857143, 1.57425, 1.55772222222222, 0.7315, 0.119, 
1.125875), SampDepth = c(27L, 18L, 13L, 12L, 13L, 13L, 18L, 17L, 
32L, 12L, 12L, 13L, 14L, 18L, 13L, 17L, 12L, 12L, 14L, 14L, 29L, 
21L, 13L, 13L, 27L, 18L, 13L, 12L, 13L, 13L, 18L, 17L, 32L, 12L, 
12L, 13L, 14L, 18L, 13L, 17L, 12L, 12L, 14L, 14L, 29L, 13L, 27L, 
18L, 13L, 12L, 13L, 13L, 18L, 17L, 32L, 12L, 12L, 13L, 14L, 18L, 
13L, 17L, 12L, 12L, 14L, 4L, 9L, 2L, 1L, 8L)), .Names = c("Year", 
"plot", "AvgRW", "SampDepth"), row.names = 2330:2399, class = "data.frame") 

當前代碼(工作完全沒有 「XX」, 「YY」,和 「多邊形」 線路):

for (i in df6$plot) {      #start loop 
    png(filename = paste0("C:\\Users\\User\\Desktop\\", i, type="png")) #saves graphs 
    par(mar=c(5.1,4.1,4.1,5.1))    #increase right margin 
    xx <- c(df6[df6$plot==i, min(df6$Year)], x, df6[df6$plot==i, max(df6$Year)]) 
    yy <- c(df6[df6$plot==i, min(df6$SampDepth)], y, df6[df6$plot==i, max(df6$SampDepth)]) 
    polygon(xx, yy, col="gray") 
    plot(df6[df6$plot==i,c(1,3)],xaxt="n", type="l", ylab="Avg RW") 
    title(main=i,line=2.5)     #add title 
    axis(1,at=as.integer(df6$Year),labels=df6$Year) #bottom axis 
    axis(3,at=as.integer(df6$Year),labels=df6$Year) #top axis 
    par(new = TRUE)         #overlay for secondary y 
    plot(df6[df6$plot==i,c(1,4)],xaxt="n",yaxt="n", ylab="",type="l", col="red") 
    axis(4)           #add secondary y axis 
    mtext("Sample Depth", side = 4, line=2)   #add secondary y label 
    dev.off() 
} 

使用此代碼時,我一直得到一個錯誤消息,我的x和y長度不同。

+1

請參閱如何創建[再現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。包含示例數據,以便我們可以運行代碼以查看發生了什麼。聽起來像是數據問題。 – MrFlick

+0

對不起,我試圖避免把太多的數據放入一個問題,所以我把鏈接放到了我早期問題的可重現數據上。我將編輯並添加它。 – KKL234

回答

1

您的問題源於您嘗試繪製多邊形的方式。繪製一個多邊形實際上比你想要做的要簡單得多。此外,我將數據集中在一起工作,因爲它更容易發現錯誤(對我而言)。如果你想回到你的內聯子集,那將會很好,因爲這只是一個功能性的例子,向你展示如何使用polygon()

對於曲線下方區域的顏色,我建議查看rgb()它允許您指定一個alpha值,以便不會清除剩餘的圖表。

乾杯!

for (i in df6$plot) {      #start loop 
     ## Cut out the data you are working with 
     plotting <- df6[which(df6$plot == i),] 

    ## Begin plotting 
    png(filename = paste0("C:\\Users\\User\\Desktop\\", i, type="png")) #saves graphs 
    par(mar=c(5.1,4.1,4.1,5.1))    #increase right margin 
    plot(plotting[,c(1,3)],xaxt="n", type="l", ylab="Avg RW") 

    ## This is what I added 
    # This will tell R how to plot the polygon, it need the x values (years) ascending and descending 
    # And it needs y values (AvgRW) for the above 0 values as well as the 0 values. 
    xx <- c(unique(plotting$Year),rev(unique(plotting$Year))) 
    yy <- c(plotting$AvgRW,rep(0,length(unique(plotting$Year)))) 

    polygon(xx, yy, col="gray") 
    title(main=i,line=2.5)     #add title 
    axis(1,at=as.integer(plotting$Year),labels=plotting$Year) #bottom axis 
    axis(3,at=as.integer(plotting$Year),labels=plotting$Year) #top axis 
    par(new = TRUE)         #overlay for secondary y 
    plot(plotting[,c(1,4)],xaxt="n",yaxt="n", ylab="",type="l", col="red") 
    axis(4)           #add secondary y axis 
    mtext("Sample Depth", side = 4, line=2)   #add secondary y label 
    dev.off() 
} 
+0

謝謝,此解決方案運行良好!我唯一的問題是多邊形繪圖:它實際上是我試圖爲(樣本深度)創建多邊形的Y2軸。我在代碼中換出了AvgRW for SampDepth,該代碼正常工作。但是,該多邊形是針對Y1軸範圍繪製的;關於如何讓它對Y2進行陰謀的任何想法? – KKL234

+1

想通了!只需移動多邊形命令的位置即可。如果放置在第二個繪圖線「plot(plotting [,c(1,4)],....」下,它就完美了! – KKL234