doraemon
doraemon

Reputation: 845

How to generate smooth graph using 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"))

enter image description here

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

enter image description here

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

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

Upvotes: 0

Views: 68

Answers (1)

zephryl
zephryl

Reputation: 17309

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)

enter image description here

Upvotes: 2

Related Questions