7

I am trying to plot three variables and want the units in the axes labels but can't find a way to label them properly in facets with the superscripts.

I've tried as_labeller, label_bquote, expression/paste and changing the original data.

p <- ggplot(data = LST, aes(x = Date, y = Measurements)) + 
geom_point((aes(color = parameter)))

p + facet_grid(parameter ~ ., scales = "free_y", 
switch="y",labeller=as_labeller(DO~(mg~L^{-1}), Temperature~(°C), Light~ 
(µmol~m^{-2}~s^{-1}))) + 
            theme_bw()+ theme(strip.background = element_blank(), 
legend.title = element_blank(), strip.placement = "outside", 
panel.grid.minor = element_blank()) + 
          scale_x_datetime()+ ylab(NULL) +ggtitle("Spring 2018") + 
scale_colour_manual(values=c('royalblue1', 'springgreen4', 'darkblue')) +
          theme(legend.position="none")+ 
theme(strip.text=element_text(size=10))

Everything I try either labels all facets the same or doesn't place the superscripts. I'm pretty new at ggplot2 so am unsure if what I'm trying will help.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
emjbar
  • 73
  • 1
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Remove code that's not essential to your question (the colors and text size seem to be just noise here) – MrFlick Feb 13 '19 at 19:10

1 Answers1

13

You want labeller = label_parsed. Here's a simple example

mt = mtcars

mt$facets = factor(mt$cyl, labels = c(
    "DO~(mg~L^{-1})", 
    "Temperature~('°C')", 
    "Light~(µmol~m^{-2}~s^{-1})"))

ggplot(mt, aes(mpg,cyl)) +
  geom_point() +
  facet_grid(~facets, labeller = label_parsed)

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111