1

I have 4 sets of data that I am plotting as a scatter plot using ggplot2 in R. Each plot contains an array of points that are both colored and sized according to different variables in the dataset. Each of the 4 sets of data that I would like to plot contain the same variables, but different maximum values.

I would like to plot my data in 4 separate plots arranged into one figure using Patchwork. With patchwork, I would like to use the command plot_layout(guides = 'collect')to collect all 4 color and size figure legends into one legend on the side.

Currently this is not possible, because each plot has a different maximum scale. How can I make this possible?

My first thought is to manually change the upper bound of the size value and color value so that they are the same across each plot? (I am aware that a solution such as this would also change the color and size of the points within each plot to scale relative to the new maximum outside the bounds of the dataset) Is it possible to manually change size and color scales while still having the map to the specific variables of interest?

ggplot(Enrichment_Analysis, aes(x = log2(enrichmentRatio), y = -log10(FDR))) +
    geom_point(aes(size = size, color = overlap))+
    scale_x_continuous(name = expression("log"[2]*"(Enrichment Ratio)"))+
    scale_y_continuous(name = expression("-log"[10]*"(FDR)"))

For each of my 4 data sets the maximum value for size ranges between 300 and 400, and the maximum value for overlap ranges between 30 and 60. I would like to set the size scale for every plot to be 0-400, and the color scale for every plot to be 0-60

eyesack
  • 13
  • 2
  • Hi eyesack! Welcome to StackOverflow! I don't know if I follow what you want. Are you looking for a way of making sure individual points keep the same colour across multiple plots? Is it that you only want one larger legend? – Mark Aug 02 '23 at 03:38
  • Welcome to Stack Overflow. You'll get better answers if you [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(Enrichment_Analysis)`, if that is not too large. – neilfws Aug 02 '23 at 03:38

1 Answers1

0

The 'collect guides' function from the patchwork package removes exact duplicates, so the only approach that I can think of is to manually set the limits for that scale for each of your plots, e.g.

library(tidyverse)

# example data
df1 <- data.frame(y = runif(min = 0, max = 10, n = 10),
                  x = 1:10,
                  colour = c("A", "B"))
df2 <- data.frame(y = runif(min = 10, max = 20, n = 10),
                  x = 1:10,
                  colour = c("A", "B"))
df3 <- data.frame(y = runif(min = 20, max = 30, n = 10),
                  x = 1:10,
                  colour = c("A", "B"))
df4 <- data.frame(y = runif(min = 30, max = 40, n = 10),
                  x = 1:10,
                  colour = c("A", "B"))

# plots
p1 <- ggplot(df1, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21)
p2 <- ggplot(df2, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21)
p3 <- ggplot(df3, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21)
p4 <- ggplot(df4, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21)
patchwork::wrap_plots(p1, p2, p3, p4, guides = "collect")

# plots with legends collated
p5 <- ggplot(df1, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21) +
  scale_fill_continuous(limits = c(0, 100)) +
  scale_size_continuous(limits = c(0, 100))
p6 <- ggplot(df2, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21) +
  scale_fill_continuous(limits = c(0, 100)) +
  scale_size_continuous(limits = c(0, 100))
p7 <- ggplot(df3, aes(x = x, y = y, color = colour,
                      fill = y, size = y)) +
  geom_point(shape = 21) +
  scale_fill_continuous(limits = c(0, 100)) +
  scale_size_continuous(limits = c(0, 100))
p8 <- ggplot(df4, aes(x = x, y = y, color = colour, 
                      fill = y, size = y)) +
  geom_point(shape = 21) +
  scale_fill_continuous(limits = c(0, 100)) +
  scale_size_continuous(limits = c(0, 100))
patchwork::wrap_plots(p5, p6, p7, p8, guides = "collect")

Created on 2023-08-02 with reprex v2.0.2

Sorry this isn't what you're hoping for, but as far as I know this is the only way to collate the legends into one.

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • 1
    This is the suggestion I was looking for. Apologies if i was not clear in my explanation. I was not aware that you could use the scale_***_continuous commands to manually set the limits. – eyesack Aug 02 '23 at 14:49
  • Oh! Fantastic - glad you solved your problem @eyesack – jared_mamrot Aug 02 '23 at 21:32