Changing sep in labeller

Using labeller = label_both allows both the variable name and value to appear in a facet, e.g.:

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + 
  facet_grid(vs ~ cyl, labeller = label_both)

I wanted to change the sep argument of label_both to an equal sign (i.e., sep = " = "). I found a solution that seems to work for me right now:

label_both_w_equal <- function(labels) label_both(labels, sep = " = ")
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + 
  facet_grid(vs ~ cyl, labeller = label_both_w_equal)

Because all I wanted to do was change the sep, I was wondering if there was another "built-in" way to do. For example, I started with what seemed natural to me:

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + 
  facet_grid(vs ~ cyl, labeller = label_both(sep = " = "))

But this doesn't work.

In any case, the problem is "solved" for now and wasn't too hard to fix. I was just wondering if there was a built-in way to change sep, or if going the custom labeller route is the intended way to go.

You can do it this way:

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + 
  facet_grid(vs ~ cyl, labeller = purrr::partial(label_both, sep = " = "))

Will this be enough?

3 Likes

That's good, I hadn't seen the partial function before.

1 Like