0

I am trying to create a ridgeline plot-type figure that displays count data on the Y-axis over time on the X-axis for individual years, stacked atop each other in a ridgeline plot (joyplot) type format. However, R only seems capable of supporting density plots in this format, where the Y axis is the distribution of the count data by frequency.

week <- c(rep(1:52,3)) # Simulated data
year <- c(rep(2012,52),rep(2013,52),rep(2014,52))
x1 <- rnbinom(52, 10, .5)
x2 <- rnbinom(52, 10, .5)
x3 <-rnbinom(52, 10, .5)
x <- c(x1,x2,x3)
df <- as.data.frame(cbind(year,week,x))

df %>% ggplot(aes(x=x,y=year)) +   
        geom_density_ridges(aes(fill=year,group=year))

# This shows the density of 'x' for each year 
# whereas I would like to show the actual 
# values of 'x' by week for each year

Is there a way to specify the X axis as 'week', the Y axis as a numeric value 'x', and each stacked ridgeline as 'year'?

Quinten
  • 35,235
  • 5
  • 20
  • 53
mmi
  • 1
  • 1

1 Answers1

1

I think you are looking for geom_ridgeline instead of geom_density_ridges, with the height aesthetic mapped to your x variable:

df %>% 
  ggplot(aes(x = week, y = year, group = year, height = x, fill = year)) +   
  geom_ridgeline(scale = 0.06) +
  scale_fill_distiller(palette = 'Set2', guide = 'none') +
  theme_minimal(base_size = 16)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87