0 votes
1 view
in R Programming by (5.3k points)

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seems to command that is now updated and named something else.

My data looks like this;

# Data plot 1                                   

        axis1     axis2   

group1 -0.212201  0.358867

group2 -0.279756 -0.126194

group3  0.186860 -0.203273

group4  0.417117 -0.002592

group1 -0.212201  0.358867

group2 -0.279756 -0.126194

group3  0.186860 -0.203273

group4  0.186860 -0.203273

# Data plot 2   

        axis1     axis2

group1  0.211826 -0.306214

group2 -0.072626  0.104988

group3 -0.072626  0.104988

group4 -0.072626  0.104988

group1  0.211826 -0.306214

group2 -0.072626  0.104988

group3 -0.072626  0.104988

group4 -0.072626  0.104988

#And I run this:

library(ggplot2)

library(gridExtra)

groups=c('group1','group2','group3','group4','group1','group2','group3','group4')

x1=data1[,1]

y1=data1[,2]

x2=data2[,1]

y2=data2[,2]

p1=ggplot(data1, aes(x=x1, y=y1,colour=groups)) + geom_point(position=position_jitter(w=0.04,h=0.02),size=1.8)

p2=ggplot(data2, aes(x=x2, y=y2,colour=groups)) + geom_point(position=position_jitter(w=0.04,h=0.02),size=1.8)

#Combine plots

p3=grid.arrange(

p1 + theme(legend.position="none"), p2+ theme(legend.position="none"), nrow=1, widths = unit(c(10.,10), "cm"), heights = unit(rep(8, 1), "cm")))

How would I extract the legend from any of these plots and add it to the bottom/center of the combined plot?

1 Answer

0 votes
by (25.4k points)

To add a common legend for the combined plots, you can do the following:

library(ggplot2)

library(gridExtra)

library(grid)

groups=c('group1','group2','group3','group4','group1','group2','group3','group4')

df1 <- read.table(text="group   x     y   

group1 -0.212201  0.358867

group2 -0.279756 -0.126194

group3  0.186860 -0.203273

group4  0.417117 -0.002592

group1 -0.212201  0.358867

group2 -0.279756 -0.126194

group3  0.186860 -0.203273

group4  0.186860 -0.203273",header=TRUE)

df2 <- read.table(text="group   x     y   

group1  0.211826 -0.306214

group2 -0.072626  0.104988

group3 -0.072626  0.104988

group4 -0.072626  0.104988

group1  0.211826 -0.306214

group2 -0.072626  0.104988

group3 -0.072626  0.104988

group4 -0.072626  0.104988",header=TRUE)

To create a Function to extract the shared legend, and arrange the plots:

shared_legend <- function(...) {

  

  plots <- list(...)

  

  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs

  

  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]

  

  lheight <- sum(legend$height)

  

  grid.arrange(

    do.call(arrangeGrob, lapply(plots, function(x)

      x + theme(legend.position="none"))),

    legend,

    ncol = 1,

    heights = unit.c(unit(1, "npc") - lheight, lheight))

}

To create the plots:

p1 <- ggplot(df1, aes(x=x, y=y,colour=group)) + geom_point(position=position_jitter(w=0.04,h=0.02),size=1.8) + theme(legend.position="bottom")

p2 <- ggplot(df2, aes(x=x, y=y,colour=group)) + geom_point(position=position_jitter(w=0.04,h=0.02),size=1.8)

Plotting p1 and p2 with a common legend:

shared_legend(p1, p2)

Output:

image

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...