> that I need to reshape my data somehow before proceeding
Yes, you were on the right track. Try the code below, which takes the
data you provided, uses 'melt' to reshape it into a useful form, and
then makes two plots following your guidelines.
Ben
library(ggplot2)
library(reshape)
data <- structure(list(Name = structure(c(1L, 1L, 2L, 2L), .Label = c("a1",
"a2"), class = "factor"), Aha1 = c(0L, 1L, 0L, 3L), Aha2 = c(3L,
4L, 0L, 0L), Aha3 = c(3L, 4L, 4L, 5L), Aha4 = c(4L, 0L, 0L, 0L
), Total = c(11L, 11L, 5L, 10L), Group = structure(c(1L, 2L,
1L, 2L), .Label = c("01", "16"), class = "factor")), .Names = c("Name",
"Aha1", "Aha2", "Aha3", "Aha4", "Total", "Group"), row.names = c(NA,
-4L), class = "data.frame")
mdata <- melt(data,id.vars=c("Name","Group","Total"))
for( i in levels(mdata$Name)) {
d <- droplevels(subset(mdata,Name==i & value>0))
p <- ggplot(d,aes(variable,value/Total*100))
print(p+geom_bar(stat="identity",position="dodge")+opts(title=i))
readline("<Return>")
}
Wow, this is amazing, thankyou very much for your response.
Just two small modifications that I am uncertain of how to accomplish:
1. There seems to be some sort of mixup with my bars.
From my example:
Name Aha1 Aha2 Aha3 Aha4 Total Group
a1 0 3 3 4 11 01
a1 1 4 4 0 11 16
For these two I am actually getting only one bar per Aha, which seems
to correspond to the maximum of the two. My goal was to have two bars
for each Aha, next to eachother, so that I could see the difference
between them.
2. is it possible to have the resulting two plots inside one plot,
next to eachother?
Thankyou very much!
Karin
After melting the data frame, things become even simpler:
p <- ggplot(mdata,aes(variable,value/Total*100))
p+geom_bar(stat="identity",position="dodge")+facet_grid(~Name)
Ben