Home > front end >  Adding a common legend to ggarrange plots
Adding a common legend to ggarrange plots

Time:01-22

I'm trying to produce a grid of ggplots, with each column having a title. I've tried a few approaches, but the best one I've found so far is below. This is the only one which doesn't leave loads of whitespace and also leaves all 4 plots the same size:

library(ggplot2)
data=data.frame(x_1 = c(1:5),y_1 =c(1:5)^2,x_2= c(1:5),y_2 =c(1:5)^3,x_3 = c(1:5),y_3 =c(1:5)^4)

plot1<- ggplot(data) geom_line(aes(x=x_1,y=y_1),col="red") geom_line(aes(x=x_2,y=y_2),col="blue") geom_line(aes(x=x_3,y=y_3),col="green") scale_color_manual(limits = c("a = 2", "a = 3", "a = 4"),values = c("red","blue","green"))

a=ggarrange(plot1, plot1,nrow=2,ncol=1,common.legend = FALSE)
b=ggarrange(plot1,plot1,nrow=2,ncol=1,common.legend = FALSE)
a=annotate_figure(a, top = text_grob("Plots 1",  size = 12))
b=annotate_figure(b, top = text_grob("Plots 2",  size = 12))
c=ggarrange(a, b,nrow=1,ncol=2,common.legend = TRUE)

enter image description here

The only issue is that there is no legend. I want one legend for the whole plot, below.

CodePudding user response:

First of all, if you want to have a legend in ggplot you should mention the variable you want to show in your aes like color. For the plots with a title you should create a separate plot but then without plotting the legend using show.legend = FALSE in each geom. Then you can combine them in one ggarrange like this:

library(ggplot2)
library(ggpubr)
data=data.frame(x_1 = c(1:5),y_1 =c(1:5)^2,x_2= c(1:5),y_2 =c(1:5)^3,x_3 = c(1:5),y_3 =c(1:5)^4)

# Normal plot
plot1<- ggplot(data) 
  geom_line(aes(x=x_1,y=y_1,col="a = 2")) 
  geom_line(aes(x=x_2,y=y_2,col="a = 3")) 
  geom_line(aes(x=x_3,y=y_3,col="a = 4")) 
  scale_color_manual(limits = c("a = 2", "a = 3", "a = 4"),
                     breaks = c("a = 2", "a = 3", "a = 4"),
                     values = c("a = 2"="red","a = 3"="blue","a = 4"="green"))

# Plot without legend
plot2<- ggplot(data) 
  geom_line(aes(x=x_1,y=y_1,col="a = 2"), show.legend = FALSE) 
  geom_line(aes(x=x_2,y=y_2,col="a = 3"), show.legend = FALSE) 
  geom_line(aes(x=x_3,y=y_3,col="a = 4"), show.legend = FALSE) 
  scale_color_manual(limits = c("a = 2", "a = 3", "a = 4"),
                     breaks = c("a = 2", "a = 3", "a = 4"),
                     values = c("a = 2"="red","a = 3"="blue","a = 4"="green"))

# Plot without legend add title
plot2_with_annotate1 = annotate_figure(plot2, top = text_grob("Plots 1",  size = 12))
plot2_with_annotate2 = annotate_figure(plot2, top = text_grob("Plots 2",  size = 12))

ggarrange(plot2_with_annotate1, 
          plot2_with_annotate2, 
          plot1, plot1,
          nrow=2,ncol=2,common.legend = TRUE, legend = "bottom")

Created on 2023-01-21 with reprex v2.0.2

  • Related