The world’s leading publication for data science, AI, and ML professionals.

ggplot2 extensions for better plotting

Extensions that will help you create better plots more efficiently

Patchwork – arrange your graphs

The goal of patchwork is to make it ridiculously simple to combine separate ggplots into the same graphic. To get started with this great library you need to remember three basic operators: + /and | . First, let’s install patchwork and create some plots.

Basic usage

Let us combine our plots into one large plot, this couldn’t be easier using the + operator:

By default, patchwork will try to keep the grid square, and fill it out in row order:

patchwork ggplot2 arrangement using the + operator- Image by author
patchwork ggplot2 arrangement using the + operator- Image by author

Custom arrangements

The /and |operators enable you to go beyond this automated arrangement. | will place the plots beside each other, while / will stack them:

patchwork ggplot2 arrangement using the / and | operator - Image by author
patchwork ggplot2 arrangement using the / and | operator – Image by author

There is a lot of other functionalities like adding annotations, subtitles and other arrangements options. If you want to learn more about this awesome package you should visit its great documentation page.


esquisse – create graphs interactively via a UI

This extension allows you to interactively explore your data by visualizing it with the ggplot2 package with a drag and drop interface. The created and styled graphs can be exported or their code can be retrieved for reproducibility. To get started with this package, you really only need two lines of code.

After execution a new window will open, that is actually very self-explanatory: You will first be asked to open a data frame from a specified environment and subsequently, you can simply drag and drop the features of the dataframe into the corresponding areas for x, y, fill, color and size. The menus on the bottom enable you to further style and adjust your plot.

esquisse visualisation from https://github.com/dreamRs/esquisse
esquisse visualisation from https://github.com/dreamRs/esquisse

To learn more about this great package, that makes it super easy to create stunning ggplots (especially for beginners) please visit the official documentation page.


gganimate – animate your plots

Are you tired of static graphs and want to add something extra to your Charts in a presentation or on the web? With gganimate you can create animations using an API that is easy to learn for ggplot2 users. To get started you first need to install two packages:

Nice! Now let us create an animation. If you for example want to plot the Petal Width and Length of the famous iris data and create an animated plot that transitions over the Species (plots them separately), you could do the following:

You can see the standard Ggplot2 command that creates the plot followed by the transition_states() function which creates the animation by declaring the states of the animation (you can think of this as frames in a movie or animation), the length (delay) between two states and the state length.

Animated Iris dataset with gganimate - Image by Author
Animated Iris dataset with gganimate – Image by Author

The documentation of this package is excellent, therefore, if you want to dive deeper into animations with ggplot2, please visit https://gganimate.com/


ggplot themes – add some style to your plots

ggplots look awesome by default, but sometimes we want to create stylish plots that match a certain topic. Here is an overview of the ggplot extension that enable you to easily change the default themes:

cowplot

"The cowplot package is a simple add-on to ggplot. It provides various features that help with creating publication-quality figures, such as a set of themes, functions to align plots and arrange them into complex compound figures, and functions that make it easy to annotate plots and or mix plots with images." – from https://github.com/wilkelab/cowplot

Cowplot creates a characteristic minimal visualisation that looks great. You can convert any plot to a cowplot by simply adding theme_cowlplot(). If you want to add a grid to the theme, you can for example use theme_minimal_hgrid() for horizontal lines.

Please also note that cowplot includes functions for arranging plots such as the plot_grid() function that was used to create the visualisation below:

Typical cowplot theme - Image by Author
Typical cowplot theme – Image by Author

ggpubr

The ‘ggpubr’ package provides some easy-to-use functions for creating and customizing ‘ggplot2’- based publication ready plots. – from https://rpkgs.datanovia.com/ggpubr/

The package offers custom functions for the creation of stylish graphs, subsequently, I want to show you three of my favourites:

  • ggdensity
  • ggviolin
  • ggdotchart
density, violin and dotchart from the ggpubr package - Image by Author
density, violin and dotchart from the ggpubr package – Image by Author

More themes

The world of ggplot themes seems endless and it wouldn’t be possible to present all themes here in this article. However if you don’t have enough, here is a couple of references that will hopefully help you finding the right theme for you:

Related material for even better plots

[1] List of ggplot2 extension: https://exts.ggplot2.tidyverse.org/gallery/

[2] Create interactive ggplots with ggiraph: https://davidgohel.github.io/ggiraph/

[3] Carson Sievert, Interactive web-based data visualization with R, plotly, and shiny: https://plotly-r.com/index.html


Related Articles