Create an inset plot

October 10, 2016

[This article was first published on Bluecology blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How to inset a plot on another plot in R

The difficulty of plotting one plot over the top of another in R has always frustrated me. I often end up opening some photo editing software (like Gimp, which is free) and doing it manually. However, manual editing can be frustrating if you need to change the plot.

A histogram with a boxplot inset

I just learned a hassle free way to create insets (thanks to Ben Stewart-Koster) and wanted to share it, so here it is:

First, let’s simulate some fake data

x <- rnorm(100)  
y <- rbinom(100, 1, 0.5)

Now the key step, use par to set the fig param to your desired coordinate space. e.g. in the below we can specify figures positions in 0-1 on both the x and y coordinates:

par(fig = c(0,1,0,1))

Now plot your first figure


Now the second key part, specify where in the coordinate space you want your inset figure to go:

par(fig = c(0.07,0.5, 0.5, 1), new = T)  
boxplot(x ~ y)  

Voila that is it, we have our figure with an inset.

To leave a comment for the author, please follow the link and comment on their blog: Bluecology blog. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)