簡體   English   中英

如何在ggplot中將誤差條組織到堆疊條plot中的相關條?

[英]How to organize error bars to relevant bars in a stacked bar plot in ggplot?

我將三種不同處理的條形圖堆疊在一起進行比較。 但是,我無法將錯誤欄放在正確的 position 中。

我不確定我的代碼是否正確,因為我覺得這應該很容易解決。 任何幫助,將不勝感激。

陰謀

    dff <- structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", 
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", 
"factor")), site = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L), .Label = c("port", "bluff", "palm"), class = "factor"), 
    o2 = c(0.00176680974320632, 0.00148126891095111, 0.000339290425939857, 
    0.00286401267875333, 0.00351454109668776, 0.0017904651506876, 
    0.00124172906552544, 0.00296532826689073, 0.00147283622695563, 
    0.000615469748914681, 0.00288144920417381, 0.000610425917237045
    ), sd = c(8.01053634208091e-05, 0.000676914258685707, 0.00042433026354163, 
    0.000120510874443334, 0.000729790000379787, 0.000750152053448522, 
    0.000883147983451001, 0.000397030811318241, 0.00107944390293209, 
    0.000315781661923161, 0.00218786030744793, 0.000184295714801481
    ), n = c(2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
    se = c(5.66430456842669e-05, 0.000478650662598528, 0.000300046806812961, 
    6.9576985800136e-05, 0.000421344453171167, 0.000433100489991655, 
    0.00050988572597971, 0.000229225845791162, 0.000623217227932945, 
    0.000182316627516485, 0.00126316173745436, 0.000106403180551129
    ), trmt = c("pro", "pro", "pro", "pro", "pro", "pro", "pro", 
    "pro", "pro", "pro", "pro", "pro")), row.names = c(NA, -12L
), class = "data.frame")

library(ggplot2)
plot <- ggplot(dff, aes(x=month, y=o2, fill=site))+
  geom_col(color="black", width=0.6, position="stack") +
  geom_errorbar(data=subset(dff, dff$trmt=="pro"), aes(ymin=o2, ymax=o2+se), width=0.2, stat="identity") +
  scale_x_discrete("Month")+ #chronological order for plotting
  scale_y_continuous(breaks =seq(0, 0.01, by = 0.001), expand = c(0,0), limits=c(0, 0.01))+
  ylab(yaxis) +
  scale_fill_manual(name = "Site",
                    labels = c("Port", "Bluff", "Palm"),
                    values = c("#FC4E07","#00AFBB", "#C3D7A4"))

您可以堆疊它,但很難閱讀錯誤欄。 @kath 對可解釋性提出了一個很好的觀點。 也許您想考慮將它們並排繪制?

library(gridExtra)

# side by side
side_p <- ggplot(dff, aes(x=month, y=o2, fill=site))+ 
geom_col(color="black", width=0.6,position=position_dodge(width=0.6))+
geom_errorbar(aes(ymin=o2, ymax=o2+se), width=0.2,position=position_dodge(width=0.6))

# calculate the new y
dff2 = dff %>% arrange(desc(site)) %>% group_by(month) %>% mutate(newy=cumsum(o2))
stacked_p <- ggplot(dff2, aes(x=month, y=o2, fill=site))+ 
geom_col(color="black", width=0.6,position=position_stack(vjust=1))+
geom_errorbar(inherit.aes = FALSE,aes(x=month,ymin=newy, ymax=newy+se), width=0.2)
#together
grid.arrange(side_p,stacked_p)

在此處輸入圖像描述

暫無
暫無

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

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