Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ggplot2, how can I make a bar chart of proportions across factors (and add error bars)?

Tags:

r

ggplot2

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

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

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.

What I'd like to get though is the proportion of cars in each cyl category, that have automatic transmission (binary variable 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?

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

like image 243
radek Avatar asked Jul 23 '13 12:07

radek


People also ask

How do you add error bars to Ggplot?

We can draw error bars to a plot using the geom_errorbar() function of the ggplot2 package of the R Language.

How do you add error bars to a bar graph in R?

Error bars can be added to plots using the arrows() function and changing the arrow head. You can add vertical and horizontal error bars to any plot type. Simply provide the x and y coordinates, and whatever you are using for your error (e.g. standard deviation, standard error).


1 Answers

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).

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()
like image 64
Didzis Elferts Avatar answered Nov 15 '22 05:11

Didzis Elferts