geom_bar + stat_summary + facet + show values

1,357 views
Skip to first unread message

Torsten Stollen

unread,
Jul 12, 2009, 4:35:24 PM7/12/09
to ggplot2
Hello group-members!

I want to compare 10 means of different groups in one output. I
realised it with a combination of stat_summary() and facet_wrap
without big problems:

p <- qplot("Means", value, data=dataset, geom="bar", stat="summary",
fun.y = "mean") + facet_wrap(~ variable, ncol = 5)

But now I wanted to add the values of the 10 different means on top of
the bars.

q <- qplot("Means", value, data=dataset, geom=c("bar", "text"),
stat="summary", fun.y = "mean", label="?") + facet_wrap(~ variable,
ncol = 5)

On top of every bar you see the ? but I to put the values of the means
there. Has anyone a hint for me, how to realise this?

I tried the following ways without success:

r1 <- qplot("Means", value, data=dataset, geom=c("bar", "text"),
stat="summary", fun.y = "mean", label=c
("5","65","39","4","1","0","1","36","42","3")) + facet_wrap(~
variable, ncol = 5)
result: error = "geom_text requires the following missing aesthetics:
label")

r2 <- qplot("Means", value, data=dataset, geom=c("bar", "text"),
stat="summary", fun.y = "mean", label=mean(value)) + facet_wrap(~
variable, ncol = 5)
result: the overall mean of value is set on every bar but not the one
of the 10 groups

How do I set the label that it works?


Torsten Stollen

---
University of Koblenz-Landau
Institute for Social Sciences
Department of Political Science
http://ipw.uni-landau.de

s...@z107.de

unread,
Jul 12, 2009, 5:05:17 PM7/12/09
to ggplot2
Hey Torsten,

this works, but I guess it's not the most elegant solution:

dataset = data.frame(value=rnorm(300), variable=c("one","two","three"))

qplot("Means", value, data=dataset, geom=c("bar", "text"),

stat="summary", fun.y = "mean", label=rep(tapply(dataset$value,
dataset$variable, mean),100)) + facet_wrap(~variable)

I think this writes each mean value a 100 times in every boxplot.

regards,
Stefan

baptiste auguie

unread,
Jul 12, 2009, 5:39:58 PM7/12/09
to ggplot2
I guess you could do it this way: create a new data.frame with only
the mean values and add a new layer to the plot,

library(ggplot2)
dataset = data.frame(value=100*rnorm(30), variable=c("one","two","three"))

datameans = ddply(dataset,.(variable), summarize, mean=round(mean(value)))

p <- qplot("Means", value, data=dataset, geom="bar", stat="summary",
fun.y = "mean") + facet_wrap(~ variable,scales="free")

p + geom_text(data=datameans, aes(y=mean, label=mean))

but one summary calculation seems redundant.

HTH,

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

stollen69

unread,
Jul 12, 2009, 6:02:19 PM7/12/09
to ggplot2
Thank you so much for your help!
The first solution doesn't work with my own dataset but the second one
works fine.

Torsten
Reply all
Reply to author
Forward
0 new messages