简体   繁体   中英

calculating percentages for bins in ggplot2 stat_binhex

I'm generating binhex plots of data points faceted by different groups. Each group potentially has a different total number of points, so rather than each bin value being the absolute number of points, I'd like it to be the percentage of the total points within that group. Here's what I'm presently trying:

d <- data.frame(grp= c(rep('a',10000), rep('b',5000)), 
                x= rnorm(15000), 
                y= rnorm(15000))
ggplot(d, aes(x= x, y= y)) + 
     stat_binhex(aes(fill= ..count../sum(..count..)*100)) + 
     facet_wrap(~grp)

Is this correct? Is sum(..count..) producing the total points on a per facet basis (10000 for group 'a' and 5000 for group 'b'), or is it resulting in 15000 for both facets?

You are correct.

> ggplot(d, aes(x= x, y= y)) + stat_binhex(aes(fill= {print(sum(..count..));..count../sum(..count..)*100})) + facet_wrap(~grp)
[1] 10000
[1] 10000
[1] 5000

this means that the data is divided into 10000 and 5000 elements (ignore the first output), which you expect.

But more easily, you can use ..density..*100

ggplot(d, aes(x= x, y= y)) + stat_binhex(aes(fill= ..density..*100)) + facet_wrap(~grp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM