Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining Percent Scales Reflective of Individual Facets with ggplot2

Tags:

r

ggplot2

So I managed to get this far...

ggplot(init, aes(x=factor(ANGLE), fill=NETWORK)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
facet_wrap(~SHOW) + opts(legend.position = "top") +
scale_y_continuous(labels = percent_format())

My problem is that the colored bars below represent the percent of ALL the camera ANGLE measurements for all the television programs in my study. For instance, the OREILLY graph has a bar that approaches 15% for ANGLE 2, which is %15 for all the ANGLE measurements in the chart, not solely those in the OREILLY facet. What I want each graph to show is the percentage of counts relative to just ONE television show (just that one facet), rather than all of them.

The idea is to compare the proportional use of camera angles among different shows, but with the way the graph is now, it is skewed to make the shows with more camera angle changes look as though they spend way more time at camera angle 2 than they actually do relative to the others.

The frustrating part of it all is that I spent an hour getting this to look the way I wanted, then I made the mistake of updating R. The packages updated along with it, and this happened.

A reduced size data table is available here.

EDIT: This doesn't work either. I tried putting "group=NETWORK" in either (and both) of the aes(..., ) terms, but nothing changed. I also tried the same thing with "group=SHOW", which I thought might have more of a chance since I wanted to get just one percentages for one SHOW in each facet (hence, the scales for each facet should go up to about 80% since so many of the shows are predominantly camera angle 2). Am I missing something?

ggplot(init, aes(x=factor(ANGLE), fill=NETWORK), group=SHOW)
+ geom_bar(aes(y = (..count..)/sum(..count..), group=NETWORK)) +
+ facet_wrap(~SHOW) + opts(legend.position = "top") +
+ scale_y_continuous(labels = percent_format())

Problematic Graphic

like image 224
user1017124 Avatar asked Mar 08 '12 08:03

user1017124


1 Answers

Using the ..density.. stat rather than ..count.. seems to work for me:

ggplot(dat, aes(x=factor(ANGLE))) +
 geom_bar(aes(y = ..density..,group = SHOW,fill = NETWORK)) +
 facet_wrap(~SHOW) + 
 opts(legend.position = "top") +
 scale_y_continuous(labels = percent_format())

At least, this produces a different result, I can't say for sure it reflects what you want. Additionally, I'm not sure why the ..count.. stat was behaving that way.

enter image description here

like image 103
joran Avatar answered Sep 30 '22 01:09

joran