Search code examples
rggplot2gradientboxplotviolin-plot

Create barplot in R with gradient based on density


I want to represent the density of a number of variables as you would in a boxplot, violin plot, or beeswarm. But in this case, each variable would be a band with the density displayed as a gradient along the bar.

Hopefully I don't need to manually draw bars as filled shapes.

Imagine if instead of the violins or boxplots, there was a bar with a gradient representing the density.

enter image description here

library(tidyverse)
library(ggplot)

df = data.frame(
  A = 2.3 + 7*rnorm(100),
  B = 0 + 5*rnorm(100),
  C = 4 + 2*rnorm(100)
)

df %>%
  gather() %>%
  ggplot(aes(x=key, y=value)) + 
  geom_violin(scale="width", fill='red', alpha=0.5) + 
  geom_boxplot(fill='green', alpha=0.5)

Solution

  • So this is my closest approximation of what I got from your question:

    # Dummy data
    df <- data.frame(
      y = c(rnorm(100, 4), rnorm(100, 12)),
      x = rep(c(1, 2), each = 100)
    )
    
    ggplot(df, aes(x, y, group = x)) +
      # To fill gap between 0 and actual data
      stat_summary(geom = "rect",
                   fun.ymin = function(x){0},
                   fun.ymax = min,
                   aes(xmin = x - 0.4, xmax = x + 0.4, fill = 0)) +
      # To make the density part
      stat_ydensity(aes(fill = stat(density), 
                        xmin = x - 0.4, xmax = x + 0.4,
                        # Nudge y by a bit depending on the spread of your data
                        ymin = stat(y) - 0.01, ymax = stat(y) + 0.01), 
                    geom = "rect", trim = FALSE)
    

    enter image description here

    Does that fit the bill?