fig: Determine figure location

View source: R/plot_extra.R

figR Documentation

Determine figure location

Description

Convenience function to determine the location of a figure within a matrix. fig2 will also identify edge figures.

Usage

fig(idx, dim, byrow = TRUE)

fig2(idx, dim, byrow = TRUE, nfig = prod(dim))

Arguments

idx

the figure index

dim

the dimensions of the figure matrix, usually set with par(mfrow = )

byrow

logical; use FALSE if par(mfcol = ) was used since this sets figures to be column-major; otherwise, figures are assumed to be drawn row-major

nfig

the number of figures that will be drawn

Examples

op <- par(no.readonly = TRUE)
## eg, a 1x1 figure
fig(1, c(1, 1))

par(mfrow = c(2, 4))
sapply(1:8, function(x)
  plot(1, main = paste(x, '-', names(fig(x, par('mfrow'))))))

par(mfcol = c(2, 4))
sapply(1:8, function(x)
  plot(1, main = paste(x, '-', names(fig(x, par('mfcol'), FALSE)))))

## par(mfrow = c(4, 4))
i <- fig(1:16, c(4, 4))
matrix(names(i), 4, 4, byrow = TRUE)

## for mfcol, set byrow = FALSE
## par(mfcol = c(2, 8))
i <- fig(1:16, c(2, 8), FALSE)
matrix(names(i), 2, 8, byrow = FALSE)

## par(mfrow = c(1, 4))
i <- fig(1:4, c(1, 4))
matrix(names(i), 1, 4, byrow = TRUE)

## par(mfrow = c(4, 1))
i <- fig(1:4, c(4, 1))
matrix(names(i), 4, 1, byrow = TRUE)

## which figures are on the edges
par(mfrow = c(3, 3))
i <- fig2(1:7, par('mfrow'), nfig = 7)
tmp <- sapply(1:7, function(ii) {
  plot(
    1,
    main = ifelse(i$top[ii], 'main', ''),
    xlab = ifelse(i$bottom[ii], 'x label', ''),
    ylab = ifelse(i$left[ii], 'y label', '')
  )
  if (i$right[ii])
    axis(4L)
  idx <- c(i$bottom[ii], i$right[ii], i$left[ii], i$top[ii])
  legend('top', bty = 'n', legend = toString(
    c('bottom', 'right', 'left', 'top')[idx]
  ))
})

## example usage
pars <- list(
  ltop    = list(pch = 4, tcl = 0, bty = 'l'),
  lcenter = list(pch = 1, las = 1, tcl = 0, bty = 'n'),
  lbottom = list(pch = 16, las = 0, tcl = 0.5, bty = '7')
)

par(mfrow = c(5, 1), oma = c(5, 4, 4, 2), mar = c(0, 0, 0, 0))
## apply a different set of pars depending on the figure location
sapply(1:5, function(ii) {
  f <- fig(ii, par('mfrow'))
  par(pars[[names(f)]])
  plot(1)
})

par(op)


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