我想在 stat_密度2 图中绘制多个组,其中 alpha 值与每组中的观察计数相关。然而,由 stat_密度2d 形成的水平似乎已标准化为每组中的观察数量。例如,
temp <- rbind(movies[1:2,],movies[movies$mpaa == "R" | movies$mpaa == "PG-13",])
ggplot(temp, aes(x=rating,y=length)) +
stat_density2d(geom="tile", aes(fill = mpaa, alpha=..density..), contour=FALSE) +
theme_minimal()
创建如下图:
因为我只包含了 2 个没有评级的点,所以它们导致的密度看起来比其他两个更紧/更强,因此消除了其他两个密度。我尝试查看 Overlay two ggplot2 stat_density2d plots with alpha channels 和 Specifying the scale for the density in ggplot2's stat_density2d,但它们并没有真正解决这个具体问题。
最终,我试图用我的真实数据完成的是,我有来自多个条件的离散二维位置的“功率”样本,并且我试图绘制它们的相对功率/空间分布。我在相对于其功率的位置上复制点,但这导致了低功率条件,在使用 stat_密度2d 时,只有几个位置看起来最强。如果有更好的方法,请告诉我!
谢谢!
最佳答案
stat_hexbin
,它除了 ..密度..
之外还可以理解 ..count..
,可能对您有用:
ggplot(temp, aes(x=rating,y=length)) +
stat_binhex(geom="hex", aes(fill = mpaa, alpha=..count..)) +
theme_minimal()
尽管您可能想调整 bin 宽度。
关于r - 覆盖 ggplot2 stat_密度2d 图,各组之间的 alpha channel 恒定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29035000/