How to combine Multiple ggplot Plots to make Publication-ready Plots

  1. Visualizing Data

Tags

  • Best R Packages
  • Data Visualisation
  • R Programming

The life cycle of Data science can never be completed without communicating the results of the analysis/research. In fact, Data Visualization is one of the areas where R as a language for Data science has got an edge over the most-celebrated Python. With ggplot2 being the de facto Visualization DSL (Domain-Specific Language) for R programmers, Now the contest has become how effectively one can use ggplot2 package to show visualizations in the given real estate.

In this tutorial, We will learn how to combine multiple ggplot plots to produce publication-ready plots. The R package that we are going to use is cowplot.

About the Package:

As mentioned in the package description, “The cowplot package is meant to provide a publication-ready theme for ggplot2, one that requires a minimum amount of fiddling with sizes of axis labels, plot backgrounds, etc. and also combining multiple plots into one figure and labeling these plots.”

Package Installation:

cowplot can be installed directly from CRAN using the following code:

or the development version from github could be installed using either devtools or remote using the following code:

Package Loading:

Once the package is installed, We can load cowplot using the following code (which is just like every other package loading in R):

Building our First Combined Plot

The way it works in cowplot is that, we have assign our individual ggplot-plots as an R object (which is by default of type ggplot). These objects are finally used by cowplot to produce a unified single plot.

In the below code, We will build three different histograms using the R’s in-built dataset iris and then assign one by one to an R object. Finally, we will use cowplot function plot_grid() to combine the two plots of our interest.

Gives this plot:

In the above plot, you could see those two plots being labelled with Captions/Labels Fig B and Fig C. These Labels were added with the Parameter labels in the plot_grid() function as it is mentioned in the above code,

There are other ways in which we can arrange the above made plots using cowplot. Let’s see a few examples:

Arranging Multiple Plots in Rows – 2 in 1

Gives this plot:

A cowplot plot with ggplot – 3 in 1

Gives this plot:

As you can see in the above code, We have combined cowplot-combined plot with ggplot-generated plot. In this way, We can combine multiple ggplot plots in a variety ways based on the given real estate and business/use-case requirement. The entire code used here in this tutorial is available here on github. Check out DataCamp tutorial, if you are interested in knowing more about Data visualization in R.


Related Post

  • Australian MP tweets collection and quick analysis
  • Introducing vizscorer: a bot advisor to score and improve your ggplot plots
  • Visualize your CV’s timeline with R (Gantt chart style)
  • Analysing UK Traffic Trends with PCA
  • Time series visualizations with wind turbine energy data in R

Related