I would like o create my own labeller function to take input for facet labels, transform it to a string that contains math notation and then have ggplot parse this. I know I can change the underlying data and use label_parsed
but would like to have the ability to ake an easily generalizable functions that others could use. This also allows one to keep the data as is (including correct factor levels) and just swap the labels. Otherwise you need to mutate the underlying data, and then add the correct factor levels so ordering is done properly.
After reading http://ggplot2.tidyverse.org/reference/labeller.html here's what I've tried.
library(ggplot2)
## can be used with label_parsed but order not retained
mtcars$mpg2 <- ifelse(mtcars$mpg > 20, 'beta', 'alpha')
## can be used with label_parsed and retains levels (but this is 2 steps)
mtcars$mpg3 <- factor(mtcars$mpg2, levels = c('beta', 'alpha'))
ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
facet_grid(mpg3 ~ mpg2,
labeller = label_parsed
)
## attempt #1 at a function
label_as_notation <- function(x, ...){
parse(text = paste('"X:" ~ ', x, '<=', ifelse(x == 0, 'omega', 'psi')))
}
ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
facet_grid( ~ am,
labeller = labeller(am = label_as_notation)
)
So I wonder what does label_parsed
do?
> label_parsed(1:3)
[[1]]
[[1]][[1]]
expression(1)
[[2]]
[[2]][[1]]
expression(2)
[[3]]
[[3]][[1]]
expression(3)
A list of lists of expressions. So I tried:
label_as_notation2 <- function(x, ...){
y <- unlist(x)
lapply(x, function(y) {
lapply(y, function(z) {
parse(text = paste('"X:" ~ ', z, '<=', ifelse(z == 0, 'omega', 'psi')))
})
})
}
ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
facet_grid( ~ am,
labeller = labeller(am = label_as_notation2)
)
A similar think happens if I make label_parsed
into a function:
ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
facet_grid( ~ am,
labeller = labeller(am = function(x) label_parsed(x))
)
So I guess it's not surprising my second approach didn't work. I have tried as_labeller
wrapped around functions that return character but that doesn't parse.
How can I make a generalizable labeller that can switch labels with math notion on the fly so that they get parsed correctly by ggplot2?