7

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

user1017124
  • 123
  • 1
  • 6

2 Answers2

6

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

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    I think you need to add ```width = 1``` to the ```geom_bar``` call because ```..density..``` draws the bars so that their area sum to one. This is only the same as the percentage falling into a bin when the bins are of width 1 and after some investigation I have found that the default width is 0.9 when the x axis is a factor (line 79 stat-bin.r). Unfortunately this also alters the appearance of the bars. To verify the problem, consider the HARDBALL panel which seems to show that the tallest bar has proportion greater than 100%. – orizon Mar 13 '13 at 07:45
  • @orizon Good catch. I was going to update my answer, but the link to the OP's data seems dead. The other two options are `..ndensity..` or simply summarize your data outside of ggplot and use `stat = "identity"`. – joran Mar 13 '13 at 14:01
-1

this is no longer working in newer versions of ggplot. The way to do it is now + stat_count(aes(y=..prop..))

spore234
  • 3,550
  • 6
  • 50
  • 76
  • Can you expand on this please? Is it just an extra line to be added into the code provided in the question? – JPD Sep 12 '17 at 10:40