0

I created a layout and I added the legend at the end of last graph plot 4, please see the code below (I deleted the codes of ploe 2 and plot 3 since they are similar to plot 1). But the last graph of plot 4 will become out of shape if I do so See picture. How can I add the legend at the bottom of whole layout with even-sized images?

library(foreign)
library(ggplot2)
library(dplyr)
library(readxl)
library(scales)
library(tidyverse)
Sys.setlocale("LC_TIME", "English")
X0_40cm <- read_excel("C:/Users/Connie/Desktop/LAI/Wheat_F_2018.xlsx")
plot1 <- ggplot(X0_40cm, aes(Date,LAI,group=1))+
  geom_point(data=subset(X0_40cm, Condition=="Measured"),col="blue")+
  geom_line(data=subset(X0_40cm, Condition=="Simulated"),col="black")+
  scale_y_continuous(limits = c(0,3)) +
  labs(title="Winter wheat of F plot in 2018",y="LAI",x="Date")+
  theme_update(plot.title=element_text(hjust=0.5))
plot1

X200cm <- read_excel("C:/Users/Connie/Desktop/LAI/Maize_F_2019.xlsx")
plot4 <- ggplot(mapping = aes(Date, LAI, color = Condition, linetype = Condition, shape = Condition))+
  geom_point(data=subset(X200cm, Condition=="Measured"))+
  geom_line(data=subset(X200cm, Condition=="Simulated"))+
  scale_color_manual(values = c("blue", "black")) +
  scale_linetype_manual(values=c(NA,1)) +
  scale_shape_manual(values=c(19,NA)) +
  theme(legend.position="bottom",legend.key.size=unit(0.1,'cm'),legend.key.width=unit(0.5,'cm'),legend.title=element_blank())+
  scale_y_continuous(limits = c(0,8)) +
  labs(title="Summer maize of F plot in 2019",y="LAI",x="Date")
theme_update(plot.title=element_text(hjust=0.5))
plot4

library(grid)
grid.newpage()
pushViewport(viewport(layout=grid.layout(4,1)))
print(plot1, vp=viewport(layout.pos.row = 1, layout.pos.col = 1))
print(plot2, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
print(plot3, vp=viewport(layout.pos.row = 3, layout.pos.col = 1))
print(plot4, vp=viewport(layout.pos.row = 4, layout.pos.col = 1))
dev.new()

enter image description here

Tung
  • 26,371
  • 7
  • 91
  • 115
Chouette
  • 153
  • 7
  • You could generate another plot that contains only the legend and then add it at as another plot. This answer might be helpful: https://stackoverflow.com/a/12041779/9022665 – Jonathan V. Solórzano Mar 06 '20 at 20:40
  • This arranges multiple legends but you can use the same idea for your case https://stackoverflow.com/a/52067836/786542 – Tung Mar 07 '20 at 00:27

1 Answers1

2

grid_arrange_shared_legend() from the lemon package should do it

library(ggplot2)
# install.packages("lemon")
library(lemon)

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data = dsamp, colour = clarity)
p2 <- qplot(cut, price, data = dsamp, colour = clarity)
p3 <- qplot(color, price, data = dsamp, colour = clarity)
p4 <- qplot(depth, price, data = dsamp, colour = clarity)

grid_arrange_shared_legend(p1, p2, p3, p4, 
                           ncol = 1, 
                           nrow = 4, 
                           position = 'bottom')

Created on 2020-03-06 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Hi! Thank you so much! It works! But I don't know how to code if I want to draw one kind of clarify with line, while others with point under lemon package. Could you please help me with your code? – Chouette Mar 08 '20 at 01:42
  • @LINKANG: Like these https://stackoverflow.com/a/59445501/786542 & https://stackoverflow.com/a/49377292/786542? – Tung Mar 08 '20 at 07:32
  • Always happy to help! – Tung Mar 08 '20 at 16:14