用 grid.arrange 排列的标签图

Labelling plots arranged with grid.arrange

我使用 grid.arrange.

将多个绘图附加到一页

有没有办法用“(a)”、“(b)”等标记每个地块...

我试过使用geom_text,但它似乎与我的情节不兼容....

...如您所见,geom_text 与我的图例符号有一些奇怪的交互。

我将展示一个使用 mtcars 数据的示例,说明我正在努力实现的目标。我发现 geom_text 的替代方法是 "annotate",它不会与我的图例符号交互。但是,只标记一个方面并不容易....

q1=ggplot(mtcars, aes(x=mpg, y=wt)) + 
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")

q2=ggplot(mtcars, aes(x=mpg, y=wt,)) + 
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")


geom_text(x=15, y=5,size=8, label="(b)")

gt1 <- ggplotGrob(q1)
gt2 <- ggplotGrob(q2)


grid.arrange(gt1,gt2, ncol=1)

因此,我的问题是,有没有一种方法可以标记使用 grid.arrange 排列的图,以便每个图中的第一个面都标记为 a、b 或 c 等...?

您可以使用 ggpubr 包中的 ggarrange 并使用参数 labels 为每个绘图设置标签:

library(ggplot2)
library(ggpubr)
q1=ggplot(mtcars, aes(x=mpg, y=wt)) + 
  geom_line() +
  geom_point()+
  facet_grid(~cyl)+
  annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")

q2=ggplot(mtcars, aes(x=mpg, y=wt,)) + 
  geom_line() +
  geom_point()+
  facet_grid(~cyl)+
  annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")                                                         

ggarrange(q1,q2, ncol = 1, labels = c("a)","b)"))

是您要找的吗?

如果你设置inherit.aes=FALSE,你可以防止它插入:

ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) + 
geom_line() +
geom_point()+
geom_text(inherit.aes=FALSE,aes(x=15,y=12,label="(a)"),
size=8,family="serif")+
facet_grid(~cyl)

如果你只想标记第一个方面(希望我没弄错),我认为指定数据框的最简单方法,例如,如果我们只想要第一个方面,

#place it in the first
lvl_data = data.frame(
x=15,y=12,label="(a)",
cyl=levels(factor(mtcars$cyl))[1]
)

ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) + 
    geom_line() +
    geom_point()+
    geom_text(data=lvl_data,inherit.aes=FALSE,
aes(x=x,y=y,label=label),size=8,family="serif")+
    facet_grid(~cyl)