How to scale (normalise) values of ggplot2 stat_bin2d within each column (by X axis)

你离开我真会死。 提交于 2019-12-14 04:17:38

问题


I have a ggplot stat_bin2d "heatmap".

library(ggplot2)
value<-rep(1:5, 1000)
df<-as.data.frame(value)    
df$group<-rep(1:7, len=5000)
df<-df[sample(nrow(df), 3000), ]
ggplot(df, aes(factor(group), factor(value))) +stat_bin2d()

I have tried to add fill to aes:

aes(factor(group), factor(value),fill = (..count..)/mean(..count..))

as a way to mimic ..density.. (not accepted) does not seem to be accepted, but it is not what I am wanting - it seems to divide by the sum of the counts for the whole df. I want the count of values in each group (by x axis) normalised by the mean (or sum, or other stat) within the group. unfortunately, sum(..count..) seems to give the sum of the whole df, not only of the column.

来源:https://stackoverflow.com/questions/30803999/how-to-scale-normalise-values-of-ggplot2-stat-bin2d-within-each-column-by-x-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!