共享图例3x3 ggplots [英] Shared legend 3x3 ggplots

查看:116
本文介绍了共享图例3x3 ggplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有共享图例的3x3 ggplot. 在多个图例之间共享图例时控制图的布局ggplot2图形问题和答案解决了1x4绘图(不是我需要的3x3)的问题.我尝试根据自己的需要修改该功能,但经过多次尝试,我必须承认浸信会的功能远远超出了我的R知识.

I need a 3x3 ggplot with a shared legend. The Controlling the plot layout when sharing legends between several ggplot2 graphs question and answers solve this for 1x4 plot (not 3x3 which is what I need). I have tried to modify the function for my needs, after many attempts, I must admit that baptistes function is much beyond my R-knowledge.

这是一个MWE,基于所提到问题中的相同示例(希望可以借用它).

Here is a MWE, based on the same example in the refered question (hope it is ok to borrow it).

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

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(cut, price, data=dsamp, colour=clarity)
p3 <- qplot(color, price, data=dsamp, colour=clarity)
p4 <- qplot(depth, price, data=dsamp, colour=clarity)
p5 <- qplot(carat, price, data=dsamp, colour=clarity)
p6 <- qplot(cut, price, data=dsamp, colour=clarity)
p7 <- qplot(color, price, data=dsamp, colour=clarity)
p8 <- qplot(depth, price, data=dsamp, colour=clarity)
p9 <- qplot(carat, price, data=dsamp, colour=clarity)

grid_arrange_shared_legend <- function(..., layout = rbind(c(1,2,3,4), 
                                                           c(5,5,5,5))) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  grid.arrange(grobs = c(gl, list(legend)), layout_matrix = layout,
               heights = grid::unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(p1, p2, p3, p4)  # This works
grid_arrange_shared_legend(p1, p2, p3, p4, p5, p6, p7, p8, p9)  # This is what I need

这是我需要处理的示例的最后一行.

It is the last line in the example I need working.

推荐答案

所提供的示例代码仅适用于单行绘图.您可以对其进行更改以使其更加舒适:

Looks like the sample code provided will only work with a single row of plots. you can change it to be more accomidating:

grid_arrange_shared_legend <- function(..., layout = rbind(c(1,2,3,4), 
                                                           c(5,5,5,5))) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  grid.arrange(grobs = c(gl, list(legend)), layout_matrix = layout,
               heights = grid::unit.c(rep((unit(1, "npc") - lheight)*(1/(nrow(layout)-1)),nrow(layout)-1), lheight))
} 

grid_arrange_shared_legend(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout=rbind(1:3, 4:6, 7:9, rep(10,3)))

这篇关于共享图例3x3 ggplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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