Re: Labels on stacked bar plot with facet_wrap

593 views
Skip to first unread message

Brandon Hurr

unread,
Oct 1, 2012, 11:28:10 AM10/1/12
to Maciej Beręsewicz, ggp...@googlegroups.com
This is certainly doable with geom_text(), but you'll have to compute the locations of each text piece in advance (non-trivial). 



On Mon, Oct 1, 2012 at 6:52 AM, Maciej Beręsewicz <maciej.b...@gmail.com> wrote:
Hi,
I would like to add labels on bar plots while using facet_wrap, but I don't know how to do that. 

Data for the example can be download from here 

With this code:

ggplot(example,aes(GroupingVar,fill=VarOfInterest)) + geom_bar(position='fill') + scale_fill_manual(values = (brewer.pal(5, "Greens"))) + 
  facet_wrap(~FacetVar,ncol=1) + coord_flip()+ scale_y_continuous(labels=percent) + ylab('Prercentage')

I get:



But I want to have something like this (of course perecentages should be on every bar plot)



I would be grateful for any help!


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Oct 1, 2012, 12:25:38 PM10/1/12
to Maciej Beręsewicz, ggp...@googlegroups.com
This gets pretty close, but it ain't pretty due to the smaller values. You could shrink the size of the font in geom_text, but that will only get you so far before it's not readable. 

require(ggplot2)
require(scales)
require(RColorBrewer)
require(plyr)
require(reshape2)


m.ex<-melt(ddply(example, .(GroupingVar, FacetVar), function(x) table(x$VarOfInterest)))

m.ex2<-ddply(m.ex, .(GroupingVar,FacetVar), transform, pct=value/sum(value))

m.ex2<-ddply(m.ex2, .(GroupingVar,FacetVar), transform, half=pct/2)

m.ex2<-ddply(m.ex2, .(GroupingVar,FacetVar), transform, cum=cumsum(pct))

m.ex2<-ddply(m.ex2, .(GroupingVar,FacetVar), transform, position=(cum-half))

ggplot() + geom_bar(data=example,aes(x=GroupingVar, fill=VarOfInterest), position='fill') +
scale_fill_manual(values = (brewer.pal(5, "Greens"))) +
geom_text(data=m.ex2, aes(y=position, x=GroupingVar, label=round(pct, digits=2)))+
facet_wrap(~FacetVar,ncol=1) +
coord_flip()+
scale_y_continuous(labels=percent) + ylab('Percentage')

Maciej Beręsewicz

unread,
Oct 1, 2012, 7:25:02 PM10/1/12
to ggp...@googlegroups.com, Maciej Beręsewicz
Thank you very much, this is really great anwser!

I (finally) did it on my own but it is much more complicated comparing to your response.

Brandon Hurr

unread,
Oct 1, 2012, 7:33:31 PM10/1/12
to Maciej Beręsewicz, ggp...@googlegroups.com
I think you can use mutate() instead of transform() to make each of those ddply() calls for m.ex2 into one. but the overall idea is the same. If the order of your factors change you might have to play with the position calculation to get them in the right place. Hopefully it was clear what I was doing... I'm not very good about commenting my code. 

Maciej Beręsewicz

unread,
Oct 1, 2012, 7:39:40 PM10/1/12
to Brandon Hurr, ggp...@googlegroups.com
Yes, indeed! I did it step by step and it was clear how you achieve final result. )
As I noticed you nedd to have aggregated data to add annotations to plot, this was the key to solve my problem.
--
pozdrawiam,
MB

===========================

Maciej Beręsewicz
tel: 663 06 26 87
mail: maciej.b...@gmail.com

Reply all
Reply to author
Forward
0 new messages