在R中,一个cowplot网格的共享图例

19

我刚刚使用cowplot包建立了一个网格(用于标记A-D图)。这些图是用ggplot2包制作的:

pfour<-ggplot(four, aes(x=Concentration, y=Percentage, fill=Phenotype)) + 
 geom_bar(stat='identity',color='black') +
 scale_fill_grey(start = .4, end = .9) + 
 theme_bw()+ylab("Distribution") + 
 xlab("Contentration [mg/ml]") + 
 ggtitle("96 hpf") +
 theme(legend.title = element_text(colour="black", size=10, face="bold")) +
 theme(legend.background = element_rect(fill="white",
                                        size=0.5, linetype="solid", 
                                        colour ="black")) +
 scale_x_discrete(limits=c('uninjected','control','0.002', '0.02', '0.2'),
                  labels=c('uninjected\n(n=251)',
                           'control\n(n=248)', 
                           '0.002\n(n=205)', 
                           '0.02\n(n=222)', 
                           '0.2\n(n=203)'))

数据看起来是这样的(4个不同的表格,百分比略有不同,但原理相同):

Concentration,Percentage,Phenotype
uninjected,0.996015936,0
uninjected,0,1
uninjected,0.003984064,2
uninjected,0,3
uninjected,0,4
control,0.995967742,0
control,0.004032258,1
control,0,2
control,0,3
control,0,4
0.002,0.985365854,0
0.002,0.004878049,1
0.002,0.004878049,2
0.002,0,3
0.002,0.004878049,4
0.02,0.981981982,0
0.02,0.004504505,1
0.02,0.004504505,2
0.02,0.004504505,3
0.02,0.004504505,4
0.2,0.985221675,0
0.2,0.004926108,1
0.2,0,2

而它看起来是这样的:

plot

那个的代码是:

plot_grid(ponezoom, ptwozoom,pthreezoom,pfourzoom, align='h', labels=c('A', 'B','C','D'))

我在想是否可能获得一个单一的共享图例来覆盖所有四个图,因为如果它出现4次,它会占用大量的绘图空间。感谢任何帮助。


刚刚编辑过。使用ggplot2绘制图形和cowplot进行网格化。 - Kai Mayer
你应该澄清每列底部是否需要两个图例还是一个图例就足够了。同时,你也应该发布一些数据。 - IRTFM
2个回答

20

这里有一个文章展示了如何实现。

方法是使用隐藏图例的方式来构建你的图形 theme(legend.position="none")。然后从这些对象中提取图例grob。

grobs <- ggplotGrob(pfour)$grobs
legend <- grobs[[which(sapply(grobs, function(x) x$name) == "guide-box")]]

然后将图例作为一个单独的“plot”绘制。 要将图例放置在右侧,您可以执行以下操作:

# build grid without legends
pgrid <- plot_grid(pone, ptwo, pthree, pfour, ncol = 2)
# add legend
p <- plot_grid(pgrid, legend, ncol = 2, rel_widths = c(1, .1))

14
请注意,cowplot 现在有一个 get_legend(p) 函数(在您提供的文档中)。该函数用于获取图例。 - Michael Schubert

7

您可以使用ggpubr包中的ggarrange函数。它有一个逻辑参数common.legend。您只需要将其设置为TRUE即可。在您的情况下,代码块如下:

library(ggpubr)

ggarrange(ponezoom, ptwozoom, pthreezoom, pfourzoom,
align='h', labels=c('A', 'B','C','D'),
common.legend = T)

看一个使用mtcars数据集的例子:
library(tidyverse)
library(ggpubr)

# Create first plot
mtcars %>% 
  ggplot(aes(mpg, hp, color = factor(cyl))) +
  geom_point(size = 2) +
  theme_minimal()-> plot1

# Create second plot
mtcars %>% 
  ggplot(aes(disp, drat, color = factor(cyl))) +
  geom_point(size = 2) +
  theme_minimal() -> plot2

# Create grid
ggpubr::ggarrange(plot1, plot2, # list of plots
                  labels = "AUTO", # labels
                  common.legend = T, # COMMON LEGEND
                  legend = "bottom", # legend position
                  align = "hv", # Align them both, horizontal and vertical
                  nrow = 2)  # number of rows

看这里:

在此输入图片描述


或者您可以查看 patchwork 包 https://patchwork.data-imaginist.com - atsyplenkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接