Easy way to mix multiple graphs on the same page - R software and data visualization

## Install and load required packages

1 | install.packages("gridExtra") |

## Prepare some data

1 | df <- ToothGrowth |

## Cowplot: Publication-ready plots

The cowplot package is an extension to ggplot2 and it can be used to provide a publication-ready plots.

## Basic plots

1 | library(cowplot) |

Recall that, the function **ggsave()**[in **ggplot2** package] can be used to save ggplots. However, when working with **cowplot**, the function **save_plot()** [in **cowplot** package] is preferred. It’s an alternative to **ggsave** with a better support for multi-figur plots.

1 | save_plot("mpg.pdf", plot.mpg, |

## Arranging multiple graphs using cowplot

1 | # Scatter plot |

Combine the two plots (the scatter plot and the bar plot):

1 | plot_grid(sp, bp, labels=c("A","B"), ncol = 2, nrow = 1) |

The function **draw_plot()** can be used to place graphs at particular locations with a particular sizes. The format of the function is:

1 | draw_plot(plot, x = 0, y = 0, width = 1, height = 1) |

**plot**: the plot to place (ggplot2 or a gtable)

**x**: The x location of the lower left corner of the plot.

**y**: The y location of the lower left corner of the plot.

**width, height**: the width and the height of the plot

The function

**ggdraw()**is used to initialize an empty drawing canvas.

1 | plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + |

## grid.arrange: Create and arrange multiple plots

The R code below creates a box plot, a dot plot, a violin plot and a stripchart (jitter plot) :

1 | library(ggplot2) |

Combine the plots using the function **grid.arrange()** [in **gridExtra**] :

1 | library(gridExtra) |

## Add a common legend for multiple ggplot2 graphs

This can be done in four simple steps :

To

**save the legend**of a ggplot, the helper function below can be used :

1 | library(gridExtra) |

(The function above is derived from this forum. )

1 | # 1. Create the plots |

## Scatter plot with marginal density plots

**Step 1/3. Create some data :**

1 | x <- c(rnorm(500, mean = -1), rnorm(500, mean = 1.5)) |

**Step 2/3. Create the plots :**

1 | # Scatter plot of x and y variables and color by groups |

Create a blank placeholder plot :

1 | lankPlot <- ggplot()+geom_blank(aes(1,1))+ |

**Step 3/3. Put the plots together:**

Arrange ggplot2 with adapted height and width for each row and column :

1 | library("gridExtra") |

## Create a complex layout using the function viewport()

The different steps are :

**grid.newpage()**

1 | # Move to a new page |

## Insert an external graphical element inside a ggplot

The function **annotation_custom()** [in **ggplot2**] can be used for adding tables, plots or other grid-based elements. The simplified format is :

1 | annotation_custom(grob, xmin, xmax, ymin, ymax) |

**grob**: the external graphical element to display

**xmin, xmax**: x location in data coordinates (horizontal location)

**ymin, ymax**: y location in data coordinates (vertical location)

The different steps are :

**annotation_custom()**

As the inset box plot overlaps with some points, a

**transparent background**is used for the box plots.

1 | # Create a transparent theme object |

Create the graphs :

1 | p1 <- scatterPlot # see previous sections for the scatterPlot |

1 | # Insert p3_grob inside the scatter plot |

If you have a solution to insert, at the same time, both p2_grob and p3_grob inside the scatter plot, please let me a comment. I got some errors trying to do this…

## Mix table, text and ggplot2 graphs

The functions below are required :

**tableGrob()**[in the package

*gridExtra*] : for adding a data table to a graphic device

**splitTextGrob()**[in the package

*RGraphics*] : for adding a text to a graph

Make sure that the package

**RGraphics**is installed.

1 | library(RGraphics) |

## Infos

This analysis has been performed using **R software** (ver. 3.1.2) and **ggplot2** (ver. 1.0.0)

Contribution from ：http://www.sthda.com/english/wiki/ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page-r-software-and-data-visualization