调整 R 中 ggarrange 上每个单独图形的图例

Adjust legends for each individual graph on ggarrange in R

我有三个 geom_point 图表,绘制了 3 个不同的变量,这些变量与 ggarrange 排列在一起。最终输出显示了相互叠加的图例。尝试 common_legend = TRUE 时,它只显示第一个的图例。是否可以安排图例,以便我为每个图表(右侧)提供三种颜色比例,然后在每个图表的某处也有变量名称。

这是一个可重现的例子: 数据集:

Samples <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) 
X = c(1.16, 1.16,   0.96,   0.96,   0.96,   0.67,   0.67,   0.67,   0.78,   0.78,   0.55,   0.3,    0.3,    0.3,    0.26,   0.26,   0.26) 
Y = c(75.45, 75.45, 86.66, 86.66, 86.66, 103.36, 103.36, 103.36, NA, NA, 107.53, NA, NA, NA, 128.49, 128.49, 128.49)
AA = c(0.003437318, 0.005842468,    0.005573348,    0.006074338,    0.002537367,    0.006583666,    0.006015314,    0.010983784,    0.009116288,    0.010872489,    0.010924257,    0.009359167,    0.009068434,    0.00601658, 0.017616501,    0.014813675,    0.018048576) 
BB = c(0.007614672, 0.007632451,    0.007066506,    0.007524053,    0.008337992,    0.012520277,    0.012249,   0.011351902,    0.01263021, 0.009969673,    0.008850031,    0.007290232,    0.00724349, 0.007161781,    0.004299581,    0.004896156,    0.005970637) 
CC = c(0.002133046, 0.00168291, 0.001580502,    0.001491037,    0.001295399,    0.001644785,    0.001738881,    0.001496376,    0.00140218, 0.001247361,    0.001364975,    0.001209774,    0.000933038,    0.002034014,    0.000665552,    0.000855588,    0.000878233)

这是用于创建单个数据框并使用三个合并的 gig-ggplots 绘图的代码:

library(ggplot2)
library(ggpubr)
coex1 = data.frame(Samples, X, Y, AA)
coex1 <- data.frame(X,Y,value = c(AA), letters = rep(c("AA"), each = length(AA)))

coex2 = data.frame(Samples, X, Y, BB)
coex2 <- data.frame(X,Y,value = c(BB), letters = rep(c("BB"), each = length(BB)))

coex3 = data.frame(Samples, X, Y, CC)
coex3 <- data.frame(X,Y,value = c(CC), letters = rep(c("CC"), each = length(CC)))


p1 <- ggplot(coex1,aes(x=X,y=Y,shape=letters,col=value, col=value))+geom_jitter(width=0.05) +  scale_color_gradient(low="blue", high="red")
p2 <- ggplot(coex2,aes(x=X,y=Y,shape=letters,col=value, size=value))+geom_jitter(width=0.05) +  scale_color_gradient(low="blue", high="red")
p3 <- ggplot(coex3,aes(x=X,y=Y,shape=letters,col=value, size=value))+geom_jitter(width=0.05) +  scale_color_gradient(low="blue", high="red")


ggarrange(p1, p2, p3, rremove("x.text"), 
          labels = c("a", "b", "c"),
          ncol = 1, nrow = 3, legend = "right")

输出:

我们可以添加 legend.box = "horizontal 并为每个 ggplot 上的图例设置正确的顺序。

然后,在 ggarrange

上添加 widthalign = "v"
p1 <- ggplot(coex1,aes(x=X, y=Y, shape=letters, col=value, col=value)) +
  geom_jitter(width=0.05) +
  scale_color_gradient(low="blue", high="red") +
  theme(legend.box = "horizontal")+
  guides(color = guide_legend(order=1),
         size = guide_legend(order=2),
         shape = guide_legend(order=3))


p2 <- ggplot(coex2,aes(x=X, y=Y, shape=letters, col=value, size=value)) +
  geom_jitter(width=0.05) +
  scale_color_gradient(low="orange", high="yellow") +
  theme(legend.box = "horizontal")+
  guides(color = guide_legend(order=1),
         size = guide_legend(order=2),
         shape = guide_legend(order=3))



p3 <- ggplot(coex3,aes(x=X, y=Y, shape=letters, col=value, size=value)) +
  geom_jitter(width=0.05) +
  scale_color_gradient(low="green", high="cyan") +
  theme(legend.box = "horizontal")+
  guides(color = guide_legend(order=1),
         size = guide_legend(order=2),
         shape = guide_legend(order=3))


ggarrange(p1, p2, p3 + rremove("x.text"), 
          labels = c("a", "b", "c"),
          ncol = 1, nrow = 3,
          legend = "right",
          widths = c(2, 2, 3),
          align = "v"
          )

你用这种方法让自己的生活变得很困难。使用 ggplot 出色的分组功能,然后就不需要绘图组合包了。

您可以按美学 and/or 方面进行分组。见下文。关键是让你的数据变长,另见下文。了解它如何显着减少您的代码。

我还介绍了如何将颜色和尺寸组合成一个图例。

library(tidyverse)

mydat <- data.frame(Samples, X, Y, AA, BB, CC) %>% 
  pivot_longer(names_to = "letters", values_to = "value", cols = AA:CC) %>%
  group_by(letters) %>%
  mutate(scaled_val = scale(value)) %>%
  ungroup()

ggplot(mydat, aes(X, Y, col = scaled_val, size = scaled_val)) +
  geom_jitter(width = 0.05) +
  scale_color_gradient(low = "blue", high = "red") +
  facet_grid(~letters) +
  guides(color=guide_legend(), size = guide_legend())
#> Warning: Removed 15 rows containing missing values (geom_point).

reprex package (v0.3.0)

于 2020-03-30 创建

数据

Samples <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)
X <- c(1.16, 1.16, 0.96, 0.96, 0.96, 0.67, 0.67, 0.67, 0.78, 0.78, 0.55, 0.3, 0.3, 0.3, 0.26, 0.26, 0.26)
Y <- c(75.45, 75.45, 86.66, 86.66, 86.66, 103.36, 103.36, 103.36, NA, NA, 107.53, NA, NA, NA, 128.49, 128.49, 128.49)
AA <- c(0.003437318, 0.005842468, 0.005573348, 0.006074338, 0.002537367, 0.006583666, 0.006015314, 0.010983784, 0.009116288, 0.010872489, 0.010924257, 0.009359167, 0.009068434, 0.00601658, 0.017616501, 0.014813675, 0.018048576)
BB <- c(0.007614672, 0.007632451, 0.007066506, 0.007524053, 0.008337992, 0.012520277, 0.012249, 0.011351902, 0.01263021, 0.009969673, 0.008850031, 0.007290232, 0.00724349, 0.007161781, 0.004299581, 0.004896156, 0.005970637)
CC <- c(0.002133046, 0.00168291, 0.001580502, 0.001491037, 0.001295399, 0.001644785, 0.001738881, 0.001496376, 0.00140218, 0.001247361, 0.001364975, 0.001209774, 0.000933038, 0.002034014, 0.000665552, 0.000855588, 0.000878233)