Reputation: 1000
What's the syntax for using a data frame as a lookup table for facet labels (https://ggplot2.tidyverse.org/reference/labeller.html)? For example:
require(ggplot2)
df <- data.frame(value = c(1,3), variable = letters[c(1,3)])
labels <- data.frame(variable = letters[1:3], label = c("Apple", "Banana", "Cherry"))
Here's a failed attempt:
ggplot(df, aes(x = "x", y = value)) + geom_col() + facet_grid( ~ variable, labeller = as_labeller(labels))
Upvotes: 0
Views: 359
Reputation: 193
you want a named character vector
labels <- data.frame(variable = letters[1:3],
label = c("Apple", "Banana", "Cherry"),
stringsAsFactors = F)
ggplot(df, aes(x = "x", y = value)) + geom_col() +
facet_grid( ~ variable, labeller = as_labeller(with(labels, setNames(label, variable))))
Upvotes: 1