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

标签 r ggplot2 label cowplot

如何使用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

enter image description here

但我不知道如何为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
)

(function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 未找到对象“A”

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

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

enter image description here

最佳答案

您可以使用绘图数学表达式来完成此操作。 @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)

enter image description here

关于r - 如何在cowplot多面板图标签中使用上标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65086663/

相关文章:

r - R : include an XML element not present in all records

r - ggplot 随着时间的推移每个唯一实例的多线图

r - 是否可以仅对facet_wrap 的各个方面进行重新排序,而不对基础因子级别进行重新排序?

java - 标签的舞台叠加 - 标签很大,设置范围对于缩放来说毫无用处

read_excel 不会修剪空格

r - 在基于 lmer 的建模中使用 I(...) 等参数的问题

将 R 中的数据集重新格式化为行作为级别,列作为范围

r - 如何使用 grid.arrange 对齐条形图?

c - 为什么 goto 'label' 会重复 'label:' 之前的语句?

python - 在 python 端动态调整 kivy 标签(和按钮)的大小