bar chart with means - using ggplot

2,037 views
Skip to first unread message

Andreas Christoffersen

unread,
Sep 11, 2009, 4:50:36 PM9/11/09
to ggplot2
In the help pages hadley provides the following example showing how to
achieve bar charts with y being means of a variable instead of counts.
The example uses qplot however - and not ggplot. Since i would like to
understand ggplot better I would really like to see how this could be
done in ggplot.

# example using qplot
library(ggplot2)
meanprice <- tapply(diamonds$price, diamonds$cut, mean)
cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
qplot(cut, meanprice, geom="bar", stat="identity", fill = I("grey50"))

Charlotte Wickham

unread,
Sep 11, 2009, 5:24:45 PM9/11/09
to Andreas Christoffersen, ggplot2
If you just want to plot the summary data using ggplot then you would do:

ggplot(data.frame(cut, meanprice), aes(cut, meanprice)) +
geom_bar(stat = "identity", fill = "grey50")

If you want to skip the summarising step then one way would be:

ggplot(diamonds, aes(cut, price)) +
stat_summary(fun.y = "mean", fill = "grey50", geom = "bar")

But notice the y-axis extends to the range of price. We can't use
ylim here since it will exclude some of the data which we are
summarising over. So instead use coord_cartesian:

ggplot(diamonds, aes(cut, price)) +
stat_summary(fun.y = "mean", fill = "grey50", geom = "bar") +
coord_cartesian(ylim = c(0,5000))

And maybe add in some more breaks in the axis and change its label:

ggplot(diamonds, aes(cut, price)) +
stat_summary(fun.y = "mean", fill = "grey50", geom = "bar") +
coord_cartesian(ylim = c(0,5000)) +
scale_y_continuous("mean_price", breaks = 1000 * 0:5)

Charlotte

smu

unread,
Sep 11, 2009, 5:27:00 PM9/11/09
to ggplot2
hey,

d <- stack(meanprice)
ggplot(d) + geom_bar(aes(x=ind,y=values),stat="identity",fill=I("grey50"))

regards,
stefan

Felipe Carrillo

unread,
Sep 11, 2009, 5:32:52 PM9/11/09
to ggplot2, Andreas Christoffersen, r-h...@stat.math.ethz.ch
Like this?

# example using qplot
library(ggplot2)
meanprice <- tapply(diamonds$price, diamonds$cut, mean);meanprice
cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
qplot(cut, meanprice, geom="bar", stat="identity", fill = I("grey50"))
dev.new() # create a new graph to compare with qplot
# Example using ggplot
ggdata <- data.frame(meanprice,cut);ggdata
ggplot(ggdata,aes(y=meanprice,x=cut)) + geom_bar(fill="grey50",stat='identity')

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA


--- On Fri, 9/11/09, Andreas Christoffersen <achrist...@gmail.com> wrote:

Dianne Cook

unread,
Sep 11, 2009, 6:00:28 PM9/11/09
to Felipe Carrillo, ggplot2, Andreas Christoffersen, r-h...@stat.math.ethz.ch
I would recommend not representing means by bars at all.

Bars are for counts, stacking from zero. Means are point estimates.
ggplot has a lot of routines for displaying means with errors bars:
geom_linerange, geom_pointrange. To hone your ggplot skills I
recommend looking into these geoms.
---------------------------
Dianne Cook
dic...@iastate.edu



Reply all
Reply to author
Forward
0 new messages