Skip to content

Insets in R

June 11, 2012

Had a bit of a stupid moment today. Laura Timms asked me about the fig graphics parameter in base R graphics, but I’d never heard of the fig graphics parameter! She wanted to make a simple inset graph (i.e. graph within a graph), and found that fig was the way to do it but couldn’t get it working. We kept getting the error message:

invalid value specified for graphical parameter "fig"

The fig parameter accepts a vector of four numbers giving the coordinates of the corners of the rectangle inside which the inset is to be plotted. As with all these things…something is bound to confuse you at first and here’s what got us. We naively thought that the coordinates were in the same units as the variables being plotted on the x and y axes — but this was wrong. They are in NDC coordinates, and getting this is key.

So here’s a simple example that works:


par(new = FALSE)
plot(1:10, 1:10, las = 1)
par(new = TRUE)
par(fig = c(0.47, 0.97, 0.1, 0.6))
plot(1:10, 10:1, xlab = '', ylab = '', las = 1)
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: