legendQuad: Creates a "four-color" plot legend for interpreting data with...

View source: R/legendQuad.r

legendQuadR Documentation

Creates a "four-color" plot legend for interpreting data with mixtures of four aspects.

Description

This function adds a legend to an existing plot. The legend is typically a square with each corner assigned a different color. The closer an object in the main plot is to one of four "assignments" (e.g., populations), the more the corresponding color is shown, with assignments of intermediate value displaying as a mixture of colors. The function first draws a "containing" box then a square "color swatch" with a color gradient inside. A legend title and labels for the four corners can be added.

Usage

legendQuad(
  x,
  y = NULL,
  inset = 0,
  width = 0.2,
  height = 0.2,
  labels = LETTERS[1:4],
  labelAdj = 1,
  cols = c("white", "cyan", "black", "red"),
  n = 25,
  border = par("fg"),
  title = "",
  titleAdj = c(0.5, 0.9),
  aspect = FALSE,
  swatchAdjX = c(0.15, 0.85),
  swatchAdjY = c(0.25, 0.85),
  boxBg = par("bg"),
  boxBorder = par("fg"),
  ...
)

Arguments

x

Numeric or character. Describes the location of the legend. This is a numeric value (in which case y must also be supplied) indicating the x-coordinate of the top left of the box surrounding the legend. Alternatively, it is a character describing the position of the box surrounding the legend relative to the existing plot ('topleft', 'topright', 'bottomleft', 'bottomright', 'top', 'bottom', 'left', or 'right').

y

Numeric or NULL. Y-coordinate of the top left corner of the legend.

inset

Numeric. If x is a word describing the position of the legend, then this is the degree to which the legend is inset (or outset, if negative) relative to the figure's border. If two values are supplied then the first pertains to the horizontal offset and the second the vertical offset.

width

Numeric. Scaling factor for box width.

height

Numeric. Scaling factor for box height.

labels

Character vector used to name the four corners of the color swatch in this order: bottom left, top left, top right, bottom right. Leave as NULL to skip displaying labels.

labelAdj

Numeric, usually between 0 and 1. Position of corner labels relative to the corners of the swatch.

cols

Character list. Names of four colors to be used to create a gradient to fill the color swatch. The first color will be assigned to the lower left corner, the second to the upper left, third to upper right, and fourth to lower right. Note that not every color combination produces a map with a unique color at each coordinate of the color swatch.

n

Positive integer, number of squares used to approximate a smooth color map in the swatch. This is the number of cells along each side of the swatch. Higher values create less "blocky" swatches but increase drawing time.

border

Character or integer. Name (or integer code) of color to use to draw border of the color swatch. Leave as NULL to skip drawing a border.

title

Character or NULL. Name of title for the legend.

titleAdj

Two numeric values between 0 and 1. Position of the legend relative to the container box. The first pertains to horizontal positioning and the second vertical positioning.

aspect

Logical, if TRUE then the height of the color swatch is scaled by swatchAdjX so that the swatch is square (and swatchAdjY is ignored). If FALSE then the height is determined by swatchAdjY.

swatchAdjX

Two numeric values between 0 and 1. Size of the color swatch in the x-dimension as a proportion of the container box size. The first pertains to the left side of the bar and the second the right side.

swatchAdjY

Two numeric values between 0 and 1. Size of the swatch in the y-dimension as a proportion of the container box size. The first pertains to the top of the bar and the second the bottom. Alternatively, if aspect is TRUE then the first value of swatchAdjY is used to place the top of the swatch but the bottom is located such that the swatch is square (i.e., the second value of swatchAdjY is ignored).

boxBg

Character or integer. Name (or integer code) of color to use to use for box containing legend. Leave as NULL to not draw a box.

boxBorder

Character or integer. Name (or integer code) of color to use to use for box border containing legend. Leave as NULL to not draw a box border.

...

Arguments to pass to plot, polygon, or text.

Value

Nothing (side effect is to add a legend to an existing graphics device).

See Also

legend, legendGrad,

Examples

data(religion)
head(religion)
religion$others <- rowSums(religion[ , c('unaffiliated', 'hindu', 'folk', 'other', 'jewish')])
religs <- c('christian', 'muslim', 'buddhist', 'others')
par(mfrow=c(1, 2))
plotQuad(religion[ , religs], 'points', background=FALSE, main='Religion by Country')
legendQuad('top', inset=0.01, labels=colnames(relig))

adamlilith/legendary documentation built on July 28, 2023, 8:13 p.m.