如何使用geom_errorbar将堆栈栏中的错误栏堆叠起来? [英] How to stack error bars in a stacked bar plot using geom_errorbar?

查看:1400
本文介绍了如何使用geom_errorbar将堆栈栏中的错误栏堆叠起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的ggplot语句中,我试图使用 position =stack position =identity

这是我的ggplot语句:

  ggplot(DF,aes(x = factor(year),y = proportion,fill = response))+ 
facet_grid(。〜sex)+
theme(legend.position =none)
geom_bar(position =stack,stat =identity)+
geom_errorbar(aes(ymin = ci_l,ymax = ci_u),
width = .2,#错误栏宽度
position =identity)+

这是我得到的结果,可能会注意到右侧的误差栏不符合栏的值。


这里是我在这个例子中使用的数据框:

  DF < -  data.frame(sex = c(men,women,men,women,men,women ),
比例= c(0.33,0.32,0.24,0.29,0.12,0.16),
ci_l = c(0.325,0.322,0.230,0.284,0.114,0.155),
ci_u = c(0.339,0.316,0.252,0.311,0.130,0.176),
year = c(2008,2008, 2013,2013,2013,2013),
response = c(是,是,是,整个旅程,是,整个旅程,是,部分旅程, 是的,部分旅程)


解决方案

这里发生的事情是, ggplot 不会堆叠错误栏(它们必须相加),因此您必须手动执行该操作(似乎哈德利认为这不是一个好主意,也不会添加这个功能)。



因此,亲自动手:

  DF $ ci_l [DF $回应==是的,旅程的一部分]<  -  with(DF,ci_l [response ==是,部分旅途] + 
ci_l [response ==是,整个旅程 ])

DF $ ci_u [DF $ response ==是,旅程的一部分]< - with(DF,ci_u [response ==是,部分旅程] +
ci_u [response ==Yes,entire the journey])

现在:

  ggplot(DF,aes(x =因子(年),y =比例))+ 
facet_grid(。〜性别)+
geom_bar(stat =identity,aes(fill = response))+
geom_errorbar(aes(ymin = ci_l,
ymax = ci_u),
width = .2,#错误栏的宽度
position =identity)


I want to stack the error bars in a stacked histogram using geom_errorbar / ggplot.

In my ggplot statement, I have tried to used both position="stack" and position="identity". None of them worked.

Here is my ggplot statement:

ggplot(DF, aes(x=factor(year), y=proportion, fill=response)) +
        facet_grid(. ~ sex) +
        theme(legend.position="none")
        geom_bar(position="stack", stat="identity") +
        geom_errorbar(aes(ymin=ci_l, ymax=ci_u),
                      width=.2,                    # Width of the error bars
                      position="identity") +

Here is the result I'm getting, and you may notice that the error bars on the right hand-side do not follow the bar values.

Here is the Data Frame I've used in this example:

DF <- data.frame(sex=c("men","women","men","women","men","women"),
                  proportion=c(0.33,0.32,0.24,0.29,0.12,0.16),
                  ci_l=c(0.325,0.322,0.230,0.284,0.114,0.155),
                  ci_u=c(0.339,0.316,0.252,0.311,0.130,0.176),
                  year=c(2008,2008,2013,2013,2013,2013),
                  response=c("Yes","Yes","Yes, entire the journey","Yes, entire the journey","Yes, part of the journey","Yes, part of the journey")
                  )

解决方案

What is happening here is that ggplot is not stacking the error bars (they would have to be summed) so you will have to do that by hand (and it seems that Hadley thinks that this is not a good idea and wil not add this functionality).

So doing by hand:

DF$ci_l[DF$response == "Yes, part of the journey"] <- with(DF,ci_l[response == "Yes, part of the journey"] +
         ci_l[response == "Yes, entire the journey"])

DF$ci_u[DF$response == "Yes, part of the journey"] <- with(DF,ci_u[response == "Yes, part of the journey"] +
                                                             ci_u[response == "Yes, entire the journey"])

Now:

ggplot(DF, aes(x=factor(year), y=proportion)) +
  facet_grid(. ~ sex) +
  geom_bar(stat="identity",aes(fill=response)) +
  geom_errorbar(aes(ymin= ci_l, 
                    ymax= ci_u),
                width=.2,                    # Width of the error bars
                position="identity")

这篇关于如何使用geom_errorbar将堆栈栏中的错误栏堆叠起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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