关于 r:网格图的常见图例

Common legend for a grid plot

本问题已经有最佳答案,请猛点这里访问。

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

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

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#---------------------
# Reproducible example
#---------------------
library(tidyverse)
library(ggplot2)
library(grid)
library(gridExtra)
d0 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d1 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d2 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d3 <- read_csv("x, y, col\
a,2,z\
b,2,z\
c,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)

终于到了让我的 ggplot2 包大放异彩的时候了!

使用 lemon 包中的 grid_arrange_shared_legend (https://cran.r-project.org/package=lemon)。在使用传奇小插图中有一个示例。

结果可能是这样的:
Example

但是...它不适用于您的示例,所以我更新了包。您需要从 github 安装开发版本:

1
2
library(devtools)
install_github('stefanedwards/lemon', ref='e05337a')

这会给你以下

1
2
3
4
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)

enter


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
library(tidyverse)
library(ggplot2)
library(grid)
library(gridExtra)
library(gtable)
d0 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d1 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d2 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d3 <- read_csv("x, y, col\
a,2,z\
b,2,z\
c,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)

enter

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

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
d0 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d1 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d2 <- read_csv("x, y, col\
a,2,x\
b,2,y\
c,1,z")
d3 <- read_csv("x, y, col\
a,2,z\
b,2,z\
c,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)