如何使用R的ggplot2绘制具有3个连续变量的热图?

huangapple go评论60阅读模式
英文:

How to plot a heatmap with 3 continuous variables in r ggplot2?

问题

样本数据如下:
count 是离散变量,temperaturerelative_humidity_percent 是连续变量。

生成样本数据的代码如下:

templ = data.frame(count = c(200,225,610,233,250,210,290,255,279,250),
                  temperature = c(12.2,11.6,12,8.5,4,8.2,9.2,10.6,10.8,10.9),
                  relative_humidity_percent = c(74,78,72,65,77,84,83,74,73,75))
count temperature relative_humidity_percent
200 12.2 74
225 11.6 78
610 12 72
233 8.5 65
250 4 77
210 8.2 84
290 9.2 83
255 10.6 74
279 10.8 73
250 10.9 75

我尝试使用 ggplot2::stat_contour 绘制热力图,


plot2 <- ggplot(templ, aes(x = temperature, y = relative_humidity_percent, z = count)) +
  stat_contour(geom = 'contour') +
  geom_tile(aes(fill = n)) +
  stat_contour(bins = 15) +
  guides(fill = guide_colorbar(title = 'count'))

plot2

结果如下:
如何使用R的ggplot2绘制具有3个连续变量的热图?

此外,我尝试使用 ggplot::stat_density_2d,

> ggplot(templ, aes(temperature, relative_humidity_percent, z = count)) +
+   stat_density_2d(aes(fill = count)) 
警告信息:
1: In stat_density_2d(aes(fill = count)) :
  忽略未知的美观:fill
2: 经统计变换期间已丢弃以下美观:fill, z
ℹ 当 ggplot 无法推断数据中的正确分组结构时,就会发生这种情况。
ℹ 是否忘记指定 `group` 美观或将数值变量转换为因子? 
> geom_density_2d() + 
+   geom_contour() +
+   metR::geom_contour_fill(na.fill=TRUE) +
+   theme_classic()
错误:`+.gg`:无法将 <ggproto> 对象相加
ℹ 是否忘记将此对象添加到 <ggplot> 对象中?
运行 `rlang::last_error()` 以查看错误发生的位置。

结果如下:
未填充颜色。
如何使用R的ggplot2绘制具有3个连续变量的热图?

我想要的是:

如何使用R的ggplot2绘制具有3个连续变量的热图?

我希望在图中将 level 替换为 count。然而,由于 count 变量不是因子,因此无法使用 ggplot::geom_contour 绘制热力图...

英文:

Sample dataset is as below:
count is discrete variable, temperature and relative_humidity_percent are continuous variables.

The code to generate sample dataset:

templ = data.frame(count = c(200,225,610,233,250,210,290,255,279,250),
                  temperature = c(12.2,11.6,12,8.5,4,8.2,9.2,10.6,10.8,10.9),
                  relative_humidity_percent = c(74,78,72,65,77,84,83,74,73,75))
count temperature relative_humidity_percent
200 12.2 74
225 11.6 78
610 12 72
233 8.5 65
250 4 77
210 8.2 84
290 9.2 83
255 10.6 74
279 10.8 73
250 10.9 75

I tried to plot a heatmap with ggplot2::stat_contour,


