在ggplot2中,如何制作跨因子比例的条形图(并添加误差条)?

时间:2022-07-22 14:55:44

I'm struggling with making a graph of proportion of a variable across a factor in ggplot.

我正在努力制作一个变量在ggplot中的一个因子的比例图。

Taking mtcars data as an example and stealing part of a solution from this question I can come up with

以mtcars数据为例,从这个问题中窃取部分解决方案,我可以想出来

ggplot(mtcars, aes(x = as.factor(cyl))) +  
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent_format())

This graph gives me proportion of each cyl category in the whole dataset.

该图给出了整个数据集中每个cyl类别的比例。

What I'd like to get though is the proportion of cars in each cyl category, that have automatic transmission (binary variable am).

我想得到的是每个汽缸类别中具有自动变速器(二进制变量am)的汽车比例。

On top of each bar I would like to add an error bar for the proportion.

在每个栏的顶部,我想为比例添加一个错误栏。

Is it possible to do it with ggplot only? Or do I have to first prepare a data frame with summaries and use it with identity option of bar graphs?

是否可以只使用ggplot?或者我是否必须首先使用摘要准备数据框并将其与条形图的标识选项一起使用?

I found some examples on Cookbook for R web page, but they deal with continuous y variable.

我在Cookbook for R网页上找到了一些例子,但它们处理的是连续的y变量。

1 个解决方案

#1


6  

I think that it would be easier to make new data frame and then use it for plotting. Here I calculated proportions and lower/upper confidence interval values (took them from prop.test() result).

我认为制作新数据框然后用它进行绘图会更容易。在这里,我计算了比例和下限/上限置信区间值(从prop.test()结果中获取)。

library(plyr)
mt.new<-ddply(mtcars,.(cyl),summarise,
      prop=sum(am)/length(am),
      low=prop.test(sum(am),length(am))$conf.int[1],
      upper=prop.test(sum(am),length(am))$conf.int[2])

ggplot(mt.new,aes(as.factor(cyl),y=prop,ymin=low,ymax=upper))+
  geom_bar(stat="identity")+
  geom_errorbar()

#1


6  

I think that it would be easier to make new data frame and then use it for plotting. Here I calculated proportions and lower/upper confidence interval values (took them from prop.test() result).

我认为制作新数据框然后用它进行绘图会更容易。在这里,我计算了比例和下限/上限置信区间值(从prop.test()结果中获取)。

library(plyr)
mt.new<-ddply(mtcars,.(cyl),summarise,
      prop=sum(am)/length(am),
      low=prop.test(sum(am),length(am))$conf.int[1],
      upper=prop.test(sum(am),length(am))$conf.int[2])

ggplot(mt.new,aes(as.factor(cyl),y=prop,ymin=low,ymax=upper))+
  geom_bar(stat="identity")+
  geom_errorbar()