barcharts with multiple grouped bars

32 views
Skip to first unread message

Karin Lagesen

unread,
Mar 24, 2012, 5:12:29 PM3/24/12
to ggplot2
Hi!

I am trying to make a barchart, but I am having difficulties getting
there. I have used ggplot2 a little bit before (a while ago
though...), so I thought I'd turn for to you for some tips on how to
get where I need to be.

My data looks like this (this is an example)


Name Aha1 Aha2 Aha3 Aha4 Total Group
a1 0 3 3 4 11 01
a1 1 4 4 0 11 16
a2 0 0 4 0 5 01
a2 3 0 5 0 10 16

In this example, I have two of the same kind (a1 and a2) which are
made different by their presence in two different Groups.

What I would like to see is a set of barcharts, where I

1. get the two kinds separated (a1 in one barchart, a2 in the next).
2. in each barchart, I would get two bars per Aha entry (one for each
Group), where each bar is shown as a percentage of each Aha out of the
Total, and with the Aha name below its two bars. In addition, I would
like to leave Ahas with only zeros out, but that would just be a
bonus.

I have to admit I am a bit at a loss of how to proceed. I do think
that I need to reshape my data somehow before proceeding, but I have
not been able to figure out how.

I hope I managed to explain what I wanted,

TIA,

Karin

Ben Bond-Lamberty

unread,
Mar 24, 2012, 10:03:02 PM3/24/12
to Karin Lagesen, ggplot2
Karin,

> 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>")
}

Karin Lagesen

unread,
Mar 25, 2012, 11:22:19 AM3/25/12
to ggplot2

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

Ben Bond-Lamberty

unread,
Mar 25, 2012, 3:56:35 PM3/25/12
to ggplot2
> 1. ... My goal was to have two bars

> 2. is it possible to have the resulting two plots inside one plot

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

Reply all
Reply to author
Forward
0 new messages