覆盖ggplot2 stat_density2d绘制alpha通道在各组之间保持不变

时间:2015-03-13 14:36:16

标签: r plot ggplot2 kernel-density

我想在stat_density2图中绘制多个组,其中alpha值与每组中的观察计数相关。但是,stat_density2d形成的水平似乎标准化为每组中的观察数量。例如,

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()

创建如下图:

enter image description here

因为我只包括没有评级的2分,所以它们的密度看起来比其他两个更紧密/更强,所以洗掉了其他两个密度。我曾尝试查看Overlay two ggplot2 stat_density2d plots with alpha channelsSpecifying the scale for the density in ggplot2's stat_density2d,但他们并未真正解决此问题。

最终,我用我的真实数据试图完成的是,我有&#34; power&#34;来自多个条件的离散2d位置的样本,我试图绘制它们的相对功率/空间分布是什么。我在相对于它们的功率的位置上重复点,但是这导致了低功率条件,在使用stat_density2d时只有几个位置看起来最强。请告诉我是否有更好的方法来做这件事!

谢谢!

2 个答案:

答案 0 :(得分:2)

stat_hexbin,除..count..之外了解..density..,可能适合您:

ggplot(temp, aes(x=rating,y=length)) + 
    stat_binhex(geom="hex", aes(fill = mpaa, alpha=..count..)) + 
    theme_minimal()

虽然您可能想要调整纸槽宽度。

答案 1 :(得分:2)

不是最优雅的r代码,但这似乎有效。我将我的实际数据标准化的方式与此不同,但这得到了我发现的解决方案。我使用for循环,我找到条件的平均功率,并添加一个新的stat_density2d图层,其alpha值按平均功率缩放。

temp <- rbind(movies[1:2,],movies[movies$mpaa == "R" | movies$mpaa == "PG-13",])
mpaa = unique(temp$mpaa)
p <- ggplot() + theme_minimal()
for (ii in seq(1,3)) {
  ratio = length(which(temp$mpaa == mpaa[ii]))
  p <- p + stat_density2d(data=temp[temp$mpaa == mpaa[ii],], 
                          aes(x=rating,y=length,fill = mpaa, alpha=..level..),
                      geom="polygon", 
                      contour=TRUE, 
                      alpha = ratio/20, 
                      lineType = "none") 
}
print(p)