Plotting percent with stat_density in Plot Builder

48 views
Skip to first unread message

Bob Muenchen

unread,
Jan 22, 2012, 9:00:27 AM1/22/12
to ded...@googlegroups.com
Hi All,

I'm trying to duplicate a code example in Plot Builder. It's a stat_density plot of percent rather than proportion. I can get this using code by simply multiplying ..density.. by 100:

ggplot() +
stat_density(aes(x = College),data=USfacts, y = 100*..density..)

But the only way I see to enter the 100 into Plot Builder that I see is to click the "use a specific value" button and enter "100*..density.." That generates the call:

ggplot() +
stat_density(aes(x = College),data=USfacts,y = '100*..density..')

and the quotes around '100*..density..' block it from working. Is there another way to get it to display percents on the y-axis?

Thanks,
Bob

Ian Fellows

unread,
Jan 22, 2012, 1:45:18 PM1/22/12
to ded...@googlegroups.com
yes, you can right click on ..density.. and enter a custom value of
100*..density..

A better solution is to use ..density.. as is, and add the "y
continuous" scale to the plot with "percent" instead of "scientific"

i.e.
ggplot() +
geom_density(aes(x = mpg,y = ..density..),data=mtcars) +
scale_y_continuous(formatter = 'percent')

Ian

Bob Muenchen

unread,
Jan 29, 2012, 9:12:58 AM1/29/12
to ded...@googlegroups.com
Very nice. I had not known about formatter. Thanks Ian!  -Bob
Reply all
Reply to author
Forward
0 new messages