################Arranging plots in a grid
#Basic usage
library(ggplot2)
library(cowplot)
## 
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
##   default ggplot2 theme anymore. To recover the previous
##   behavior, execute:
##   theme_set(theme_cowplot())
## ********************************************************
###########################m_1
p1 <- ggplot(mtcars, aes(disp, mpg)) +
  geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()
plot_grid(p1, p2, labels = c('A', 'B'),label_size = 20,
          label_fontfamily = "sans",
          label_fontface = "bold",
          label_colour = "red",
          ncol = 2)

plot_grid(p1, p2, labels = "AUTO", rel_widths = c(1, 2))

# the second plot in the first row and the
# first plot in the second row are missing
plot_grid(
  p1, NULL, NULL, p2,
  labels = "AUTO", ncol = 2
)

##########################M_2
p3 <- p1 +
  # use large, rotated axis tick labels to highlight alignment issues
  theme(axis.text.x = element_text(size = 14, angle = 90, vjust = 0.5))

# plots are drawn without alignment
plot_grid(p3, p2, labels = "AUTO")

# plots are drawn with horizontal alignment
plot_grid(p3, p2, labels = "AUTO", align = "h")

plot_grid(p3, p2, labels = "AUTO", align = "v",nrow = 2)

#Nested plot grids
bottom_row <- plot_grid(p1, p2, labels = c('B', 'C'), label_size = 12)
bottom_row

p3 <- ggplot(mtcars, aes(x = qsec, y = disp)) + geom_point() + facet_wrap(~gear)
p3

plot_grid(p3, bottom_row, labels = c('A', ''), label_size = 12, ncol = 1)

