如何使用`stat_summary_2d`生成平滑图表?

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

How to generate smooth graph using stat_summary_2d?

问题

生成示例数据集的代码:

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))

我使用stat_summary_2d生成了以下图形:

ggplot(templ, aes(temperature, relative_humidity_percent, z = count)) +
  stat_summary_2d(bins = 5) +
  viridis::scale_fill_viridis() +
  theme_classic()+
  theme(panel.background = element_rect(fill = "black"))

但我希望图形更加平滑,类似于:

如何使用`stat_summary_2d`生成平滑图表?

是否可能通过使用 stat_summary_2d 生成这种类型的图形?

我搜索了一下,发现了很少关于这个的结果...

英文:

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))

I used stats_summary_2d to generate the following graph:

ggplot(templ, aes(temperature, relative_humidity_percent, z = count)) +
  stat_summary_2d(bins = 5) +
  viridis::scale_fill_viridis() +
  theme_classic()+
  theme(panel.background = element_rect(fill = "black"))

如何使用`stat_summary_2d`生成平滑图表?

But I want the graph to be more smooth, something like:

如何使用`stat_summary_2d`生成平滑图表?

Is it possible to generate this type of graph by using stats_summary_2d?

I googled and found very few result about this...

答案1

得分: 2

你可以使用 tidyr::uncount() 函数展开你的数据,然后使用 geom_density_2d_filled()

library(ggplot2)
library(tidyr)

templ %>%
  uncount(count) %>%
  ggplot(aes(temperature, relative_humidity_percent)) +
  geom_density_2d_filled(show.legend = FALSE) +
  theme_classic() +
  coord_cartesian(expand = FALSE)

如何使用`stat_summary_2d`生成平滑图表?

英文:

You could tidyr::uncount() your data, then use geom_density_2d_filled().

library(ggplot2)
library(tidyr)

templ %>%
  uncount(count) %>%
  ggplot(aes(temperature, relative_humidity_percent)) +
  geom_density_2d_filled(show.legend = FALSE) +
  theme_classic() +
  coord_cartesian(expand = FALSE)

如何使用`stat_summary_2d`生成平滑图表?

huangapple
  • 本文由 发表于 2023年2月10日 09:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406258.html
匿名

发表评论

匿名网友

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

确定