tomw
tomw

Reputation: 3158

ggridges with heights scaled to counts

How do I change the scaling of a ggridges figure so that the plot behaves more like a histogram, and less like a kernel density plot? That is, I would like the figure to reflect the disparate size of the categorical variable.

For instance

library(tidyverse)
library(ggridges)

data(mpg)    

mpg %>% 
  mutate(
    drv = drv %>% 
      fct_reorder(
        cty
        )
    ) %>% 
  ggplot(
    aes(cty, drv)
    ) +
  geom_density_ridges(
    stat = "binline", 
    scale = .8
    )

enter image description here

the issue here is that the r category of mpg$drv has only 25 observations, while both f and 4 have over 100 observations each. I want the height of the figure to reflect the count of observations at each point

Upvotes: 3

Views: 1490

Answers (1)

Dave Gruenewald
Dave Gruenewald

Reputation: 5689

Funny enough, your title is essentially the solution. You will want to include the height = ..count.. in your aes()

mpg %>% 
  mutate(
    drv = drv %>% 
      fct_reorder(
        cty
      )
  ) %>% 
  ggplot(
    aes(cty, drv, height = ..count..)
  ) +
  geom_density_ridges(
    stat = "binline", 
    scale = .8
  )

Which gives you the following: unscaled height ggpridges

Upvotes: 5

Related Questions