共享图例 3x3 ggplots
Shared legend 3x3 ggplots
我需要一个带有共享图例的 3x3 ggplot。 问答解决了 1x4 图(不是我需要的 3x3)的问题。我曾尝试根据需要修改函数,经过多次尝试,我必须承认 baptistes 函数远远超出了我的 R 知识范围。
这是一个 MWE,基于所引用问题中的相同示例(希望可以借用它)。
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
这是我需要处理的示例中的最后一行。
看起来提供的示例代码仅适用于单行绘图。您可以将其更改为更贴心:
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 ggplot。
这是一个 MWE,基于所引用问题中的相同示例(希望可以借用它)。
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
这是我需要处理的示例中的最后一行。
看起来提供的示例代码仅适用于单行绘图。您可以将其更改为更贴心:
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)))