Using superscript when changing labels in facet_wrap()

465 Views Asked by At

I am using facet_wrap() to plot 7 air quality pollutants on 1 page. I want to rename the individual plot labels from the abbreviation used in my dataframe (ex. "PM") to the long form (ex. "Fine Particulate Matter (ug/m3)") and want to include a superscript in one of the labels (ug/m^3). I can't find a way to integrate the superscript when changing the labels.

My code without the superscripts works fine:

> SeasonalMean
    Pollutant Season       mean        cv
 1:       SO2 Winter  4.3594786 1.2606103
 2:        NO Winter  4.8086810 1.3138420
 3:       NO2 Winter 13.4610992 0.4983027
 4:       NOX Winter 18.2377363 0.6503697
 5:        CO Winter  0.2578629 0.3373904
 6:        O3 Winter 25.0978910 0.4094255
 7:        PM Winter  9.4271302 0.5599974
 8:       NOX Spring 14.1144831 0.6320194
 9:        CO Spring  0.2198282 0.2964914
10:        O3 Spring 26.2944466 0.3021818
11:        PM Spring  7.7559475 0.5107155
12:       SO2 Spring  2.7709979 1.5018185
13:        NO Spring  3.3221394 1.4227275
14:       NO2 Spring 10.7621779 0.4771676
15:        CO Summer  0.2332291 0.3337901
16:        O3 Summer 25.8531689 0.3426781
17:        PM Summer  8.5609107 0.5751629
18:       SO2 Summer  3.2485138 1.3612882
19:        NO Summer  4.3011274 1.6905351
20:       NO2 Summer 11.4917325 0.4739557
21:       NOX Summer 15.8515746 0.7333461
22:        O3   Fall 25.3569794 0.3529466
23:        PM   Fall  8.0808294 0.5460776
24:       SO2   Fall  3.7543985 1.3780758
25:        NO   Fall  3.1074317 1.1202237
26:       NO2   Fall 11.8911633 0.4549728
27:       NOX   Fall 15.0387167 0.5627663
28:        CO   Fall  0.2378491 0.2780384

new_labels <- c("CO" = "Carbon Monoxide (ppb)", "NO" = "Nitric Oxide (ppb)", "NO2" = "Nitrogen Dioxide (ppb)", "NOX" = "Nitrogen Oxides (ppb)", "O3" = "Ground-Level Ozone (ppb)", "SO2" = "Sulphur Dioxide (ppb)", "PM" = "Fine Particulate Matter (ug/m^3)")

p1 <- ggplot(SeasonalMean, aes(x = Season, y = mean)) +
  facet_wrap(~ Pollutant, scales = "free", labeller = labeller(Pollutant = new_labels)) +
  geom_point() +
  labs(title = "Average Seasonal Air Pollutant Concentrations in Hamilton Downtown (2017)", x = "Season", y = "Mean") +
  theme(axis.text.x = element_text(angle = 90, vjust=0.5), plot.title = element_text(hjust = 0.5))

I looked at some other questions but couldnt find any that combined changing facet labels while also using superscript: How do I include a superscript to texts on a plot on R?, Superscript in R, Subscripts in plots in R, How to change facet labels?

My attempts have included using expression() and bquote() when making the list of label names:

new_labels <- c("CO" = "Carbon Monoxide (ppb)", "NO" = "Nitric Oxide (ppb)", "NO2" = "Nitrogen Dioxide (ppb)", "NOX" = "Nitrogen Oxides (ppb)", "O3" = "Ground-Level Ozone (ppb)", "SO2" = "Sulphur Dioxide (ppb)", "PM" = expression("Fine Particulate Matter (ug/m^3)"))

but this just makes the whole list an expression:

> new_labels
expression(CO = "Carbon Monoxide (ppb)", NO = "Nitric Oxide (ppb)", 
    NO2 = "Nitrogen Dioxide (ppb)", NOX = "Nitrogen Oxides (ppb)", 
    O3 = "Ground-Level Ozone (ppb)", SO2 = "Sulphur Dioxide (ppb)", 
    PM = "Fine Particulate Matter (ug/m^3)")

