Milaa
Milaa

Reputation: 419

Need help in layout two graphs in one page using grid arrange

I have two plots I want to display them on one page with common legend, I have tried all possibilities but none of them work. Below are the code and the desired results:

library(ggplot2)
library("rnaturalearthdata")
library("rnaturalearth")

world <- ne_countries(scale = "medium", returnclass = "sf")
# first plot
grd1<- data.frame(lon=rep(seq(12,18,0.25),13), lat=rep(seq(33.5, 36.5, 0.25),25),a= runif(325))
p1<- ggplot(data = world) +
  geom_sf(fill="antiquewhite") +
  coord_sf(xlim = c(12, 18), ylim = c(30, 40), expand = FALSE)+
  geom_raster(data=grd1, aes(x = lon, y = lat, fill = a ),interpolate = FALSE) +
  theme(legend.position="bottom")

# second plot
grd2<- data.frame(lon=rep(seq(12,18,0.25),13), lat=rep(seq(33.5, 36.5, 0.25),25),a= runif(325))
p2<- ggplot(data = world) +
  geom_sf(fill="antiquewhite") +
  coord_sf(xlim = c(12, 18), ylim = c(30, 40), expand = FALSE)+
  geom_raster(data=grd2, aes(x = lon, y = lat, fill = a ),interpolate = FALSE) 


# Get the legend
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)
}
legend<- get_legend(p1)
p1<-p1+ theme(legend.position="none")
p2<-p2+ theme(legend.position="none")

# layout the two plots in one page
grid.arrange(
  grobs=list(p1, p2, legend), ncol=2,nrow=2,
  layout_matrix=rbind(c(1,2),c(3,3)),
  widths=c(4,4)
)

This code yields the following result enter image description here

The desired result should look like this enter image description here

Upvotes: 0

Views: 110

Answers (1)

stefan
stefan

Reputation: 125537

One approach to achieve this would be to make use of patchwork which via plot_layout makes it possible to collect the guides. To make this work we have to set the same limits for the fill scale so that the fill legends are the same (otherwise the scale of your fill legends will be slightly different):

library(ggplot2)
library(patchwork)
library(rnaturalearthdata)
library(rnaturalearth)

world <- ne_countries(scale = "medium", returnclass = "sf")
# first plot
grd1<- data.frame(lon=rep(seq(12,18,0.25),13), lat=rep(seq(33.5, 36.5, 0.25),25), a= runif(325))
p1<- ggplot(data = world) +
  geom_sf(fill="antiquewhite") +
  coord_sf(xlim = c(12, 18), ylim = c(30, 40), expand = FALSE)+
  geom_raster(data=grd1, aes(x = lon, y = lat, fill = a ),interpolate = FALSE) +
  scale_fill_continuous(limits = c(0, 1))
# second plot
grd2<- data.frame(lon=rep(seq(12,18,0.25),13), lat=rep(seq(33.5, 36.5, 0.25),25), a= runif(325))
p2<- ggplot(data = world) +
  geom_sf(fill="antiquewhite") +
  coord_sf(xlim = c(12, 18), ylim = c(30, 40), expand = FALSE)+
  geom_raster(data=grd2, aes(x = lon, y = lat, fill = a ),interpolate = FALSE) +
  scale_fill_continuous(limits = c(0, 1)) 

p1 + p2 + 
  plot_layout(guides = "collect") & 
  theme(legend.position = "bottom")

Upvotes: 1

Related Questions