plot2 &lt;- ggplot(templ, aes(x = temperature, y = relative_humidity_percent, z = count)) +
  stat_contour(geom = &#39;contour&#39;) +
  geom_tile(aes(fill = n)) +
  stat_contour(bins = 15) +
  guides(fill = guide_colorbar(title = &#39;count&#39;))

plot2

The result is:
如何使用R的ggplot2绘制具有3个连续变量的热图?

Also, I tried to use ggplot::stat_density_2d,

&gt; ggplot(templ, aes(temperature, relative_humidity_percent, z = count)) +
+   stat_density_2d(aes(fill = count)) 
Warning messages:
1: In stat_density_2d(aes(fill = count)) :
  Ignoring unknown aesthetics: fill
2: The following aesthetics were dropped during statistical transformation: fill, z
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor? 
&gt; geom_density_2d() + 
+   geom_contour() +
+   metR::geom_contour_fill(na.fill=TRUE) +
+   theme_classic()
Error in `+.gg`:
! Cannot add &lt;ggproto&gt; objects together
ℹ Did you forget to add this object to a &lt;ggplot&gt; object?
Run `rlang::last_error()` to see where the error occurred.

The result:
which was not filled with colour.
如何使用R的ggplot2绘制具有3个连续变量的热图?

What I want is:

如何使用R的ggplot2绘制具有3个连续变量的热图?

I want to replace level with count in the graph. However, since count variable is not factor. Therefore I cannot plot heatmap by using ggplot::geom_contour...

答案1

得分: 1

只使用geom_point并根据计数进行颜色设置。当然,您也可以将点形状设置为正方形。

或者,如果您的计数尚未实际上是一个聚合度量,而您想要显示相邻观测值的密度,您可以使用ggpointdensity::geom_pointdensity来实现这一点(在您的示例中,我需要先取消计数)。

library(ggplot2)
library(dplyr)
library(tidyr)

templ = data.frame(count = c(200,225,610,233,250,210,290,255,279,250),
                   temperature = c(12.2,11.6,12,8.5,4,8.2,9.2,10.6,10.8,10.9),
                   relative_humidity_percent = c(74,78,72,65,77,84,83,74,73,75))

ggplot(templ) +
  geom_point(aes(temperature, relative_humidity_percent, color = count), shape = 15, size = 5)

如何使用R的ggplot2绘制具有3个连续变量的热图?


## 首先取消计数
templ %>%
  uncount(count) %>%
ggplot() +
  ggpointdensity::geom_pointdensity(aes(temperature, relative_humidity_percent))

如何使用R的ggplot2绘制具有3个连续变量的热图?

英文:

Just use geom_point and color according to your count. You can of course make your points square.

Or, if your count is not yet actually an aggregate measure and you want to show the density of neighbouring observations, you could use ggpointdensity::geom_pointdensity for this. (in your example, I have to uncount first).

library(ggplot2)
library(dplyr)
library(tidyr)

templ = data.frame(count = c(200,225,610,233,250,210,290,255,279,250),
                   temperature = c(12.2,11.6,12,8.5,4,8.2,9.2,10.6,10.8,10.9),
                   relative_humidity_percent = c(74,78,72,65,77,84,83,74,73,75))

ggplot(templ) +
  geom_point(aes(temperature, relative_humidity_percent, color = count), shape = 15, size = 5)

如何使用R的ggplot2绘制具有3个连续变量的热图?<!-- -->


## first uncount 
templ %&gt;%
  uncount(count) %&gt;%
ggplot() +
  ggpointdensity::geom_pointdensity(aes(temperature, relative_humidity_percent))

如何使用R的ggplot2绘制具有3个连续变量的热图?<!-- -->

答案2

得分: 1

我了解您的评论,您希望“填充整个图形”,从而对三维数据的准确表示进行了一些牺牲,这可以更准确地表示为散点图,并本地编码您的第三个变量。我明白您的意图是在测量位置之间插值观察密度。

您当然可以使用geom_density_2d来实现这一点。只需像我其他回答中所示那样使用相同的技巧,首先取消计数数据。

注意:这当然是创建密度箱的过程。否则,具有等密度线的此类型可视化将无法正常工作。

ggplot(tidyr::uncount(templ, count)) +
  geom_density_2d_filled(aes(temperature, relative_humidity_percent))
英文:

I understand from your comment that you want to "fill the entire graph", thus having a less truthful representation of your three dimensional data, which would be more accurately represented as a scatter plot and local coding of your third variable. I understand that you intend to interpolate the observation density between the measured locations.

You can of course use geom_density_2d for this. Just do the same trick as in my other answer and uncount your data first.

NB this is of course creating bins of densities. Otherwise this type of visualisation with iso density lines is not working.

ggplot(tidyr::uncount(templ, count)) +
  geom_density_2d_filled(aes(temperature, relative_humidity_percent))

如何使用R的ggplot2绘制具有3个连续变量的热图?<!-- -->

huangapple
  • 本文由 发表于 2023年2月8日 15:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定