rm(list = ls())
library("patchwork")
library("ggplot2")
ggp1 <- ggplot(iris,               # Create ggplot2 scatterplot
               aes(x = Sepal.Length,
                   y = Sepal.Width,
                   col = Species)) + geom_point()
ggp1                               # Draw ggplot2 scatterplot

###########################
ggp2 <- ggplot(iris,               # Create ggplot2 barchart
               aes(x = Species,
                   y = Sepal.Width,
                   fill = Species)) + geom_bar(stat = "identity")
ggp2                               # Draw ggplot2 barchart

###########################
ggp3 <- ggplot(iris,               # Create ggplot2 boxplot
               aes(x = Species,
                   y = Sepal.Width,
                   col = Species)) + geom_boxplot()
ggp3                               # Draw ggplot2 boxplot

###########################
#Example 1: Draw Composition of ggplot2 Plots Using patchwork Package
ggp_sbs <- (ggp1 + ggp2) / ggp3    # Create plot composition
ggp_sbs                            # Draw plot composition

##########################
#Example 2: Print ggplot2 Plots On Top of Each Other Using patchwork Package
ggp_top <- ggp1 +                  # Add plots on top of each other
  inset_element(ggp2, 0.1, 0.1, 0.8, 0.8) +
  inset_element(ggp3, 0.5, 0.5, 0.99, 0.99)
ggp_top   

ggp_top + plot_annotation(title = "The surprising story about mtcars")

ggp_top + plot_annotation(tag_levels = "I")

?plot_annotation
## starting httpd help server ... done
# Draw combined plots
#########################
ggp1 + grid::textGrob("Some really important text", hjust = 1, vjust = 1)

