subplot: Subplot

View source: R/plot_extra.R

subplotR Documentation

Subplot

Description

Embed a subplot in an existing plot at specified coordinates.

Usage

subplot(
  expr,
  x,
  y = NULL,
  log = NULL,
  size = c(1, 1),
  vadj = 0.5,
  hadj = 0.5,
  inset = c(0, 0),
  type = c("plt", "fig"),
  pars = NULL
)

Arguments

expr

an expression defining the plot to be embedded

x, y

coordinates of the new plot given by a keyword (see details) or values accepted by xy.coords; if missing, the user will be prompted to select the bottom-left and top-right coordinates for the plotting region

log

character, "x", "y" or both, as for plot; sets negative values to NA and gives a warning

size

the size of the plot in inches if x and y are length 1

vadj

vertical adjustment of the plot when y is a scalar; the default is to center vertically; 0 means place the bottom of the plot at y, and 1 places the top of the plot at y

hadj

horizontal adjustment of the plot when x is a scalar, the default is to center horizontally; 0 means place the left edge of the plot at x, and 1 means place the right edge of the plot at x

inset

1 or 2 numbers representing the proportion of the plot to inset the subplot from edges when x is a character string; the first element is the horizontal inset, and the second is the vertical inset

type

character string of "plt" or "fig"; if "plt" then the plotting region is defined by x, y, and size with axes, etc. outside the box; if "fig" then all annotations are also inside the box

pars

a list of parameters to be passed to par before running expr

Details

The coordinates for expr may be given by a keyword passed to x (similar to legend). x will be grepped for "right", "left", "top", and/or "bottom", and the coordinates for expr will be automatically calculated; see examples.

If x and y are numeric and length 1, these will be used as the center of the plotting region; if length 2, these will be used as the minimum and maximum coordinates of the plotting region (similarly if x is missing and the user is prompted by locator to give the coordinates interactively).

Value

The list of graphical parameters used when drawing expr, useful for adding an additional subplot after subplot has finished.

See Also

TeachingDemos::subplot; http://sickel.net/blogg/?p=688; inset

Examples

plot(1)
subplot(plot(density(rnorm(100)), ann = FALSE), 1.2, 1.2)
subplot(plot(density(rnorm(100)), ann = FALSE, axes = FALSE), 'bottomright')


## Not run: 
## augment a map
library('maptools')
data(state.vbm)

plot(state.vbm, fg = NULL)
tmp <- cbind(state.vbm$center_x, state.vbm$center_y)

for (i in 1:50) {
  tmp2 <- as.matrix(USArrests[i, c(1, 4)])
  tmp3 <- max(USArrests[, c(1, 4)])
  subplot({
    barplot(matrix(tmp2), ylim = c(0, tmp3), yaxt = 'n', col = 1:2,
            beside = TRUE, border = 1:2)
   }, x = tmp[i, 1], y = tmp[i, 2], size = c(.1, .1))
}
legend('bottomright', legend = colnames(USArrests)[c(1, 4)], fill = 1:2)

## End(Not run)


set.seed(1)
tmp <- rnorm(25)
qqnorm(tmp)
qqline(tmp)
pars <- subplot(hist(tmp, ann = FALSE), 0, -1)

## wrong way to add a reference line to histogram
abline(v = 0, col = 2, lwd = 2)

## right way to add a reference line to histogram
op <- par(no.readonly = TRUE)
par(pars)
abline(v = 0, col = 3, lwd = 2)
par(op)


raredd/plotr documentation built on Nov. 19, 2023, 4:09 a.m.