计算ggplot2 stat_binhex中箱的百分比 [英] calculating percentages for bins in ggplot2 stat_binhex
问题描述
我正在生成不同组别的数据点binhex图。每个组可能具有不同的总点数,因此,而不是每个bin值是绝对点数,我希望它是该组内总点数的百分比。这是我目前正在尝试的:
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)
这是正确的吗?是 sum(.. count ..)
以每个方面为基础生成总积分(组a为10000,组b为5000),还是它导致15000两个方面?
您是正确的。
> 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
这意味着数据被分为10000和5000个元素(忽略第一个输出),这是你期望的。
但更容易的是,您可以使用 .. density .. * 100
ggplot(d,aes(x = x,y = y))+ stat_binhex(aes(fill = ..density .. * 100))+ facet_wrap(〜grp)
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)
这篇关于计算ggplot2 stat_binhex中箱的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!