A new version of the R package Patchwork is available on CRAN. If you are not familiar with the Patchwork, it is a R package developed by “one and only” Thomas Lin Pedersen and it makes it possible to combine multiple plots made with R either ggplot2 or base R into a single graphics.
As Thomas Lin Pederson says, Patchwork
allows easy composition of graphics, primarily aimed at ggplot2, but with support for base graphics as well”
A as huge fan of Patchwork, I was really happy to see one of the new features of Patchwork. The new version has the ability to inset on a plot. The Patchwork version 1.1.0.9000 has a new function inset_element(), which allows to “specify the exact location of the edges of the inset in any grid unit you want, thus giving you full freedom of the placement” of a plot inside a plot.
Let us see an examole of how to inset with patchwork. First, let us load the packages needed, here tidyverse and patchwork.
library(tidyverse) library(patchwork) theme_set(theme_bw(16))
Make sure we have the right version of patchwork package.
packageVersion("patchwork")
We will use mobile and landline subscriber datasets containing user growth over time across the worls from TidyTuesday project.
mobile <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/mobile.csv') ## Parsed with column specification: ## cols( ## entity = col_character(), ## code = col_character(), ## year = col_double(), ## total_pop = col_double(), ## gdp_per_cap = col_double(), ## mobile_subs = col_double(), ## continent = col_character() ## )
For the same of simplicity, let us look at the data corresponding to USA alone. Let us make barplot of mobile subscriptions over time for US.
p1 <- mobile %>% filter(entity=="United States") %>% ggplot(aes(x=year, y=mobile_subs)) + geom_col()+ labs(title="Mobile Subscriber Growth: USA") print(p1)
We can clearly see the growth of mobile subscriptions over time.
Now is the time to look at the decline of landline subscriptions. First, let us load the landline data directly from its github page.
landline <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/landline.csv') ## Parsed with column specification: ## cols( ## entity = col_character(), ## code = col_character(), ## year = col_double(), ## total_pop = col_double(), ## gdp_per_cap = col_double(), ## landline_subs = col_double(), ## continent = col_character() ## )
We will be making a similar barplot with landline data as well.
p2 <- landline %>% filter(entity=="United States") %>% ggplot(aes(x=year, y=landline_subs)) + geom_col()+ labs(title="Landline Subscriber Growth: USA") print(p2)
We have saved both the plots as variables and ready to use patchworks’ inset feature. Patchwork has the function inset_element() that lets you add inset to an existing plot. Here we add landline plot to the mobile plot.
p1 + inset_element(p2, right = 0.5, bottom = 0.4, left = 0.01, top = 0.96) ggsave("insetting_with_patchwork.png", width=9,height=8)
Patchwork’s inset_element() function lets you specify exact position to place the second plot within the first plot. Here we adjust the location and the size of inset using the arguments right, bottom, left and top.
Although the ability to have an inset is fantastic, the correct use of inset can be tricky. For example, one needs to think where would an inset is a better option than say facetting. One potential use is when you have a main plot to which you want to draw attention to, but another related plot that can stress the importance of the main plot is a scenario where an inset can be useful.