legendBreaks | R Documentation |
This function adds a legend to an existing plot that shows blocks of color. It is useful, for example, for displaying maps of continuous values that have been thresholded at multiple values. It first draws a "containing" box then inside the box a second box with a set of stacked bars, one per color. A legend title and labels for levels indicated by the color bar can be added.
legendBreaks(
x,
y = NULL,
inset = 0,
horiz = FALSE,
width = 0.2,
height = 0.5,
labels = c(0.25, 0.5, 0.75),
labAdjX = 0.75,
labAdjY = c(0.25, 0.5, 0.75),
col = c("gray", "yellow", "orange", "red"),
colBorder = NA,
border = "black",
title = "Title",
titleAdj = c(0.5, 0.9),
adjX = c(0.2, 0.5),
adjY = c(0.1, 0.8),
boxBg = par("bg"),
boxBorder = "black",
swatches = NULL,
...
)
x |
Numeric or character. Describes the location of the legend. This is a numeric value (in which case |
y |
Numeric or |
inset |
Numeric. If |
horiz |
Logical, if |
width |
Numeric. Scaling factor for box width. |
height |
Numeric. Scaling factor for box height. |
labels |
Vector of characters of numeric values. Labels (from least to most) of levels of the focal variable indicated by the color ramp. |
labAdjX |
Numeric or numeric vector of values typically between 0 and 1. If |
labAdjY |
Numeric vector of values or a single numeric value typically between 0 and 1. If |
col |
List of characters or integers. Names of colors. The first color will be the lowest value and the last the highest value. |
colBorder |
Characters or integer. Names of color to be used to draw an outline around each color in the color box. Use |
border |
Character or integer. Name (or integer code) of color to use to draw border of the color bar. |
title |
Character or |
titleAdj |
Two numeric values between 0 and 1. Position of the legend relative to the legend box. The first pertains to horizontal positioning and the second vertical positioning. |
adjX |
Two numeric values between 0 and 1. Size of the color bar in the x-dimension as a proportion of the legend box size. The first pertains to the left side of the bar and the second the right side. |
adjY |
Two numeric values between 0 and 1. Size of the color bar in the y-dimension as a proportion of the legend box size. The first pertains to the bottom of the bar and the second the top. |
boxBg |
Character or integer. Name (or integer code) of color to use to use for box containing legend. Leave as |
boxBorder |
Character or integer. Name (or integer code) of color to use to use for box border containing legend. Leave as |
swatches |
A list or lists, each of which contains information on extra "swatches" of a single color to add above/below/on the color bar. These are useful, for example, for describing data that does not fall into the range covered by the data (e.g.,
|
... |
Arguments to pass to |
Nothing (side effect is to add a legend to an existing graphics device).
legend
, legendQuad
data(welfare)
# for visual clarity, put countries with high HIV on bottom
# of data frame so they're plotted last
welfare <- welfare[order(welfare$hivAge15to49_2018_perc), ]
welfare <- rbind(
welfare[is.na(welfare$hivAge15to49_2018_perc), ],
welfare[!is.na(welfare$hivAge15to49_2018_perc), ]
)
pop <- welfare$population2019
gdp <- welfare$gdp_2019usd
hiv <- welfare$hivAge15to49_2018_perc
hiv <- hiv / 100
gdpPerCap <- gdp / pop
# color categories
hivMax <- max(hiv, na.rm=TRUE)
cols <- rep(NA, length(hiv))
cols[hiv <= hivMax] <- 'darkred'
cols[hiv <= 0.75 * hivMax] <- 'red'
cols[hiv <= 0.5 * hivMax] <- 'lightsalmon'
cols[hiv <= 0.25 * hivMax] <- 'white'
cols[is.na(hiv)] <- 'gray'
# legend label positions
hivQuants <- hivMax * c(0, 0.25, 0.5, 0.75, 1)
hivLabelPos <- c(0, hivQuants / hivMax)
labels <- sprintf('%.2f', c(0, hivQuants))
# vertical alignment of legend
plot(log10(gdp), gdpPerCap,
pch=21, cex=2, bg=cols, xlab='GDP (log 2019 USD)',
ylab='2019 GDP per Capita (USD)',
)
legendBreaks(
x='bottomright',
y = NULL,
inset = 0.02,
width = 0.23,
height = 0.7,
labels = labels,
labAdjX = 0.75,
labAdjY = hivLabelPos,
col = c('white', 'lightsalmon', 'red', 'darkred'),
border = 'black',
title = 'HIV (%)',
titleAdj = c(0.5, 0.9),
adjX = c(0.2, 0.5),
adjY = c(0.2, 0.8),
boxBorder = 'black',
swatches=list(
list(
swatchAdjY=c(0.05, 0.15),
col='gray',
border='black',
labels='NA'
)
)
)
# horizontal alignment of legend
plot(log10(gdp), gdpPerCap,
pch=21, cex=2, bg=cols, xlab='GDP (log 2019 USD)',
ylab='2019 GDP per Capita (USD)',
)
legendBreaks(
x = 'topleft',
y = NULL,
horiz = TRUE,
inset = 0.02,
width = 0.7,
height = 0.23,
labels = labels,
labAdjX = hivLabelPos,
labAdjY = 0.13,
col = c('white', 'lightsalmon', 'red', 'darkred'),
border = 'black',
title = 'HIV (%)',
titleAdj = c(0.5, 0.9),
adjX = c(0.2, 0.95),
adjY = c(0.3, 0.8),
boxBorder = NA,
boxBg = NA,
swatches=list(
list(
swatchAdjX=c(0.03, 0.18),
col='gray',
border='black',
labels='NA'
)
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.