Search code examples
rggplot2ggridges

stack bars with stat_binline()


I have data similar to the example below.

I am wanting to visualise the spread of the outcome variable (value) for each group (name). The fill aesthetic is the desired interval - the example below uses the interquartile range.

I would expect the position="identity" to stack the bars on top of each other for the fill aesthetic (as it does for geom_bar). This is the behaviour that I want.

When I try position="stack", it's a mess.

I have looked at the stat_binline examples and the ggridges vignette but neither have examples where the position is modified to stack the ridges (binned or not).

library(ggplot2)
library(ggridges)
set.seed(123)

size <- 1000
data.frame(
  name=sample(LETTERS[1:5], size=size, replace=T),
  value=c(sample(1:20, size=size*0.8, replace=T), rep(15, size*0.2))
) %>%
  group_by(name) %>%
  arrange(value) %>%
  mutate(percentile=row_number()/n()) %>%
  ungroup() %>%
  mutate(in_interval=percentile > 0.25 & percentile < 0.75)%>%
  ggplot(aes(x = value, y = name, height = stat(count), fill=in_interval)) +
  stat_binline(position = "identity", alpha=0.3, bins=20, scale=0.9) +
  coord_flip()

The overlap that I want to avoid is shown here. I want these bars to the stacked instead.

enter image description here

Thank you!


Solution

  • I reviewed the ggridges docs - https://wilkelab.org/ggridges/reference/stat_binline.html

    The ggplot position page - https://ggplot2.tidyverse.org/reference/position_stack.html

    And a few of the great [ggridges] tagged answers on SO -

    https://stackoverflow.com/a/58557352/10276092

    Add color gradient to ridgelines according to height

    And all I've produced is a non-ggridges answer:

    df %>% 
      ggplot(aes(x=value, fill=in_interval)) +
      geom_histogram(bins=20) +      
      facet_grid(cols=vars(name)) +
      coord_flip()
    

    enter image description here