2013-05-19 51 views
11

我竭力把誤差線到正確的位置堆疊欄上。正如我在以前的帖子中看到的,我使用ddply來堆棧錯誤欄。然後,這改變了堆疊的順序,所以我訂購了這個因子。現在看起來誤差條在一組條上是正確的,但在另一條上卻是正確的。我想要的是一張如下圖所示的圖形,只是帶有誤差條顯示的標準誤差。我列出了原始數據和ddply數據的輸入以及數據集。 enter image description here誤差條GGPLOT2

Suz2$org <- factor(Suz2$org, levels = c('fungi','bacteria'),ordered = TRUE) 

library(plyr) 
plydat <- ddply(Suz2,.(org, group, time),transform,ybegin = copy - se,yend = copy + se) 

colvec <-c("blue", "orange") 

ggplot(plydat, aes(time, copy)) + 
    geom_bar(aes(fill = factor(org)), stat="identity", width = 0.7) + 
    scale_fill_manual(values = colvec) + 
    facet_wrap(~group,nrow = 1)+ 
    geom_errorbar(aes(ymax=ybegin , ymin= yend),width=.5) + 
    theme(panel.background = element_rect(fill='white', colour='white'), 
     panel.grid = element_line(color = NA), 
     panel.grid.minor = element_line(color = NA), 
     panel.border = element_rect(fill = NA, color = "black"), 
     axis.text.x = element_text(size=10, colour="black", face = "bold"), 
     axis.title.x = element_text(vjust=0.1, face = "bold"), 
     axis.text.y = element_text(size=12, colour="black"), 
     axis.title.y = element_text(vjust=0.2, size = 12, face = "bold")) 

dput(plydat)

structure(list(org = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("fungi", "bacteria" 
), class = c("ordered", "factor")), time = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0W", 
"6W"), class = "factor"), copy = c(97800000, 15500000, 40200000, 
10400000, 55100000, 14300000, 1.6e+07, 8640000, 2.98e+08, 77900000, 
2.33e+08, 2.2e+08, 3.37e+08, 88400000, 3.24e+08, 1.89e+08), group = structure(c(3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("Native D0", 
"Native D707", "Notill D0", "Notill D707"), class = "factor"), 
    se = c(11100000, 2810000, 7110000, 2910000, 1.7e+07, 1500000, 
    1930000, 2980000, 43900000, 20100000, 56400000, 41200000, 
    75700000, 22500000, 57500000, 28100000), ybegin = c(86700000, 
    12690000, 33090000, 7490000, 38100000, 12800000, 14070000, 
    5660000, 254100000, 57800000, 176600000, 178800000, 261300000, 
    65900000, 266500000, 160900000), yend = c(108900000, 18310000, 
    47310000, 13310000, 72100000, 15800000, 17930000, 11620000, 
    341900000, 9.8e+07, 289400000, 261200000, 412700000, 110900000, 
    381500000, 217100000)), .Names = c("org", "time", "copy", 
"group", "se", "ybegin", "yend"), row.names = c(NA, -16L), class = "data.frame") 

dput(Suz2)

structure(list(org = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("fungi", "bacteria" 
), class = c("ordered", "factor")), time = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0W", 
"6W"), class = "factor"), copy = c(97800000, 15500000, 40200000, 
10400000, 55100000, 14300000, 1.6e+07, 8640000, 2.98e+08, 77900000, 
2.33e+08, 2.2e+08, 3.37e+08, 88400000, 3.24e+08, 1.89e+08), group = structure(c(3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("Native D0", 
"Native D707", "Notill D0", "Notill D707"), class = "factor"), 
    se = c(11100000, 2810000, 7110000, 2910000, 1.7e+07, 1500000, 
    1930000, 2980000, 43900000, 20100000, 56400000, 41200000, 
    75700000, 22500000, 57500000, 28100000)), .Names = c("org", 
"time", "copy", "group", "se"), row.names = c(NA, -16L), class = "data.frame") 

Suz2

