Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move the legend position with grid.arrange

I´m trying to arrange 4 plots in a page, placing the legend in the bottom center

I used this to get the legends from one of the plots (as they will be the same for the four plots)

    get_legend<-function(myggplot){
    tmp <- ggplot_gtable(ggplot_build(myggplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
    }

then, I get the legend from any of the plots

   legend <- get_legend(p2)

so for the figure I used:

 tt <-grid.arrange(arrangeGrob(p6, p7, p8, p9, legend,
                          nrow = 2, #
                          left = textGrob("Mammalian species richness", rot = 90, vjust = 1, 
                                          gp = gpar(fontsize = 12))))

but what I´ve got is this:

enter image description here

How can I move the legend to the center button and have 2rows 2 columns? commands nrow, ncol did not work as I received an error message, neither something like

tt  <- tt + theme(legend.position ="bottom"). 

ps. just in case this info is important, if I attempt not to place any legend I could have the four plots in a grid as I want, but without legends, so, you understand is not good for publication.

like image 793
lep Avatar asked Oct 17 '22 14:10

lep


1 Answers

You can use the argument layout_matrix in grid.arrange for more control.

A reproducible example,

library(ggplot2)
library(gridExtra)

get_legend<-function(myggplot){
    tmp <- ggplot_gtable(ggplot_build(myggplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
    }

p <- ggplot()
p2 <- ggplot(mtcars, aes(mpg, wt, col=factor(am))) + geom_point()
legend <- get_legend(p2)


grid.arrange(p, p, p, p, legend, 
             layout_matrix=rbind(c(1,2,3), c(4,5,5)))

Which produces

enter image description here

Or perhaps you want

grid.arrange(p, p ,p, p, legend, 
             layout_matrix=rbind(c(1,1,2,2,5), c(3,3,4,4,5)))

enter image description here

like image 199
user20650 Avatar answered Oct 21 '22 05:10

user20650