Patchwork, the R package that lets you combine multiple figures made by ggplot2, got a big update late last year and it is on CRAN now.
If you have not heard of Patchwork, it is an R package made by the awesome Thomas Lin Pedersen. Patchwork lets you combine separate plots made by ggplot to make a single figure that is publication quality. Patchwork is like two other options out there gridExtra and cowplot.
Finally got a chance to try out patchwork for combining plots under most common scenario. And here are the tips to get started with patchwork.
Let us first install the latest version of patchwork (https://github.com/thomasp85/patchwork). One can install patchwork directly from CRAN using
install.packages('patchwork')
Let us load all the R packages needed to get started. We will use gapminder dataset for making plots and combining them with patwork.
# load tidyverse library(tidyverse) # load gapminder library(gapminder) # load patchwork library(patchwork) # set ggplot theme theme_set(theme_bw())
Let us make two plots: a scatter plot and a boxplot using gapminder data with ggplot2. We save each plot to a variable and will reuse the variables throughout the post.
p1 <- gapminder %>% filter(year==1952) %>% ggplot(aes(x=gdpPercap, y=lifeExp))+ geom_point() + scale_x_log10() + labs(subtitle="1952") p2 <- gapminder %>% filter(year==1952) %>% ggplot(aes(x=continent, y=lifeExp, group=continent))+ geom_boxplot() + geom_jitter(width=0.2, alpha=0.4)+ labs(subtitle="1952")
How to Combine Two Plots?
Combining two plots is simple, use plus sign to combine two ggplot2 objects.
p1 + p2
How to Combine Two Plots Side by Side?
Sometimes you may want to combine two plots in a specific way. For example, to combine to plots so that both the plots are side by side, you use “|” the pipe symbol instead of “plus” symbol.
# two plots side by side p1|p2
You get a combined plot where two plots are side by side.
How to Combine Two Plots one over the other?
If you want to combine two plots such that one is on top of the other, i.e. vertically, use “/” between the two ggplot2 objects.
# two plots one over the other p1/p2
The first plot object will be on top of the second object.
How to Combine Multiple Plots in a Grid?
What if you want to combine more than two plots. We can combine multiple plots into a single plot in a grid using the combination of the “+” and “/”symbols.
For example to have two plots in the first row and third plot in the second row you would use
p3 <- gapminder %>% ggplot(aes(x=lifeExp, fill=continent))+ geom_density(alpha=0.4) (p1 + p2)/p3
Now you have a grid with three plots combined in a single patched plot.
How to Place Legends in a Common Place?
When combining multiple plots together, sometimes you might want to put legends in a common place instead of right next to each plot.
We can place all legends in a common place using plot_layout() function as shown below. Here, we have combined two plots side by side and placed the legends in a common place.
p1 + p2 + plot_layout(guides="collect") ggsave("combine_two_plots_legends_in_common_place_patchwork.jpg")
6. How to Tag each plot in a combined plot with patchwork?
When we combine multiple plots adding a name or tag to each plot is extremely useful to refer a specific plot from the combined plot in a document. Patchwork’s plot_annotation() function can add tags automatically by specifying what type of tags we want to add.
For example to add upper case letters, we use plot_annotation(tag_levels = ‘A’) and ‘1’ for Arabic numerals, ‘a’ for lowercase Latin letters, ‘I’ for uppercase Roman numerals, and ‘i’ for lowercase Roman numerals.
Let us add tags with upper case letters for the patched plot.
patched <- (p1 + p2)/p3 patched + plot_annotation(tag_levels = 'A')
Here we have combined three plots and added A, B, and C as tags to each plot.
We can also adjust the font size of the tags easily by using element_text() function within theme as shown below.
patched + plot_annotation(tag_levels = 'A') & theme(plot.tag = element_text(size = 8))
7. How to Combine a ggplot and table?
We can also combine a plot easily with a table. Here we create a table with dplyr functions and use gridExtra to create a table and then combine it with a plot.
p1 + gridExtra::tableGrob(gapminder %>% group_by(continent) %>% summarize(lifeExp_Ave=mean(lifeExp)))
Now we have both the plot and table in a single figure.
[…] out here for more […]