我正在生成由不同组划分的数据点的 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 个元素(忽略第一个输出),正如您所期望的。
但更简单的是,您可以使用..密度..*100
ggplot(d, aes(x= x, y= y)) + stat_binhex(aes(fill= ..density..*100)) + facet_wrap(~grp)
关于r - 计算 ggplot2 stat_binhex 中 bin 的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950753/