如何在ggplot中的堆叠条形图中将误差线组织到相关条? [英] How to organize error bars to relevant bars in a stacked bar plot in ggplot?

查看:450
本文介绍了如何在ggplot中的堆叠条形图中将误差线组织到相关条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将三种不同处理的条形图堆叠在一起以进行比较.但是,我无法将误差线放在正确的位置.

I stacked the bar plots from three different treatment together for comparison. However, I am having trouble putting the error bars in the right position.

我不确定我的代码是否正确,因为我感觉这应该很容易解决.任何帮助,将不胜感激.

I am not sure if my codes are correct since I have a feeling that this should be an easy fix. Any help would be appreciated.

    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很好地解释了可解释性.也许您想考虑将它们并排绘制?

You can stack it, but it's hard to read the error bars. @kath made a good point about interpretability. Maybe you want to consider plotting them side by side?

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)

这篇关于如何在ggplot中的堆叠条形图中将误差线组织到相关条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