简体   繁体   中英

Adjust legends for each individual graph on ggarrange in R

I have three geom_point graphs plotting 3 different variables arranged together with ggarrange . The final output shows the legends superponed on top of each other. When trying common_legend = TRUE it only shows the legend of the first one. Would it be possible to arrange the legends so I have the three colours scales for each graph (on the right), and then the variable name somewhere on each graph as well.

Here is a reproducible example: Dataset:

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)

And this is the code used to create individual data frames and plotting with the three merged 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")

Output: 在此处输入图片说明

We can add the legend.box = "horizontal and set a right order to the legend on each ggplot.

Then, add width and align = "v" on ggarrange

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"
          )

在此处输入图片说明

You're making your own life quite difficult with this approach. Use ggplot's fantastic grouping features, and then there is no need for a plot combining package.

You can group by aesthetic and/or facet. See below. Key is to make your data long, see also below. See how it also drastically reduces your code.

I also included how to combine both color and size into one legend. see this thread

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).

Created on 2020-03-30 by the reprex package (v0.3.0)

data

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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