本文介绍了网格图的常见图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this reproducible example grid plot, 3 plots have 3 fill colours, and z displays with the "col" blue, but in the fourth plot there is only 1 "col", so z displays as red.

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

I want to show only one common legend (which I can do), but I want z to be blue in all four plots.. Is there a simple way to do that?

#---------------------
# 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因素的水平来实现:

This can be achieved using gtable to extract the legend and reversing the levels of col factor:

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而不更改因子水平.

Another approach is to use scale_fill_manual in every plot without changing the factor levels.

示例:

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

因此,请提取原始数据和图例:

so with your original data and legend extracted:

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)

这篇关于网格图的常见图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 23:22