Home > Software engineering >  Customize facet_wrap plot label in R?
Customize facet_wrap plot label in R?

Time:12-17

I'm creating a facet_wrap plot in R, and I'm trying to automate the labeller. I can create a custom label manually, using this code:

library(ggplot2)
library(tidyverse)
df <- data.frame(a = rep(c(1/8,1/4,1/2), each = 100),
                 b = rep(c("A", "B", "C", "D"), each = 25),
                 x = rnorm(100))


names <- c(
  `0.125` = "alpha~`=`~1/8",
  `0.25` = "alpha~`=`~1/4",
  `0.5` = "alpha~`=`~1/2"
)

df %>% ggplot()  
  geom_density(aes(x = x, colour = b)) 
  facet_wrap(~a, labeller = labeller(a = as_labeller(names, label_parsed)))

The above code produces this plot:

solutionPlot

  • Related