Need help making multiple superscripts in gplot legend title

Need help making the superscripts for grams of carbon per meter squared per year (g C m[1] yr[2]).

pp_plot <- gplot(ras.stack[[5]])+
geom_raster(aes(x=x,y=y,fill=value)) +
scale_fill_viridis_c(na.value="grey95", name = "Primary production (g C m[3] yr[4])") +
theme_bw()+
labs(tag = "D") +
theme(legend.position = "bottom",
legend.key.size = unit(0.5, 'cm'),
legend.key.height = unit(0.5, 'cm'),
legend.key.width = unit(0.5, 'cm'))


  1. −2 ↩︎

  2. −1 ↩︎

  3. −2 ↩︎

  4. −1 ↩︎

Hi @kaito,
Welcome to the RStudio Community Forum.
Try this:

x_name <- c(expression(paste( "Primary production (g C m"^{"-2"},".yr"^{"−1"},")"))) 
plot(1,1, xlab=x_name)

Created on 2022-09-11 with reprex v2.0.2

Getting this to work correctly can be quite tricky. See help(plotmath).
I discovered that I needed to quote the "-2" and "-1" to get this example to work as required.

Great that worked. Thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.