Common legend for a grid plot
在这个可重现的示例网格图中,3 个图有 3 种填充颜色,z 显示为蓝色的”col”,但在第四个图中只有 1 个”col”,因此 z 显示为红色。
我只想显示一个常见的图例(我可以这样做),但我希望 z 在所有四个图中都是蓝色的。有没有简单的方法可以做到这一点?
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)。在使用传奇小插图中有一个示例。
结果可能是这样的:
但是…它不适用于您的示例,所以我更新了包。您需要从 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) |
- 谢谢你。这个包看起来很有趣,有时间我会试试的。作为示例的短期解决方案,我使用了 scale_fill_manual。
这可以使用 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 %>% d1 %>% d2 %>% d3 %>% legend = gtable_filter(ggplot_gtable(ggplot_build(p1)),”guide-box”) grid.arrange(p0 + theme(legend.position=”none”), |
另一种方法是在每个图中使用 scale_fill_manual 而不改变因子水平。
示例:
1
|
p0 + scale_fill_manual(values = c(“x” =”red”,”z” =”black”,”y” =”green”))
|
因此提取了您的原始数据和图例:
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”), |
- 谢谢你的好主意。目前,我使用 scale_fill_manual 作为我的代码,就像示例一样,只有 3 个图例值。当 In 有更多时间时,我会探索其他选项。
- 这让我发疯,为什么传说不完全集中在下一行?
- @wolfsatthedoor 这是一个相当过时的答案,有许多软件包可用于获取共享图例 – 请在此处查看答案:stackoverflow.com/questions/13649473/…
来源:https://www.codenong.com/46238676/