Interest calculation for bins in ggplot2 stat_binhex

I am creating binhex plots of data points bordering different groups. Each group potentially has a different total number of points, therefore, and not every bin value, which is the absolute number of points, I would like this percentage to the total amount of points in this group. Here is what I'm trying now:

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) 

It is right? Is sum(..count..) total points based on each face (10000 for group “a” and 5000 for group “b”), or does this result in 15000 for both fascia?

+4
source share
1 answer

You're right.

 > 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 10,000 and 5,000 elements (ignore the first output) that you expect.

But easier, you can use ..density..*100

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

Source: https://habr.com/ru/post/1392172/


All Articles