R 中的六边形和热型密度图

Hexagon and heat style Density Plots in R

我正在尝试创建一个显示机器误差与温度和湿度的图像。阅读论文后(见下图),似乎最好的方法是使用六边形图或密度图来显示这些错误。我的问题是,每次我创建 (1) 密度图时,它都会生成一个灰色图表,实际上没有显示任何数据 (2) 六角星图,它只显示计数数据。

Example of my subset of my data (with only the temperature, humidity and PMdata included as thats what I want to display

library(ggplot2)
ggplot(DM_EPA_1H)+
geom_hex(aes(x=Relative.humidity, y=Temperature, color=Diff_PM1)

Image produced with the hex

上图符合我的要求,但显然很难解释,因为它有计数数据。我不知道在什么情况下 (temperature/humidity) 我们会看到错误。

ggplot(DM_EPA_1H, aes(x=Relative.humidity,y=Temperature), na.rm = FALSE)+
stat_density_2d(aes(fill=Diff_PM1), geom = "polygon")+
scale_fill_viridis_c()

Image produced by stat_density

上面这张图片不是很容易理解,我不确定下一个最佳途径是什么来获得预期的结果。

Desired format for displaying data. Credit Lui et al., 2019 (Atmosphere, 10, 41)

不幸的是,上面的图片没有任何关于他们如何制作这些图片的源代码,因此很难复制。它仍然有可能甚至没有在 ggplot 中完成,但对我来说它看起来像来源。

感谢您的帮助。让我知道是否需要更多说明

使用 stat_summary_hexgeom_density2d。使用 stat_summary_hex,您可以指定要为每个 bin 计算的内容而不是计数;在这里我假设你想要平均值,但你基本上可以使用任何函数。另外,你没有提供任何示例数据,所以有点困难,所以我随机生成了一些。

library(tidyverse)

set.seed(0)
DM_EPA_1H = tibble(Relative.humidity = (rbeta(1000, 6, 1.3)) * 100, Temperature = rnorm(1000, mean = 50, sd = 10), Diff_PM1 = rnorm(1000, mean = 0, sd = 5))

ggplot(DM_EPA_1H, mapping = aes(x = Relative.humidity, y = Temperature)) +
  stat_summary_hex(mapping = aes(z = Diff_PM1), fun = ~mean(.x)) +
  scale_fill_steps2(low = "#eb0000", mid = "#e0e0e0", high = "#1094c4") +
  geom_hex(stat = "identity") +
  geom_density2d(colour = "black") +
  geom_point(size = 0.5)

这大致重现了原始情节:

当然,如果您想像第二个代码示例中指出的那样使用 viridis,您也可以使用 scale_fill_viridis_c 而不是 scale_fill_steps2