Drawing outside of the plotting region

In R, traditional graphical output is usually 'clipped' to the plotting region. In other words, any output that would appear otuside of the plotting region is not drawn. In addition, traditional high- and low-level plotting functions that draw in the margins clip output to the current figure region or to the device.

Obviously, it can be useful to override the default clipping region (e.g., this is necessary to draw a legend outside of the plotting region). The traditional clipping region is controlled via the par() function's xpd= argument.

Clipping can occur either to the whole device (xpd = NA), to the current figure region (xpd = TRUE), or to the current plot region (xpd = FALSE, the default).

For our first example, let's draw some lines outside of the plotting region --- this example code comes from section 3.4.3 "Annotating the margins" in Paul Murrell's "R Graphics" book.
y1 <- rnorm(100)
y2 <- rnorm(100)

par(mfrow = c(2, 1), xpd = NA)
# First figure
plot(y1, type = "l", axes = FALSE, xlab = "", ylab = "", main = "")
box(col = "grey")
mtext("Left end of margin", adj = 0, side = 3)
lines(x = c(20, 20, 40, 40), y = c(-7, max(y1), max(y1), -7),
   lwd = 3, col = "grey")
# Second figure
plot(y2, type = "l", axes = FALSE, xlab = "", ylab = "", main = "")
box(col = "grey")
mtext("Right end of margin", adj = 0, side = 3)
mtext("Label below x = 30", at = 30, side = 1)
lines(x = c(20, 20, 40, 40), y = c(7, min(y2), min(y2), 7),
   lwd = 3, col = "grey")

For our second example, let's show how to draw a legend outside of the plotting region --- this example code was printed in a CRC Press' Statistics News pamphlet (code submitted by Paul Murrell):

par(xpd = NA, mar = c(3, 3, 2, 8))
plot(-4:4, (-4:4)^2, pch = 1:2, ann = FALSE)
legend(c("odd", "even"), pch = 1:2, x = 5, y = 4^2)
Topic revision: r2 - 13 Nov 2006, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback