R & GGPLOT – Expanded Plots

November 26, 2013

[This article was first published on Darren Wilkinson » R, 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.

Learn how to create expanded inserts like this easily in R with this ggplot template.

Learn how to create expanded inserts like this easily in R with this ggplot template.


R and GGPLOT are great! There are some things, however, that I want to be able to do easily. For these things I have to create template codes, which I want to share with you!

This time around I wanted to show you how you can create “zoomed” areas of a plot and insert them into your main plot. This template will also show you how you can insert a completely different plot as an insert. This could be a table, map,….anything. This is a common thing to see in many plots, and alas now the functionality can be achieved easily.

DOWNLOAD TEMPLATE – Link to download from my script archive

Summary of the code:

  • You will define two plots: p1 and p2
    • p1 is the main plot – code it normally
    • p2 is the zoomed in area of p1 – the code for this is almost the same (see differences below)
    • p2 is placed inside of p1 and placed automatically into one of the corners that you specify
  • The differences between p1 and p2 are:
    • p1 has a rectangular grob defining the zoomed area – make sure the variables for its limits are linked to the customisation variables!
    • p2 has had the theme options changed to get rid of scales etc – we basically want the plot area to take up the entire device for p2, with no scales or padding.
  • Most of the customisation in the template is automated with variables – found at the top of the document
    • zoom – set the level of zoom
    • pad – set the padding of the insert
    • ex.col – fill colour of the expand boxes if you wish
    • ex.alpha – set this alpha to zero if you don’t want a fill
    • ex.lin – line colour of the zoom boxes
    • x.expand – the x range you wish to expand
    • y.expand – the y range you wish to expand
    • j.expand – the corner you wish the zoomed box to be in
    • ….other variables must be defined, but these are nicely laid out in a box at the top of the code
  • Awesome features of the code include
    • Aspect ratio is maintained for your zoomed area
    • Level of zoom can be easily defined – everything else is adjusted accordingly
    • The zoomed area is labeled with a zoom level
    • You can position the expanded box in any corner simply by defining j.expand as “tl” for Top Left etc – the computation of plotting coordinates is done automatically
    • Resolution is dynamic in the zoom box – so if you change your mind about zoom, it can be adjusted at 100% resolution – no loss

DOWNLOAD TEMPLATE – Link to download from my script archive

The code is easily customised, but pay attention to areas of code that are linked into customisation parameters. When you create your own code you should link in these variables into your plot to allow easy editing! To help you out with this, parts of the plot code that contain variables linked into the customisation parameters are commented with “#IMPORTANT“. As a general rule, though, follow this workflow:

  1. Create your base plot (p1)
  2. Define the rectangular grob (using customisation variables)
  3. Copy and use theme () options from p1 in the template
  4. Duplicate p1 to create p2
  5. Copy and use the theme () options for p2 from the template

If you have any questions, please don’t hesitate to comment. If you are having problems, provide simple examples of code which exhibit the problem you’re having and I’ll do my best to look at it!

Below are some edits of the code that show you how easily things can be changed…

zoom = 2.6

zoom = 2.6

pad = 3Screen Shot 2013-11-26 at 19.26.38

ex.lin = “red”

Screen Shot 2013-11-26 at 19.28.04

zoom = 5; pad = 1; x.expand = c(3, 4); y.expand = c(13, 16), j.expand = “br”

Screen Shot 2013-11-26 at 19.33.19

To leave a comment for the author, please follow the link and comment on their blog: Darren Wilkinson » R.

R-bloggers.com 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)