5

I would like to plot a figure to custom the strip text with expressions and normal strings (included spaces), but got an error when the normal text contained spaces. This is an example to reproduce my question:

library(ggplot2)
labels <- c("'FT'[1]*' - Ctrl'", 
        "'FT'[tot]*' - FT'[1]*''",
        "'FT'[tot]*' - Ctrl'")
lbl_pd <- data.frame(impact = c('Direct impact', 'Direct impact', 
            'Indirect impact', 'Indirect impact'),
        type = c(labels[1], labels[2], labels[1], labels[2]),
        Latitude = -22.5, Longitude = 115,
        label = c('a', 'b', 'c', 'd'))
p <- ggplot(lbl_pd)
p <- p + geom_text(aes(Longitude, Latitude, label = label), size = 2)

p <- p + facet_grid(type~impact, labeller = label_parsed)
p

The error is

Error in parse(text = x) : <text>:1:8: unexpected symbol
1: Direct impact
       ^

There is no error if column "impact" without any spaces.

How could I solve this problem? Thanks for any suggestions.

Bangyou
  • 9,462
  • 16
  • 62
  • 94
  • 3
    You can replace spaces by `~` these will then be parsed as spaces. See comments on [this answer](http://stackoverflow.com/a/37418333/2641825). – Paul Rougieux May 24 '16 at 16:25

2 Answers2

8

As of 2018, this is now possible to do directly with ggplot2:

p + facet_grid(type~impact, labeller = labeller(type = label_parsed))

Here's the link to the Tidyverse help page.

enter image description here

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
  • any idea how to incorporate a greek letter? I need to change one label out to be `expression(paste("L"[infinity]))`. If I omit the expression and just use `paste('L'[infinity]` I get an error `unkown object "infinity"`. – dandrews Jan 15 '22 at 15:55
  • @dandrews I don't know. I would encourage you to ask a new question with your MVCE. – Richard Erickson Jan 17 '22 at 01:19
  • thank you, I was able to figure it out. Had to do with using the labeller function as a response to the labeller argument call to facet_grid. I posed a questions regarding this: https://stackoverflow.com/questions/70758135/what-are-the-differences-in-use-of-labeller-as-function-and-as-argument-in-ggplo hopefully it will help clarify. – dandrews Jan 18 '22 at 15:41
3

You can substitute your own label function:

library(plyr)

my_label_parsed <- function (variable, value) {
  if (variable == "impact") {
    return(as.character(value))
  } else {
    llply(as.character(value), function(x) parse(text = x))    
  }
}

library(ggplot2)

labels <- c("'FT'[1]*' - Ctrl'", 
        "'FT'[tot]*' - FT'[1]*''",
        "'FT'[tot]*' - Ctrl'")
lbl_pd <- data.frame(impact = c('Direct impact', 'Direct impact', 
            'Indirect impact', 'Indirect impact'),
        type = c(labels[1], labels[2], labels[1], labels[2]),
        Latitude = -22.5, Longitude = 115,
        label = c('a', 'b', 'c', 'd'))
p <- ggplot(lbl_pd)
p <- p + geom_text(aes(Longitude, Latitude, label = label), size = 2)

p <- p + facet_grid(type~impact, labeller = my_label_parsed)
p

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205