如何在 cowplot 多面板绘图标签中使用上标

How to use superscripts in cowplot multipanel plot labels

如何使用 cowplot 为多图面板中的图添加标签,其中标签有上标?

我可以在带有 substitutebquoteexpression 的轴标题上使用上标,如下所示:

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

但我不知道如何将一组这些标签提供给 cowplot 以标记多个地块:


# 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

让它工作的秘诀是什么,所以我可以得到像 Ax 和 Bx 这样的东西作为绘图标签而不是下图中的 A 和 B?

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

您可以使用 plotmath 表达式来完成此操作。 @user63230 在评论中有正确的想法,但是,您不能将 parse 参数传递给 plot_grid() 函数,但您可以在之后使用 draw_plot_label() 尽管您还需要指定标签职位:

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)