Dear Greg,
First convert your data.frame to a long format using melt(). Then use
ddply() to calculate the averages. Once you get at this point is should
be rather straightforward.
library (ggplot2)
v1 <- c(1,2,3,3,4)
v2 <- c(4,3,1,1,9)
v3 <- c(3,5,7,2,9)
gender <-
c("m","f","m","f","f")
d.data <- data.frame (v1, v2, v3, gender)
molten <- melt(d.data)
Average <- ddply(molten, c("gender", "variable"),
function(z){
c(Mean = mean(z$value))}
)
ggplot (data=Average, aes(x = variable, y = Mean, fill = gender)) +
geom_bar(position = position_dodge()) +
coord_flip() +
geom_text (position = position_dodge(0.5),
aes(label=round(Mean, 1)), vjust=0.5, hjust=4,colour="white", size=7)
ggplot (data=Average, aes(x = variable, y = Mean)) +
geom_bar() +
coord_flip() +
geom_text(aes(label=round(Mean, 1)), vjust=0.5,
hjust=4,colour="white", size=7) +
facet_wrap(~gender)
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] Namens Waller Gregor (wall)
> Verzonden: woensdag 1 september 2010 17:16
> Aan: r-help at r-project.org
> Onderwerp: [R] ggplot2 multiple group barchart
>
>
> hi there.. i got a problem with ggplot2.
>
> here my example:
>
> library (ggplot2)
>
> v1 <- c(1,2,3,3,4)
> v2 <- c(4,3,1,1,9)
> v3 <- c(3,5,7,2,9)
> gender <-
c("m","f","m","f","f")
>
> d.data <- data.frame (v1, v2, v3, gender) d.data
>
> x <- names (d.data[1:3])
> y <- mean (d.data[1:3])
>
>
> pl <- ggplot (data=d.data, aes (x=x,y=y)) pl <- pl +
> geom_bar() pl <- pl + coord_flip() pl <- pl + geom_text
> (aes(label=round(y,1)),vjust=0.5, hjust=4,colour="white", size=7)
pl
>
> this gives me a nice barchart to compare the means of my
> variables "v1","v2" and "v3".
> my question: how do i have to proceed if i want this barchart
> splittet by the variable "gender".
> so i get two small bars for "v1", one for female and one for
> male, two bars for "v2" etc.
> i need them all in one chart.
>
> fill=gender, position="dodge" do not work...
>
> any ideas?
>
> thanks a lot
>
> greg
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.