英文:
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
生成这种类型的图形?
我搜索了一下,发现了很少关于这个的结果...
英文:
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"))
But I want the graph to be more smooth, something like:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论