and I also tried using expression() and bquote() within the labeller() parameter of facet_wrap(), but this results in none of the plot labels changing:

p1 <- ggplot(SeasonalMean, aes(x = Season, y = mean)) +
  facet_wrap(~ Pollutant, scales = "free", labeller = labeller(Pollutant = expression(new_labels))) +
  geom_point() +
  labs(title = "Average Seasonal Air Pollutant Concentrations in Hamilton Downtown (2017)", x = "Season", y = "Mean") +
  theme(axis.text.x = element_text(angle = 90, vjust=0.5), plot.title = element_text(hjust = 0.5))

Sorry I'm not able to include any pictures of the plots, thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

Try this tidyverse solution with ggtext. This would imply having the labels in a dataframe similar as those in the vector. Then joining them and using the new added variable which contains the labels inside the facet option. After that you can format the strip text to be of the class element_markdown(). Here the code:

library(tidyverse)
library(ggtext)
#Labels
Datalabels <- data.frame(Pollutant=c("CO","NO","NO2","NOX","O3","SO2","PM"),
                         Lab=c("Carbon Monoxide (ppb)","Nitric Oxide (ppb)","Nitrogen Dioxide (ppb)",
                               "Nitrogen Oxides (ppb)","Ground-Level Ozone (ppb)","Sulphur Dioxide (ppb)",
                               "Fine Particulate Matter (ug/m^3)"),stringsAsFactors = F)
#Plot
SeasonalMean %>% left_join(Datalabels) %>%
  ggplot(aes(x = Season, y = mean)) +
  facet_wrap(~ Lab, scales = "free") +
  geom_point() +
  labs(title = "Average Seasonal Air Pollutant Concentrations in Hamilton Downtown (2017)",
       x = "Season", y = "Mean") +
  theme(axis.text.x = element_text(angle = 90, vjust=0.5),
        plot.title = element_text(hjust = 0.5),
        strip.text = element_markdown())

Output:

enter image description here

Some data used:

#Data
SeasonalMean <- structure(list(Pollutant = c("SO2", "NO", "NO2", "NOX", "CO", 
"O3", "PM", "NOX", "CO", "O3", "PM", "SO2", "NO", "NO2", "CO", 
"O3", "PM", "SO2", "NO", "NO2", "NOX", "O3", "PM", "SO2", "NO", 
"NO2", "NOX", "CO"), Season = c("Winter", "Winter", "Winter", 
"Winter", "Winter", "Winter", "Winter", "Spring", "Spring", "Spring", 
"Spring", "Spring", "Spring", "Spring", "Summer", "Summer", "Summer", 
"Summer", "Summer", "Summer", "Summer", "Fall", "Fall", "Fall", 
"Fall", "Fall", "Fall", "Fall"), mean = c(4.3594786, 4.808681, 
13.4610992, 18.2377363, 0.2578629, 25.097891, 9.4271302, 14.1144831, 
0.2198282, 26.2944466, 7.7559475, 2.7709979, 3.3221394, 10.7621779, 
0.2332291, 25.8531689, 8.5609107, 3.2485138, 4.3011274, 11.4917325, 
15.8515746, 25.3569794, 8.0808294, 3.7543985, 3.1074317, 11.8911633, 
15.0387167, 0.2378491), cv = c(1.2606103, 1.313842, 0.4983027, 
0.6503697, 0.3373904, 0.4094255, 0.5599974, 0.6320194, 0.2964914, 
0.3021818, 0.5107155, 1.5018185, 1.4227275, 0.4771676, 0.3337901, 
0.3426781, 0.5751629, 1.3612882, 1.6905351, 0.4739557, 0.7333461, 
0.3529466, 0.5460776, 1.3780758, 1.1202237, 0.4549728, 0.5627663, 
0.2780384)), class = "data.frame", row.names = c(NA, -28L))