Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

r - Obtaining Percent Scales Reflective of Individual Facets with 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...