What methods are there to make the graphics smoother

Data can be downloaded from here

  • PM is the concentration of PM2.5,
  • pop is the number of exposed individuals at the corresponding concentration
  • year

Then draw the number of people corresponding to different pm values for different years.

library(tidyverse)
library(ggridges)

pm_ua  <- readxl::read_excel("pm_ua_pop.xlsx") 

pm_ua  %>%
  mutate(year = factor(year, levels = c("2020", "2010", "2000"))) %>% 
  ggplot() +
  geom_density_ridges(
    aes(x = pm, y = year, height = pop, fill = year),
    size = 0,
    stat = 'identity'
  ) 

But I hope the graphics are smoother. I thought for a long time and used a foolish method, but it seems too unscientific and occupying too much memory.

d <- pm_ua %>%
    uncount(pop) 


d %>% 
  mutate(year = factor(year, levels = c("2020", "2010", "2000"))) %>% 
  ggplot() +
  geom_density_ridges(
    aes(x = pm, y = year, fill = year),
    stat = "density_ridges"
  ) 

Is there a better way to ask for advice? thanks

You could try using slider to calculate a windowed mean of the unsmooth data.

library(tidyverse)
library(ggridges)
library(slider)
download.file("https://github.com/perlatex/r-quesitons/raw/75f0ca4000acfd64b7991175d71a3208ac0164c0/data/pm_ua_pop.xlsx",
              destfile = "pm_ua_pop.xlsx",
              mode = "wb")

pm_ua  <- readxl::read_excel("pm_ua_pop.xlsx") 

pm_ua2 <- pm_ua  |>
  mutate(year = factor(year, levels = c("2020", "2010", "2000"))) |>
  group_by(year) |>
  mutate(mean_pop= slide_mean(pop,before=5L,
                              after=5L,
                              complete = FALSE))

ggplot(pm_ua2) +
  geom_density_ridges(
    aes(x = pm, y = year, height = mean_pop, fill = year),
    size = 0,
    stat = 'identity'
  )

image

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.