org time  copy  group  se 
1  fungi 0W 9.78e+07 Notill D0 11100000 
2  fungi 0W 1.55e+07 Notill D707 2810000 
3  fungi 0W 4.02e+07 Native D0 7110000 
4  fungi 0W 1.04e+07 Native D707 2910000 
5  fungi 6W 5.51e+07 Notill D0 17000000 
6  fungi 6W 1.43e+07 Notill D707 1500000 
7  fungi 6W 1.60e+07 Native D0 1930000 
8  fungi 6W 8.64e+06 Native D707 2980000 
9 bacteria 0W 2.98e+08 Notill D0 43900000 
10 bacteria 0W 7.79e+07 Notill D707 20100000 
11 bacteria 0W 2.33e+08 Native D0 56400000 
12 bacteria 0W 2.20e+08 Native D707 41200000 
13 bacteria 6W 3.37e+08 Notill D0 75700000 
14 bacteria 6W 8.84e+07 Notill D707 22500000 
15 bacteria 6W 3.24e+08 Native D0 57500000 
16 bacteria 6W 1.89e+08 Native D707 28100000 
+3

1進行詳細的問題,包括可再現的例子。 –

+3

對於數據以外的數據使用barplot基本上是錯誤的,並且對於您繪製的每個barplot(帶有錯誤欄),一隻可愛的小狗和/或小貓死亡,我因爲一個良好形成的問題和可重複的示例而大聲疾呼。 –

回答

10

的兩個ybegin和值中,errorbar的範圍內,是太低的bacteria數據。由於用於bacteria杆都在fungi條的頂部,所述fungi棒的高度(plydat$copy[plydat$org == "fungi"])必須被添加到bacteria數據的errorbar值。

plydat[plydat$org == "bacteria", ] 
    <- transform(plydat[plydat$org == "bacteria", ], 
       ybegin = ybegin + plydat[plydat$org == "fungi", "copy"], 
       yend = yend + plydat[plydat$org == "fungi", "copy"]) 

enter image description here

+1

+1雖然我不是堆疊式barcharts的忠實粉絲,但請參閱我的答案尋找替代方案。 –

9

就個人而言,我真的不喜歡堆積條形圖的,尤其是當堆疊條的數量較大(這是不適合你的情況下)。主要問題是,除了最低的堆棧之外的所有堆棧都沒有共享相同的基準。在你的情況下,很難比較橙色bacteria類,因爲它們不共享相同的鹼基(y值,copy)。

我建議使用一種稱爲點陣圖情節:

library(ggplot2) 
theme_set(theme_bw()) 
ggplot(plydat, aes(time, copy, color = org)) + 
    geom_point() + facet_wrap(~group, ncol = 1) + 
    geom_errorbar(aes(ymax=ybegin , ymin= yend), width = 0) + coord_flip() 

enter image description here

注意,copy值這裏沒有添加劑,因爲它是在堆疊條形圖。因爲它們共享相同的基本copy值(0),可以很容易的bacteria不同值之間進行比較。另外,我交換了x軸和y軸,以便於比較copy的值(只需刪除coord_flip以查看比較copy的效果如何)。

唯一真正的缺點是沒有簡單的方法來判斷fungibacteria的總和。根據圖表要顯示的內容(圖表的故事),這可能會或可能不會成爲問題。你可以添加一個單獨的附加類別org,即both這兩個類別的總和,來解決這個問題。當然,在這個總結的類別中解釋錯誤並不是微不足道的。

3

從以上答案的組合,我想我會去這樣的事情。

plydat <- ddply(Suz2,.(org),transform,ybegin = copy - se,yend = copy + se) 

colvec <-c("blue", "orange") 

ggplot(plydat, aes(time, copy, color = factor(org))) + 
    geom_point(size = 3.5) + facet_wrap(~group, ncol = 4) + 
    scale_color_manual(values = colvec) + 
    geom_errorbar(aes(ymax=ybegin , ymin= yend), width = 0.08, 
     color = "black", size = 0.1) + 
    theme(panel.background = element_rect(fill='white', colour='white'), 
     panel.grid = element_line(color = NA), 
     panel.grid.minor = element_line(color = NA), 
     panel.border = element_rect(fill = NA, color = "black"), 
     strip.background = element_blank(), 
     axis.text.x = element_text(size=10, colour="black", face = "bold"), 
     axis.title.x = element_text(vjust=0.1, face = "bold"), 
     axis.text.y = element_text(size=12, colour="black"), 
     axis.title.y = element_text(vjust=0.2, size = 12, face = "bold")) 

enter image description here