r - 覆盖 ggplot2 stat_密度2d 图,各组之间的 alpha channel 恒定

标签 r plot ggplot2 kernel-density

我想在 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()

创建如下图:

enter image description here

因为我只包含了 2 个没有评级的点,所以它们导致的密度看起来比其他两个更紧/更强,因此消除了其他两个密度。我尝试查看 Overlay two ggplot2 stat_density2d plots with alpha channelsSpecifying 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/

相关文章:

r - 在标题中插入图像时出现问题

python - 绘制数据框中按分组的聚合

python - 如何使用 Matplotlib 加速绘制大量矩形?

rCharts Polychart : Adding horizontal or vertical lines to a plot

r - as.POSIXct.numeric(value) : 'origin' must be supplied in ggplot 中的错误

r - 无法在 WSL Ubuntu 18.04 上安装 R Shiny Server

r - 如何将唯一的计数器列附加到数据框

read.table读取数字作为因子

r - 在 R 中执行具有挑战性的水平点图,显示最小值、最大值、平均值和标准值

r - 具有三个度量的分组条形图