• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggplot2 / New Version of Patchwork is Here: Inset a plot inside a plot

New Version of Patchwork is Here: Inset a plot inside a plot

November 14, 2020 by cmdlinetips

Insetting with patchwork

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.

Mobile Growth in USA
Mobile Growth in USA

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)
Landline Phone Growth in USA
Landline Phone Growth in USA

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.

Insetting plots with Patchwork in R
Insetting plots with Patchwork in R

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.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Seaborn Version 0.11.0 is HereSeaborn Version 0.11.0 is here with displot, histplot and ecdfplot Add Tags to combined plot: Patchwork7 Tips to Combine Multiple ggplots Using Patchwork Default Thumbnailggplot2 Version 3.0.0 Brings Tidy Evaluation to ggplot forcats Version is here with four new fct_lump functionsIntroduction to the new lumping functions in forcats version 0.5.0

Filed Under: ggplot2, patchwork inset, R Tagged With: ggplot2, Inset with Patchwork, R

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version