How to use superscripts in cowplot multipanel plot labels

418 Views Asked by At

How can I use cowplot to add labels to plots in multiplot panels, where the labels have superscripts?

I can use superscripts on the axis titles with substitute, bquote or expression, like this:

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(disp, mpg)) +
  geom_point()
p2 <- ggplot(mtcars, aes(disp, hp)) +
  geom_point() +
  # this will create a nice superscript
  xlab(bquote(A^x))

p2

enter image description here

But I don't know how to give a set of those labels to cowplot to label multiple plots:


# create a vector of labels for the grid of plots
labels_with_superscript <- 
  c(bquote(A^x), 
    bquote(B^x))

# create the grid of plots? 
plot_grid(p1, p2, 
          ncol = 1, 
          align = "v",
          labels =  labels_with_superscript
)

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : object 'A' not found

What's the secret to getting that to work so I can get something like Ax and Bx as the plot labels instead of A and B in the plot below?

plot_grid(p1, p2, 
          ncol = 1, 
          align = "v",
          labels =  "AUTO"
)

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

You can do this using a plotmath expression. @user63230 has the right idea in the comments, however, you can't pass the parse argument to the plot_grid() function but you can use draw_plot_label() afterwards although you also need to specify the label positions:

library(ggplot2)
library(cowplot)

labels_with_superscript <-  c("A^x", "B^x")

plot_grid(p1, p2, 
          ncol = 1, 
          align = "v") +
  draw_plot_label(labels_with_superscript, x = c(0, 0), y = c(1, .5), parse = TRUE)

enter image description here