ggplot2-如何使用grid.arrange为多个图添加唯一图例? [英] ggplot2 - How to add unique legend for multiple plots with grid.arrange?

查看:152
本文介绍了ggplot2-如何使用grid.arrange为多个图添加唯一图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,其中用 ggplot2 grid.arrange 制作了4个面板.每个面板都有一个图例,所有图例都相同.

如何删除图例的底部中的4个图例并创建一个唯一的图例?

这是我的示例数据和图表:

  set.seed(100)df_1 = data.frame(lat = rnorm(20),lon = rnorm(20),cor = c(rep('positive',7),rep('negative',13)),符号= c(rep(99,5),rep(95,6),rep(90,9)))lst_df =列表(df_1,df_1,df_1,df_1)库(ggplot2)图书馆(gridExtra)图书馆(网格)对于(i in 1:length(lst_df)){p [[i]] = ggplot()+geom_point(data = lst_df [[i]],aes(x = lon,y = lat,size = sign,color = cor),alpha = 0.5)+scale_color_manual(values = c("blue","orange"),name ='col',标签= c('neg','pos'),向导= guide_legend(override.aes =列表(alpha = 1,大小= 3)))+scale_size(范围= c(1,3),休息时间= c(90,95,99),标签= c(1,5,10),名称=测试",guide = guide_legend(override.aes = list(colour ='black',alpha = 1)))}grid.arrange(p [[1]],p [[2]],p [[3]],p [[4]],ncol = 2,nrow = 2,top = textGrob(expression(bold("test")),gp = gpar(fontsize = 25,face ='bold'))) 

有什么建议吗?谢谢

解决方案

这是带有 cowplot 的工作流,它提供了一些精巧的功能,可将杂项组合在一起并提取图例等元素.他们有一个(v0.2.0)创建于2018-09-11.

I have a plot with 4 panels inside made with ggplot2 and grid.arrange. Each of the panel has a legend which is the same for all of them.

How can I remove the 4 legends and create a unique one at the bottom of the plot?

Here my sample data and plot:

set.seed(100)
df_1 = data.frame(lat = rnorm(20), 
                  lon = rnorm(20), 
                  cor = c(rep('positive', 7), rep('negative', 13)), 
                  sign = c(rep(99, 5), rep(95, 6), rep(90,9)))

lst_df = list(df_1, df_1, df_1, df_1)


library(ggplot2)
library(gridExtra)
library(grid)

for (i in 1:length(lst_df)) {
p[[i]] = ggplot() +

    geom_point(data=lst_df[[i]], aes(x=lon, y=lat, size=sign, colour = cor), alpha = 0.5) +

    scale_color_manual(values=c("blue", "orange"),
                       name='col', 
                       labels = c('neg', 'pos'),
                       guide = guide_legend(override.aes = list(alpha = 1, size = 3))) +

    scale_size(range = c(1,3), 
               breaks = c(90, 95, 99),
               labels = c(1, 5, 10),
               name = 'test',
               guide = guide_legend(override.aes = list(colour = 'black', 
                                                        alpha = 1)))
}


grid.arrange(p[[1]], p[[2]], p[[3]], p[[4]], 
             ncol=2, nrow=2,
             top=textGrob(expression(bold("test")), gp=gpar(fontsize=25, face= 'bold')))

Any suggestion? Thanks

解决方案

Here's a workflow with cowplot, which provides some neat functions for putting grobs together and extracting elements like legends. They have a detailed vignette on creating grids of plots with shared legends like you're looking for. Similarly, the vignette on plot annotations goes over cowplot functions for creating and adding labels--they function much like the other plot elements and can be used in cowplot::plot_grid.

The process is basically

  1. Creating a list of plots without legends, using lapply (could instead be a loop)
  2. Extracting one of the legends--doesn't matter which since they're all the same--that's been set to position at the bottom
  3. Creating a text grob for the title
  4. Creating the grid from the list of legendless plots
  5. Creating a grid from the title, the legendless plots grid, and the legend

As an aside, loading cowplot lets it set its default ggplot theme, which I don't particularly like, so I use cowplot::function notation instead of library(cowplot).

You can tweak the relative heights used to make the final grid--this was the first ratio that worked well for me.

Should it come up, I posted a question a few months ago on making the draw_label grob take theme guidelines like you would expect in normal ggplot elements like titles; answers from the package author and my specialty function are here.

library(ggplot2)
...

p_no_legend <- lapply(p, function(x) x + theme(legend.position = "none"))
legend <- cowplot::get_legend(p[[1]] + theme(legend.position = "bottom"))

title <- cowplot::ggdraw() + cowplot::draw_label("test", fontface = "bold")

p_grid <- cowplot::plot_grid(plotlist = p_no_legend, ncol = 2)
cowplot::plot_grid(title, p_grid, legend, ncol = 1, rel_heights = c(0.1, 1, 0.2))

Created on 2018-09-11 by the reprex package (v0.2.0).

这篇关于ggplot2-如何使用grid.arrange为多个图添加唯一图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