简体   繁体   中英

Labelling plots arranged with grid.arrange

I have attached multiple plots to one page using grid.arrange.

Is there a way to label each plot with "(a)","(b)" etc...

I have tried using geom_text but it does not seem compatible with my plots....

我的阴谋

.... as you can see, geom_text has some strange interaction with my legend symbols.

I will show an example using the mtcars data of what I am trying to achieve. THe alternative to geom_text I have found is "annotate" which does not interact with my legend symbols. However, it is not easy to label only one facet....

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)

mtcars 示例

Therefore, my question is, is there a way to label plots arranged using grid.arrange, so that the first facet in each plot is labelled with either a, or b or c etc...?

You can use ggarrange from ggpubr package and set labels for each plot using the argument 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)"))

在此处输入图片说明

Is it what you are looking for ?

If you set inherit.aes=FALSE, you can prevent it from interring:

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)

在此处输入图片说明

If you want to only label the first facet (hope I got you correct), I think the easiest way to specify a data frame, eg if we want only something in the first,

#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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM