2013-09-10 38 views
1

這個問題是一個直接的後繼問題,在這裏被稱爲「帶有X和Y誤差條的疊加平均值的兩組ggplot散點圖」。那個問題的答案看起來正是我要完成的事情,但是提供的代碼導致了一個我無法解決的錯誤。我將在這裏使用我的數據作爲示例,但我也嘗試了原始問題代碼以及相同的結果。 我有一個數據幀,看起來像這樣:ggplot2散點圖與平均值和雙向SD條重疊

structure(list(Meta_ID = structure(c(15L, 22L, 31L, 17L), .Label = c("NM*624-46", 
"NM*624-54", "NM*624-56", "NM*624-61", "NM*624-70", "NM624-36", 
"NM624-38", "NM624-39", "NM624-40", "NM624-41", "NM624-43", "NM624-46", 
"NM624-47", "NM624-51", "NM624-54 ", "NM624-56", "NM624-57", 
"NM624-59", "NM624-61", "NM624-64", "NM624-70", "NM624-73", "NM624-75", 
"NM624-77", "NM624-81", "NM624-82", "NM624-83", "NM624-84", "NM625-02", 
"NM625-10", "NM625-11", "SM621-43", "SM621-44", "SM621-46", "SM621-47", 
"SM621-48", "SM621-52", "SM621-53", "SM621-55", "SM621-56", "SM621-96", 
"SM621-97", "SM622-51", "SM622-52", "SM623-14", "SM623-23", "SM623-26", 
"SM623-27", "SM623-32", "SM623-33", "SM623-34", "SM623-55", "SM623-56", 
"SM623-57", "SM623-58", "SM623-59", "SM623-61", "SM623-62", "SM623-64", 
"SM623-65", "SM623-66", "SM623-67", "SM680-74", "SM681-16"), class = "factor"), 
Region = structure(c(1L, 1L, 1L, 1L), .Label = c("N", "S" 
), class = "factor"), Tissue = structure(c(1L, 2L, 1L, 1L 
), .Label = c("M", "M*"), class = "factor"), Tag_Num = structure(c(41L, 
48L, 57L, 43L), .Label = c("621-43", "621-44", "621-46", 
"621-47", "621-48", "621-52", "621-53", "621-55", "621-56", 
"621-96", "621-97", "622-51", "622-52", "623-14", "623-23", 
"623-26", "623-27", "623-32", "623-33", "623-34", "623-55", 
"623-56", "623-57", "623-58", "623-59", "623-61", "623-62", 
"623-64", "623-65", "623-66", "623-67", "624-36", "624-38", 
"624-39", "624-40", "624-41", "624-43", "624-46", "624-47", 
"624-51", "624-54", "624-56", "624-57", "624-59", "624-61", 
"624-64", "624-70", "624-73", "624-75", "624-77", "624-81", 
"624-82", "624-83", "624-84", "625-02", "625-10", "625-11", 
"680-74", "681-16"), class = "factor"), Lab_Num = structure(1:4, .Label = c("C4683", 
"C4684", "C4685", "C4686", "C4687", "C4688", "C4689", "C4690", 
"C4691", "C4692", "C4693", "C4694", "C4695", "C4696", "C4697", 
"C4698", "C4699", "C4700", "C4701", "C4702", "C4703", "C4704", 
"C4705", "C4706", "C4707", "C4708", "C4709", "C4710", "C4711", 
"C4712", "C4713", "C4714", "C4715", "C4716", "C4717", "C4718", 
"C4719", "C4720", "C4721", "C4722", "C4723", "C4724", "C4725", 
"C4726", "C4727", "C4728", "C4729", "C4730", "C4731", "C4732", 
"C4733", "C4734", "C4735", "C4736", "C4737", "C4738", "C4739", 
"C4740", "C4741", "C4742", "C4743", "C4744", "C4745", "C4746", 
"C4747", "C4748"), class = "factor"), C = c(46.5, 46.7, 45, 
43.6), N = c(12.9, 13.7, 14.5, 13.4), C.N = c(3.6, 3.4, 3.1, 
3.3), d13C = c(-19.7, -19.5, -19.4, -19.2), d15N = c(13.3, 
12.4, 11.7, 11.9)), .Names = c("Meta_ID", "Region", "Tissue", 
"Tag_Num", "Lab_Num", "C", "N", "C.N", "d13C", "d15N"), row.names = c(NA, 
4L), class = "data.frame") 

我想生產與數據的覆蓋原始數據的散點圖意味着每個「區」與雙向誤差條。爲了做到這一點,我使用plyr來總結我的數據並生成手段和標準。然後我用GGPLOT2:

library(plyr) 
Basic <- ddply(First.run,.(Region),summarise, 
     N = length(d13C), 
     d13C.mean = mean(d13C), 
     d15N.mean = mean(d15N), 
     d13C.SD = sd(d13C), 
     d15N.SD = sd(d15N)) 

ggplot(data=First.run, aes(x = First.run$d13C, y = First.run$d15N))+ 
geom_point(aes(colour = Region))+ 
geom_point(data = Basic,aes(colour = Region))+ 
geom_errorbarh(data = Basic, aes(xmin = d13C.mean + d13C.SD, xmax = d13C.mean -   d13C.SD, 
           y = d15N.mean, colour = Region, height = 0.01))+ 
    geom_errorbar(data = Basic, aes(ymin = d15N.mean - d15N.SD, ymax = d15N.mean + d15N.SD, 
          x = d13C.mean,colour = Region)) 

但每次我運行此代碼時,我得到了同樣的錯誤,並不能找出問題所在。 錯誤:美學必須是長度爲1或與dataProblems的長度相同:區域

任何幫助將不勝感激。

編輯:由於我的示例數據是從我的完整數據集的頭部獲取的,因此它只包含來自「N」區域的樣本。只有這一個區域的代碼可以正常工作,但如果使用fix()更改提供的數據集,以便至少包含一個其他區域(在我的數據中另一個區域是「S」),則會顯示出現的錯誤。我的錯誤是不包括每個地區的一些數據。

+1

這對我來說沒有錯誤,對於您提供的小數據集運行。您的R/ggplot2是最新的? – aosmith

+0

謝謝你指出這一點。我已經添加了一個編輯來解決這個問題。請使用fix()手動將其中一個「N」區域更改爲「S」,以便我的數據中的兩個區域都被表示出來。然後發生錯誤。對不起,疏忽了。 – 2ton21

回答

1

我最終改變了兩個「N」區域爲「S」,所以我可以計算兩組的標準偏差。

我認爲問題在於您在某些幾何圖形中缺少所需的美觀(例如,geom_point缺少x和y)。至少將所有必要的美學融入每一個幾何圖案看起來都能讓所有東西都起作用。我在清理其他一些東西的時候會將代碼縮短一點。

ggplot(data = First.run, aes(x = d13C, y = d15N, colour = Region)) + 
    geom_point() + 
    geom_point(data = Basic,aes(x = d13C.mean, y = d15N.mean)) + 
    geom_errorbarh(data = Basic, aes(xmin = d13C.mean + d13C.SD, 
     xmax = d13C.mean - d13C.SD, y = d15N.mean, x = d13C.mean), height = .5) + 
    geom_errorbar(data = Basic, aes(ymin = d15N.mean - d15N.SD, 
     ymax = d15N.mean + d15N.SD, x = d13C.mean, y = d15N.mean), width = .01)