2

This question explains how to add mathematical symbols inside a plot. But this doesn't work when the text is supposed to be stored in the data frame itself. This is the case for the text that appears in little boxes of the facet_wrap sub plots.

Here is a reproducible example. Let's say for example that I have this data in T and in m³ and that I would like to draw a plot like this one.

library(ggplot2)
dtf <- data.frame(year = rep(1961:2010,2),
                  consumption = cumsum(rnorm(100)),
                  item = rep(c("Tea bags","Coffee beans"),each=50),
                  unit = rep(c("T","m^3"),each=50))
ggplot(data=dtf)+
    geom_line(aes(x=year, y=consumption),size=1) +
    ylab(expression(paste("Consumption in T or ",m^3))) +
    scale_x_continuous(breaks = seq(1960,2010,10)) +
    theme_bw() + facet_wrap(item~unit, scales = "free_y")

enter image description here

ylab(expression(m^3)) displays the unit correctly. How could I display a similar m³ in the facet ?

Community
  • 1
  • 1
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • Maybe [this post](http://stackoverflow.com/questions/37399450/parsed-labels-along-one-facet-axis-unparsed-labels-along-the-other) will be helpful. – lmo May 24 '16 at 15:46

1 Answers1

5

add labeller = label_parsed to your facet_wrap function, format your unit as m^3, and replace spaces in your labels with ~

library(ggplot2)
dtf <- data.frame(year = rep(1961:2010,2),
                  consumption = cumsum(rnorm(100)),
                  item = rep(c("Tea~bags","Coffee~beans"),each=50),
                  unit = rep(c("T","m^3"),each=50))
ggplot(data=dtf)+
  geom_line(aes(x=year, y=consumption),size=1) +
  ylab(expression(paste("Consumption in T or ",m^3))) +
  scale_x_continuous(breaks = seq(1960,2010,10)) +
  theme_bw() + facet_wrap(item~unit, scales = "free_y",labeller = label_parsed)

Example plot from R showing desired solution

Edward R. Mazurek
  • 2,107
  • 16
  • 29
  • Thanks it would work but I have a space in my item name and using `label_parsed` returns an error. I modified the example with space in item names in my question. `Error in parse(text = as.character(values)) : :1:8: unexpected symbol 1: Coffee beans ^`. I see this has been answered [here](http://stackoverflow.com/questions/26269122/label-parsed-of-facet-grid-in-ggplot2-mixed-with-spaces-and-expressions). – Paul Rougieux May 24 '16 at 16:01
  • 1
    Try `item = rep(c("Tea~bags","Coffee~beans"),each=50)` – Edward R. Mazurek May 24 '16 at 16:13
  • 1
    Ok thanks replacing spaces by `~` is the solution. [An answer](http://stackoverflow.com/a/26269348/2641825) suggested creating my own labeller function but it seems to be deprecated now "Labellers taking `variable`and `value` arguments are now deprecated." – Paul Rougieux May 24 '16 at 16:15
  • Cool, glad it helped, I'll update my answer to reflect proper treatment of spaces – Edward R. Mazurek May 24 '16 at 16:16
  • 1
    Yes In my original data frame I replaced spaces by `~` with `dtf$item <- gsub(" ","~",dtf$item)`. – Paul Rougieux May 24 '16 at 16:23