Paul Lemmens
Paul Lemmens

Reputation: 635

Add data frame variable to ggplot2 facet label

I want add some additional information into the facet labels of a ggplot plot from the data that the plot is built on. For various reasons I'm trying to do this after the fact instead of relying on adhoc changes to the data.

I tried to model my labeller function on label_wrap_gen thinking that I might just as well add in a data frame variable as parameter to the function instead of just a fixed number that label_wrap_gen takes.

However, that does not work as expected. Instead of the desired "4 (5)" in the leftmost facet, I'm getting "4 ()". The problem seems to be that app_n does not get the actual data in, or something like that. If instead of .data$en (below) use just en, I'm getting an error for a variable not found.

app_n <- function(.n) {
  fun <- function(labels) {
    labels <- label_value(labels, multi_line = TRUE)
    lapply(labels, function(x) {
      x <- paste0(x, '(', .n, ')')
      vapply(x, paste, character(1), collapse = "\n")
    })
  }
  structure(fun, class = 'labeller')
}

mtcars %>%
  group_by(cyl) %>%
    mutate(en = 5) %>%
ggplot(aes(drat, wt)) +
  facet_wrap(~ cyl, labeller = app_n(.n = .data$en))

The reason why I'm taking this route is that in my actual application I'm trying to add a sample size to the facet label and for reasons like "I want to learn if this is possible" as well as "code is already complicated enough" I'm first trying to take this route.

Can this work at all? (yes, I am aware that it might result in a vector being inserted as text. While that wouldn't work for my actual plot, at this stage I would be happy to get that result)

Upvotes: 0

Views: 344

Answers (1)

O.A
O.A

Reputation: 141

It is possible to pass the column en to your labelling function by changing .data to ., and using {} to let the pipe know that there are two data args.

mtcars %>%
  group_by(cyl) %>%
    mutate(en = 5) %>%
{ggplot(., aes(drat, wt)) +
  facet_wrap(~ cyl, labeller = app_n(.n = .$en))}

This returns the expected labels.

However, using a data frame variable itself to label your facets implies that there are as many facets as rows. Otherwise only the first few labels are used and a warning is returned.

Upvotes: 1

Related Questions