I have a dataset from which I would like to plot small multiples, specifically in a 2-by-2 array, like this:
mydf <- data.frame(letter = factor(rep(c("A", "B", "C", "D"), each = 20)), x = rnorm(80), y = rnorm(80))
ggplot(mydf, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() + facet_wrap(~ letter, ncol = 2)
However, I want each facet label to include an expression, such as
expression(paste("A or ", alpha))
I can make this happen using facet_grid()
via
f_names <- list('A' = expression(paste("A or ", alpha)), 'B' = expression(paste("B or ", beta)), 'C' = expression(paste("C or ", gamma)), 'D' = expression(paste("D or ", delta)))
f_labeller <- function(variable, value){return(f_names[value])}
ggplot(mydf, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() + facet_grid(~ letter, labeller = f_labeller)
But then I lose the 2-by-2 array. How can I rename the facet_wrap()
facet labels with an expression? Or, how can I solve this by recreating the 2-by-2 array using facet_grid()
, but only faceting by a single variable?
(This question builds off of the parenthetical note in @baptiste's answer to this previous question.)
Thanks!