inbar: Inset barplot

View source: R/plot2.R

inbarR Documentation

Inset barplot

Description

Draw a barplot with inset bars.

Usage

inbar(
  height,
  width = 1,
  r = NULL,
  alpha = if (group.col) r else 1,
  space = NULL,
  names.arg = NULL,
  legend.text = NULL,
  horiz = FALSE,
  density = NULL,
  angle = 45,
  col = NULL,
  group.col = TRUE,
  border = par("fg"),
  main = NULL,
  sub = NULL,
  xlab = NULL,
  ylab = NULL,
  xlim = NULL,
  ylim = NULL,
  xpd = TRUE,
  log = "",
  axes = TRUE,
  axisnames = TRUE,
  cex.axis = par("cex.axis"),
  cex.names = par("cex.axis"),
  plot = TRUE,
  axis.lty = 0,
  offset = 0,
  add = FALSE,
  ann = !add && par("ann"),
  args.legend = NULL,
  ...
)

Arguments

height

a matrix of data describing the bars which make up the plot, usually a main/total bar and one or more smaller or subsets of the total; bars are grouped by columns; the first row will be the main bar and other rows will be minor bars

width

optional vector of bar widths. Re-cycled to length the number of bars drawn. Specifying a single value will have no visible effect unless xlim is specified.

r

distance (in user coordinates) each inset bar is from the edge of the main bar

alpha

numeric value in [0,1] for the alpha transparency; either a vector equal to nrow(height) (recycled as needed) or a matrix having the same dimensions of height

space

the amount of space (as a fraction of the average bar width) left before each bar. May be given as a single number or one number per bar. If height is a matrix and beside is TRUE, space may be specified by two numbers, where the first is the space between bars in the same group, and the second the space between the groups. If not given explicitly, it defaults to c(0,1) if height is a matrix and beside is TRUE, and to 0.2 otherwise.

names.arg

a vector of names to be plotted below each bar or group of bars. If this argument is omitted, then the names are taken from the names attribute of height if this is a vector, or the column names if it is a matrix.

legend.text

a vector of text used to construct a legend for the plot, or a logical indicating whether a legend should be included. This is only useful when height is a matrix. In that case given legend labels should correspond to the rows of height; if legend.text is true, the row names of height will be used as labels if they are non-null.

horiz

a logical value. If FALSE, the bars are drawn vertically with the first bar to the left. If TRUE, the bars are drawn horizontally with the first at the bottom.

density

a vector giving the density of shading lines, in lines per inch, for the bars or bar components. The default value of NULL means that no shading lines are drawn. Non-positive values of density also inhibit the drawing of shading lines.

angle

the slope of shading lines, given as an angle in degrees (counter-clockwise), for the bars or bar components.

col

colors for each bar; if a vector, each col is applied to a group of bars with alpha transparency added to each minor bar; alternatively, a matrix of colors having the same dimensions of height to set each bar color

group.col

logical; if TRUE (default), columns of height are treated as groups and mapped to one color; if FALSE, each row is mapped to col and recycled for each column of height

border

the color to be used for the border of the bars. Use border = NA to omit borders. If there are shading lines, border = TRUE means use the same colour for the border as for the shading lines.

main, sub

main title and subtitle for the plot.

xlab

a label for the x axis.

ylab

a label for the y axis.

xlim

limits for the x axis.

ylim

limits for the y axis.

xpd

logical. Should bars be allowed to go outside region?

log

string specifying if axis scales should be logarithmic; see plot.default.

axes

logical. If TRUE, a vertical (or horizontal, if horiz is true) axis is drawn.

axisnames

logical. If TRUE, and if there are names.arg (see above), the other axis is drawn (with lty = 0) and labeled.

cex.axis

expansion factor for numeric axis labels (see par('cex')).

cex.names

expansion factor for axis names (bar labels).

plot

logical. If FALSE, nothing is plotted.

axis.lty

the graphics parameter lty (see par('lty')) applied to the axis and tick marks of the categorical (default horizontal) axis. Note that by default the axis is suppressed.

offset

a vector indicating how much the bars should be shifted relative to the x axis.

add

logical specifying if bars should be added to an already existing plot; defaults to FALSE.

ann

logical specifying if the default annotation (main, sub, xlab, ylab) should appear on the plot, see title.

args.legend

list of additional arguments to pass to legend(); names of the list are used as argument names. Only used if legend.text is supplied.

...

arguments to be passed to/from other methods. For the default method these can include further arguments (such as axes, asp and main) and graphical parameters (see par) which are passed to plot.window(), title() and axis.

Value

A numeric vector with the midpoint of each main bar (i.e., identical to a call to barplot(..., beside = FALSE)).

Additionally, a list attribute (attr(., "coords")) where each list element corresponds to a group of bars. The matrices contain coordinates of each rectangle plus midpoints.

See Also

tracebar

Examples

set.seed(1)
tbl <- sapply(1:3, function(x) sort(rpois(3, 10), decreasing = TRUE))
barplot(tbl, col = 1:3)
inbar(tbl, col = 1:3)
inbar(tbl, col = matrix(rainbow(length(tbl)), nrow(tbl)))

## compare group.col = TRUE (default)
inbar(tbl, col = 1:3)
## compare group.col = FALSE
inbar(tbl, col = 1:3, group.col = FALSE)

inbar(
  tbl, col = 1:3, r = c(0.5, 0.8), alpha = c(1, 0.5, 0.25),
  horiz = TRUE, names.arg = 1:3,
  legend.text = 1:3, xlim = c(0, 20), border = NA
)

## inbar returns rect coordinates for future use
co <- inbar(tbl)
co <- do.call('rbind', attr(co, 'coords'))
points(co[, 'midpoint'], co[, 'ytop'], xpd = NA)


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