gridExtra::tableGrob(mtcars[1:10, c('mpg', 'disp')])
## TableGrob (11 x 3) "rowhead-fg": 66 grobs
##     z         cells       name                 grob
## 1   1 ( 1- 1, 1- 1) rowhead-fg text[GRID.text.1351]
## 2   2 ( 2- 2, 1- 1) rowhead-fg text[GRID.text.1352]
## 3   3 ( 3- 3, 1- 1) rowhead-fg text[GRID.text.1353]
## 4   4 ( 4- 4, 1- 1) rowhead-fg text[GRID.text.1354]
## 5   5 ( 5- 5, 1- 1) rowhead-fg text[GRID.text.1355]
## 6   6 ( 6- 6, 1- 1) rowhead-fg text[GRID.text.1356]
## 7   7 ( 7- 7, 1- 1) rowhead-fg text[GRID.text.1357]
## 8   8 ( 8- 8, 1- 1) rowhead-fg text[GRID.text.1358]
## 9   9 ( 9- 9, 1- 1) rowhead-fg text[GRID.text.1359]
## 10 10 (10-10, 1- 1) rowhead-fg text[GRID.text.1360]
## 11 11 (11-11, 1- 1) rowhead-fg text[GRID.text.1361]
## 12  0 ( 1- 1, 1- 1) rowhead-bg rect[GRID.rect.1362]
## 13  0 ( 2- 2, 1- 1) rowhead-bg rect[GRID.rect.1363]
## 14  0 ( 3- 3, 1- 1) rowhead-bg rect[GRID.rect.1364]
## 15  0 ( 4- 4, 1- 1) rowhead-bg rect[GRID.rect.1365]
## 16  0 ( 5- 5, 1- 1) rowhead-bg rect[GRID.rect.1366]
## 17  0 ( 6- 6, 1- 1) rowhead-bg rect[GRID.rect.1367]
## 18  0 ( 7- 7, 1- 1) rowhead-bg rect[GRID.rect.1368]
## 19  0 ( 8- 8, 1- 1) rowhead-bg rect[GRID.rect.1369]
## 20  0 ( 9- 9, 1- 1) rowhead-bg rect[GRID.rect.1370]
## 21  0 (10-10, 1- 1) rowhead-bg rect[GRID.rect.1371]
## 22  0 (11-11, 1- 1) rowhead-bg rect[GRID.rect.1372]
## 23  1 ( 1- 1, 2- 2) colhead-fg text[GRID.text.1347]
## 24  2 ( 1- 1, 3- 3) colhead-fg text[GRID.text.1348]
## 25  0 ( 1- 1, 2- 2) colhead-bg rect[GRID.rect.1349]
## 26  0 ( 1- 1, 3- 3) colhead-bg rect[GRID.rect.1350]
## 27  1 ( 2- 2, 2- 2)    core-fg text[GRID.text.1307]
## 28  2 ( 3- 3, 2- 2)    core-fg text[GRID.text.1308]
## 29  3 ( 4- 4, 2- 2)    core-fg text[GRID.text.1309]
## 30  4 ( 5- 5, 2- 2)    core-fg text[GRID.text.1310]
## 31  5 ( 6- 6, 2- 2)    core-fg text[GRID.text.1311]
## 32  6 ( 7- 7, 2- 2)    core-fg text[GRID.text.1312]
## 33  7 ( 8- 8, 2- 2)    core-fg text[GRID.text.1313]
## 34  8 ( 9- 9, 2- 2)    core-fg text[GRID.text.1314]
## 35  9 (10-10, 2- 2)    core-fg text[GRID.text.1315]
## 36 10 (11-11, 2- 2)    core-fg text[GRID.text.1316]
## 37 11 ( 2- 2, 3- 3)    core-fg text[GRID.text.1317]
## 38 12 ( 3- 3, 3- 3)    core-fg text[GRID.text.1318]
## 39 13 ( 4- 4, 3- 3)    core-fg text[GRID.text.1319]
## 40 14 ( 5- 5, 3- 3)    core-fg text[GRID.text.1320]
## 41 15 ( 6- 6, 3- 3)    core-fg text[GRID.text.1321]
## 42 16 ( 7- 7, 3- 3)    core-fg text[GRID.text.1322]
## 43 17 ( 8- 8, 3- 3)    core-fg text[GRID.text.1323]
## 44 18 ( 9- 9, 3- 3)    core-fg text[GRID.text.1324]
## 45 19 (10-10, 3- 3)    core-fg text[GRID.text.1325]
## 46 20 (11-11, 3- 3)    core-fg text[GRID.text.1326]
## 47  0 ( 2- 2, 2- 2)    core-bg rect[GRID.rect.1327]
## 48  0 ( 3- 3, 2- 2)    core-bg rect[GRID.rect.1328]
## 49  0 ( 4- 4, 2- 2)    core-bg rect[GRID.rect.1329]
## 50  0 ( 5- 5, 2- 2)    core-bg rect[GRID.rect.1330]
## 51  0 ( 6- 6, 2- 2)    core-bg rect[GRID.rect.1331]
## 52  0 ( 7- 7, 2- 2)    core-bg rect[GRID.rect.1332]
## 53  0 ( 8- 8, 2- 2)    core-bg rect[GRID.rect.1333]
## 54  0 ( 9- 9, 2- 2)    core-bg rect[GRID.rect.1334]
## 55  0 (10-10, 2- 2)    core-bg rect[GRID.rect.1335]
## 56  0 (11-11, 2- 2)    core-bg rect[GRID.rect.1336]
## 57  0 ( 2- 2, 3- 3)    core-bg rect[GRID.rect.1337]
## 58  0 ( 3- 3, 3- 3)    core-bg rect[GRID.rect.1338]
## 59  0 ( 4- 4, 3- 3)    core-bg rect[GRID.rect.1339]
## 60  0 ( 5- 5, 3- 3)    core-bg rect[GRID.rect.1340]
## 61  0 ( 6- 6, 3- 3)    core-bg rect[GRID.rect.1341]
## 62  0 ( 7- 7, 3- 3)    core-bg rect[GRID.rect.1342]
## 63  0 ( 8- 8, 3- 3)    core-bg rect[GRID.rect.1343]
## 64  0 ( 9- 9, 3- 3)    core-bg rect[GRID.rect.1344]
## 65  0 (10-10, 3- 3)    core-bg rect[GRID.rect.1345]
## 66  0 (11-11, 3- 3)    core-bg rect[GRID.rect.1346]
#ref https://statisticsglobe.com/patchwork-r-package
#https://rpubs.com/lijunjie_ggplot2/597961