在 ggplot2 ggridges 中禁用直方图缩放

Disable Histogram Scaling in ggplot2 ggridges

请查看以下一年中各个月份的温度直方图。我将温度限制在 50 度以上,以便在较冷的月份有目的地强制某些直方图 。记下第 1、2 和 3 个月,它们太小了,几乎没有记录在侧面图上。

library(nycflights13)
library(ggplot2)
library(dplyr)
ggplot(weather %>% filter(temp > 50), aes(temp)) +
  geom_histogram() + 
  facet_wrap(~ as.factor(month))

这个 ggridges 包很棒。它还绘制直方图。默认情况下,它会缩放直方图,使 y 值的高度相对相同。我如何禁用它? 我知道我必须以某种方式指定 height = ..stat_identity_count..height = ..y.. 但我已经尝试了所有可能的组合但无法弄清楚。在下面的图中,上面几乎不引人注意的第 1、2 和 3 个月现在已按比例放大并变得巨大。我希望 y 轴的高度反映它们各自直方图箱的实际计数。就像原来的 facet wrap 示例一样。

library(ggridges)
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges()

而且我知道通过..density.. 与绝对计数比较直方图通常更容易,但这不是我当前分析所需要的。

这可以通过 stat_density() 来完成,使用 ..count.. 美学而不是 ..density..:

ggplot(weather %>% filter(temp > 50),
       aes(x = temp, y = as.factor(month),
           group = as.factor(month), height = ..count..)) + 
  geom_density_ridges(stat = "density")