繁体   English   中英

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

[英]Adjust legends for each individual graph on ggarrange in R

我有三个geom_point图,绘制了与ggarrange排列在一起的 3 个不同变量。 最终输出显示了相互叠加的图例。 当尝试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奇妙的分组功能,然后就不需要plot组合包了。

您可以按审美和/或方面进行分组。 见下文。 关键是让你的数据变长,另见下文。 看看它如何也大大减少了你的代码。

我还介绍了如何将颜色和大小组合成一个图例。 看到这个线程

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 包(v0.3.0) 于 2020 年 3 月 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM