2

I have solved my particular use case issue regarding this, but I hope that by posing this question I may assist others with similar issues and provide a bookmark to come back to in the future if I need to make similar graphics. When using library(ggplot2) the capability to facet plots into multi-panel figures is paramount to many analyses, however, getting the labels right using the labeller argument and/or function can be rather finicky when the underlying data is not formatted correctly. I present to you a simple data set to demonstrate my particular use case and some variations that caused me problems, as well as the solutions that I used. I also pose some questions about how the labeller function is used within the labeller argument to facet_wrap and/or facet_grid. The starting data:

dat <- tibble(label1 = c('one','two','three','four','five','six',
                         'six','five','four','three','two','one'),
              label2 = rep('T[alpha]'),
              fit = c(25,50,75,40,60,90,40,50,70,30,90,100),
              upr = c(35,60,85,50,70,100,50,60,80,40,100,110),
              lwr = c(15,40,65,30,50,80,30,40,60,20,80,90),
              var1 = rep(c(seq(1990,1995,1)),2))

Notice that label2 here is a character expression that can be evaluated as an expression. This appears to be the same as using base-R to plot:

plot(dat$var1, dat$fit)
mtext(expression(paste('T'[alpha])))

Essentially, as long as the expression can be evaluated within the expression function, it will work with label_parsed as an argument to facet_grid or facet_wrap. Here is what the code looks like for the actual plot I want:

p1 <- ggplot(dat, aes(x = var1, y = fit)) +
  geom_point() +
  geom_segment(aes(xend = var1, y = lwr, yend = upr)) +
  facet_grid(label1~label2, scales =  "fixed", 
             labeller = label_parsed) 

p1

Now if I want to use label1 with a space, I can use this solution as well, but need to include a tilde ~ anywhere the space should exist so that it is still able to be evaluated as an expression.

dat1 <- dat %>% 
  mutate(label1 = paste(label1, '~x', sep = ''))

dat1

p2 <- ggplot(dat1, aes(x = var1, y = fit)) +
  geom_point() +
  geom_segment(aes(xend = var1, y = lwr, yend = upr)) +
  facet_grid(label1~label2, scales =  "fixed", 
             labeller = label_parsed)

p2

This is as far as I had to go to solve my use case. Howver, to better understand the functionality I would like to ask the community to clarify another way of using labeller as a function. The question posed here: label_parsed of facet_grid in ggplot2 mixed with spaces and expressions eventually helped me arrive at my solution. However, if we use the code as written in that question, where the labeller argument is: labeller = labeller(type=label_parsed) the result is not what is desired (we get the character expression as written instead of being evaluated as an expression.

p3 <- ggplot(dat1, aes(x = var1, y = fit)) +
  geom_point() +
  geom_segment(aes(xend = var1, y = lwr, yend = upr)) +
  facet_grid(label1~label2, scales =  "fixed", 
             labeller = labeller(type=label_parsed)) # this is the change

p3

Can anyone explain when it would be appropriate to use labeller as a function within the call to labeller from facet_grid? My hope is that a solution exists that doesn't require re-formatting of the entire dataset to reflect expressions and perhaps that utility lies within the labeller function itself.

camille
  • 16,432
  • 18
  • 38
  • 60
dandrews
  • 967
  • 5
  • 18
  • 1
    `labeller` doesn't have a `type=` parameter. It's meant for you to use name of the columns that you want to change. Perhaps you want `labeller = labeller(label2=label_parsed)` since that's the one that contains the expressions. – MrFlick Jan 18 '22 at 18:32
  • @ Mr. Flick Thank you that works. And this also will work to update only a single label. Using this I could also use `labeller = labeller(label2=label_parsed, label1=label_value)` which is immensely helpful and prevents the need for changing underlying data if it not necessary! – dandrews Jan 18 '22 at 19:09

0 Answers0