将密度图覆盖到 ggplot2 中每个现有的小平面包裹密度图?

Overlay density plot to each existing facet wrapped density plot in ggplot2?

我有一个包含 ~37000 行的数据框,其中包含字符串格式的 'name' 和 posixct 格式的 'UTCDateTime',我正在使用它来生成按名称分组的时间面包裹密度图:

我还有一个来自完全不同的数据帧的 posixct 日期时间数据的单独密度图:

我想将第二个密度图叠加到第一个密度图中的每个 facet_wrapped 图上。有没有办法做到这一点?一般来说,如果我有任何类型的小平面包裹的图和另一个相同类型但不同数据的图,我想覆盖在小平面包裹的每个小平面上,我该怎么做?

这在理论上应该很简单,就像在第二个数据框中没有您要分面的列一样。示例如下:

library(ggplot2)

ggplot(iris, aes(Sepal.Width)) +
  geom_density(aes(fill = Species)) +
  geom_density(data = faithful,
               aes(x = eruptions)) +
  facet_wrap(~ Species)

reprex package (v0.3.0)

于 2020-08-12 创建

编辑:要获得两种类型数据的相同比例的密度,您可以使用计算变量 after_stat()*:

ggplot(iris, aes(Sepal.Width)) +
  geom_density(aes(y = after_stat(scaled),
                   fill = Species)) +
  geom_density(data = faithful,
               aes(x = eruptions,
                   y = after_stat(scaled))) +
  facet_wrap(~ Species)

* 在 ggplot2 v3.3.0 之前还有 stat(variable)...variable....