Greg,

Try this:

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

#library(reshape)
#library(plyr)
# These are already loaded by ggplot2, but for your reference: reshape
provides melt(), plyr provides ddply().

d.data <- melt(d.data, id.var="gender")
new.data <- ddply(d.data, .(gender, variable), summarize, means =
mean(value))
plot <- ggplot(data=new.data, aes(variable, y=means)) +
 geom_bar(aes(fill=gender), stat="identity", position="dodge") +
 coord_flip()
plot

I took the numbers out because it's not easy to make them fit the dodged
bars.

Jonathan


On Wed, Sep 1, 2010 at 9:15 AM, Waller Gregor (wall) <w...@zhaw.ch> wrote:

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

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to