# first align the top-row plot (p3) with the left-most plot of the
# bottom row (p1)
plots <- align_plots(p3, p1, align = 'v', axis = 'l')
plots
## [[1]]
## TableGrob (13 x 17) "layout": 27 grobs
##     z         cells        name                                           grob
## 1   0 ( 1-13, 1-17)  background               rect[plot.background..rect.1161]
## 2   1 ( 8- 8, 5- 5)   panel-1-1                      gTree[panel-1.gTree.1027]
## 3   1 ( 8- 8, 9- 9)   panel-2-1                      gTree[panel-2.gTree.1042]
## 4   1 ( 8- 8,13-13)   panel-3-1                      gTree[panel-3.gTree.1057]
## 5   3 ( 6- 6, 5- 5)  axis-t-1-1                                 zeroGrob[NULL]
## 6   3 ( 6- 6, 9- 9)  axis-t-2-1                                 zeroGrob[NULL]
## 7   3 ( 6- 6,13-13)  axis-t-3-1                                 zeroGrob[NULL]
## 8   3 ( 9- 9, 5- 5)  axis-b-1-1           absoluteGrob[GRID.absoluteGrob.1061]
## 9   3 ( 9- 9, 9- 9)  axis-b-2-1           absoluteGrob[GRID.absoluteGrob.1061]
## 10  3 ( 9- 9,13-13)  axis-b-3-1           absoluteGrob[GRID.absoluteGrob.1061]
## 11  3 ( 8- 8,12-12)  axis-l-1-3                                 zeroGrob[NULL]
## 12  3 ( 8- 8, 8- 8)  axis-l-1-2                                 zeroGrob[NULL]
## 13  3 ( 8- 8, 4- 4)  axis-l-1-1           absoluteGrob[GRID.absoluteGrob.1073]
## 14  3 ( 8- 8,14-14)  axis-r-1-3                                 zeroGrob[NULL]
## 15  3 ( 8- 8,10-10)  axis-r-1-2                                 zeroGrob[NULL]
## 16  3 ( 8- 8, 6- 6)  axis-r-1-1                                 zeroGrob[NULL]
## 17  2 ( 7- 7, 5- 5) strip-t-1-1                                  gtable[strip]
## 18  2 ( 7- 7, 9- 9) strip-t-2-1                                  gtable[strip]
## 19  2 ( 7- 7,13-13) strip-t-3-1                                  gtable[strip]
## 20  4 ( 5- 5, 5-13)      xlab-t                                 zeroGrob[NULL]
## 21  5 (10-10, 5-13)      xlab-b titleGrob[axis.title.x.bottom..titleGrob.1152]
## 22  6 ( 8- 8, 3- 3)      ylab-l   titleGrob[axis.title.y.left..titleGrob.1155]
## 23  7 ( 8- 8,15-15)      ylab-r                                 zeroGrob[NULL]
## 24  8 ( 4- 4, 5-13)    subtitle         zeroGrob[plot.subtitle..zeroGrob.1157]
## 25  9 ( 3- 3, 5-13)       title            zeroGrob[plot.title..zeroGrob.1156]
## 26 10 (11-11, 5-13)     caption          zeroGrob[plot.caption..zeroGrob.1159]
## 27 11 ( 2- 2, 2- 2)         tag              zeroGrob[plot.tag..zeroGrob.1158]
## 
## [[2]]
## TableGrob (12 x 9) "layout": 18 grobs
##     z         cells       name                                           grob
## 1   0 ( 1-12, 1- 9) background               rect[plot.background..rect.1198]
## 2   5 ( 6- 6, 4- 4)     spacer                                 zeroGrob[NULL]
## 3   7 ( 7- 7, 4- 4)     axis-l           absoluteGrob[GRID.absoluteGrob.1186]
## 4   3 ( 8- 8, 4- 4)     spacer                                 zeroGrob[NULL]
## 5   6 ( 6- 6, 5- 5)     axis-t                                 zeroGrob[NULL]
## 6   1 ( 7- 7, 5- 5)      panel                      gTree[panel-1.gTree.1178]
## 7   9 ( 8- 8, 5- 5)     axis-b           absoluteGrob[GRID.absoluteGrob.1182]
## 8   4 ( 6- 6, 6- 6)     spacer                                 zeroGrob[NULL]
## 9   8 ( 7- 7, 6- 6)     axis-r                                 zeroGrob[NULL]
## 10  2 ( 8- 8, 6- 6)     spacer                                 zeroGrob[NULL]
## 11 10 ( 5- 5, 5- 5)     xlab-t                                 zeroGrob[NULL]
## 12 11 ( 9- 9, 5- 5)     xlab-b titleGrob[axis.title.x.bottom..titleGrob.1189]
## 13 12 ( 7- 7, 3- 3)     ylab-l   titleGrob[axis.title.y.left..titleGrob.1192]
## 14 13 ( 7- 7, 7- 7)     ylab-r                                 zeroGrob[NULL]
## 15 14 ( 4- 4, 5- 5)   subtitle         zeroGrob[plot.subtitle..zeroGrob.1194]
## 16 15 ( 3- 3, 5- 5)      title            zeroGrob[plot.title..zeroGrob.1193]
## 17 16 (10-10, 5- 5)    caption          zeroGrob[plot.caption..zeroGrob.1196]
## 18 17 ( 2- 2, 2- 2)        tag              zeroGrob[plot.tag..zeroGrob.1195]
# then build the bottom row
bottom_row <- plot_grid(plots[[2]], p2, labels = c('B', 'C'), label_size = 12)
bottom_row

# then combine with the top row for final plot
plot_grid(plots[[1]], bottom_row, labels = c('A', ''), label_size = 12, ncol = 1)

#Joint plot titles
# make a plot grid consisting of two panels
p1 <- ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(colour = "blue") +
  theme_half_open(12) +
  background_grid(minor = 'none')

p2 <- ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(colour = "green") +
  theme_half_open(12) +
  background_grid(minor = 'none')
p1; p2

plot_row <- plot_grid(p1, p2)
# now add the title
title <- ggdraw() +
  draw_label(
    "Miles per gallon decline with displacement and horsepower",
    fontface = 'bold',
    x = 0,
    hjust = 0
  ) +
  theme(
    # add margin on the left of the drawing canvas,
    # so title is aligned with left edge of first plot
    plot.margin = margin(0, 0, 0, 7)
  )
title

plot_row

plot_grid(
  title, plot_row,
  ncol = 1,
  # rel_heights values control vertical title margins
  rel_heights = c(0.1, 1)
)

#ref  https://wilkelab.org/cowplot/articles/plot_grid.html