Multiple plots with subplot in R

[This article was first published on 0xCAFEBABE, 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.

I’m in the middle of creating a poster and wanted to compresss the content by transforming some of the charts into subplots of other charts.

I made a little survey and found that there is a TeachingDemos library in CRAN that fits my needs. Well, the parameterization of the functions is a bit tricky but after a few tries you get used to it.

However, there is a minor flaw in the code of version 2.7: when I wanted to use multiple plots on one layout (e.g., next to each other to use a common legend), it always drew the charts into the first plotting area. A bit of Googling helped, my solution is based on this thread on the R-help list since October, 2010. If you manage to install version 2.8, the problem is solved automatically. On the other side, if you want a quick solution, add the following code segment to your script (so it is shadowing the original function definition).

Only change the first 3 lines of the code, leave the other untouched (you can verify it by diffing this code and entering subplot into the R console).


There is a second workaround suggested in the aforementioned thread, namely managing par('mfg') by hand before and after executing the subplot() function, but it didn’t work for me. The third option (wait a few days until the CRAN packages are updated to 2.8) seems to take a bit longer. 🙂 Anyway, it is working so I’m happy.

To leave a comment for the author, please follow the link and comment on their blog: 0xCAFEBABE. 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.

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)