网格图的常见图例

Common legend for a grid plot

在这个可重现的示例网格图中,3 个图有 3 种填充颜色,z 显示为 "col" 蓝色,但在第四个图中只有 1 个 "col",因此 z 显示为红色.

我只想显示一个常见的图例(我可以做到),但我希望 z 在所有四个图中都是蓝色的。。有简单的方法吗?

#---------------------
# Reproducible example
#---------------------
library(tidyverse)
library(ggplot2)
library(grid)
library(gridExtra)
d0 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d1 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d2 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d3 <- read_csv("x, y, col\na,2,z\nb,2,z\nc,1,z")
p0 <- ggplot(d0) + geom_col(mapping = aes(x, y, fill = col))
p1 <- ggplot(d1) + geom_col(mapping = aes(x, y, fill = col))
p2 <- ggplot(d2) + geom_col(mapping = aes(x, y, fill = col))
p3 <- ggplot(d3) + geom_col(mapping = aes(x, y, fill = col))
grid.arrange(p0, arrangeGrob(p1,p2,p3, ncol=3), ncol=1)

这可以使用 gtable 提取图例并反转 col 因子的水平来实现:

library(tidyverse)
library(ggplot2)
library(grid)
library(gridExtra)
library(gtable)
d0 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d1 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d2 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d3 <- read_csv("x, y, col\na,2,z\nb,2,z\nc,1,z")

d0 %>% 
  mutate(col = factor(col, levels = c("z", "y", "x"))) %>% 
  ggplot() + geom_col(mapping = aes(x, y, fill = col)) -> p0

d1 %>%
  mutate(col = factor(col, levels = c("z", "y", "x"))) %>% 
  ggplot() + geom_col(mapping = aes(x, y, fill = col))+
  theme(legend.position="bottom") -> p1

d2 %>%
  mutate(col = factor(col, levels = c("z", "y", "x"))) %>% 
  ggplot() + geom_col(mapping = aes(x, y, fill = col)) -> p2

d3 %>%
  ggplot() + geom_col(mapping = aes(x, y, fill = col)) -> p3

legend = gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box")

grid.arrange(p0 + theme(legend.position="none"), 
             arrangeGrob(p1 + theme(legend.position="none"),
                         p2 + theme(legend.position="none"), 
                         p3 + theme(legend.position="none"),
                         nrow = 1),
             legend,
             heights=c(1.1, 1.1, 0.1),
             nrow = 3)

另一种方法是在每个图中使用 scale_fill_manual 而不改变因子水平。

示例:

p0 + scale_fill_manual(values = c("x" = "red", "z" = "black", "y" = "green"))

因此提取了您的原始数据和图例:

d0 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d1 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d2 <- read_csv("x, y, col\na,2,x\nb,2,y\nc,1,z")
d3 <- read_csv("x, y, col\na,2,z\nb,2,z\nc,1,z")
p0 <- ggplot(d0) + geom_col(mapping = aes(x, y, fill = col))
p1 <- ggplot(d1) + geom_col(mapping = aes(x, y, fill = col))
p2 <- ggplot(d2) + geom_col(mapping = aes(x, y, fill = col))
p3 <- ggplot(d3) + geom_col(mapping = aes(x, y, fill = col))
legend = gtable_filter(ggplot_gtable(ggplot_build(p1 + theme(legend.position="bottom"))), "guide-box")

grid.arrange(p0 + theme(legend.position="none"), 
             arrangeGrob(p1 + theme(legend.position="none"),
                         p2 + theme(legend.position="none"), 
                         p3 + theme(legend.position="none") +
                           scale_fill_manual(values = c("z" = "#619CFF")),
                         nrow = 1),
             legend,
             heights=c(1.1, 1.1, 0.1),
             nrow = 3)

我的 ggplot2 包终于大放异彩了!

使用包 lemon 中包含的 grid_arrange_shared_legendhttps://cran.r-project.org/package=lemon). There's an example in the Working with legends 插图。

结果可能是这样的:

但是...它不适用于您的示例,所以我更新了程序包。您需要从 github:

安装开发版本
library(devtools)
install_github('stefanedwards/lemon', ref='e05337a')

这会给你以下内容

library(lemon)
# your code to create p0 - p4
nt <- theme(legend.position='none')
grid_arrange_shared_legend(p0, arrangeGrob(p1+nt,p2+nt,p3+nt, ncol=3), ncol=1, nrow=2)