legendGrad | R Documentation |
This function adds a legend to an existing plot that shows a gradient in color. It first draws a "containing" box then a bar with a color gradient inside the box. A legend title and labels for levels indicated by the color bar can be added.
legendGrad(
x,
y = NULL,
inset = 0,
vert = TRUE,
width = 0.2,
height = 0.5,
labels = c(0, 0.33, 0.67, 1),
labAdj = 0.75,
labPos = 4,
labCex = 1,
col = c("yellow", "orange", "red"),
border = "black",
title = "Title",
titleAdj = c(0.5, 0.9),
titlePos = NULL,
titleCex = 1,
adjX = c(0.2, 0.65),
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 |
vert |
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. |
labAdj |
Numeric between 0 and 1. If |
labPos |
1 (right-align labels), 2 (bottom-align labels), 3 (left-align labels), or 4 (top-align labels). |
labCex |
Positive numeric, size of label text. |
col |
List of characters or integers. Names of colors to be used to create a gradient to fill the legend bar. The first color will be the lowest value and the last the highest value. |
border |
Character or integer. Name (or integer code) of color to use to draw border of the gradient bar. |
title |
Character or |
titleAdj |
Two numeric values between 0 and 1. Position of the legend title relative to the container box. The first pertains to horizontal positioning and the second vertical positioning. |
titlePos |
1 (right-align title), 2 (bottom-align title), 3 (left-align title), or 4 (top-align title). |
titleCex |
Positive numeric, size of title text. |
adjX |
Two numeric values between 0 and 1. Size of the gradient bar 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. |
adjY |
Two numeric values between 0 and 1. Size of the gradient bar in the y-dimension as a proportion of the container 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 gradient 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
wealth <- data.frame(
country = c('USA', 'Japan', 'Malaysia', 'Germany', 'England',
'North Korea', 'South Korea', 'Nigeria'),
pop = c(325365189, 126672000, 31718000, 82175700,
54786300, 25370000, 51446201, 185989640),
gdp = c(18558, 5420, 913.593, 4150, 1870, 12.38, 2029, 1166),
perCapGdp = c(145894, 57220, 28490, 50206, 34205, 583, 39446, 6351),
hivPerc = c(0.03, 0.01, 0.40, 0.15, 0.16, NA, 0.29, 0.29)
)
# color ramp
colFx <- grDevices::colorRampPalette(c('white', 'red'))
cols <- colFx(100)
hivPerc <- round(100 * wealth$hivPerc / max(wealth$hivPerc, na.rm=TRUE))
cols <- cols[hivPerc]
cols[is.na(cols)] <- 'gray'
# rescale population (symbols size)
popRescaled <- 1 + 3 * (log10(wealth$pop) - min(log10(wealth$pop)))
plot(wealth$gdp, wealth$perCapGdp, pch=21,
cex=popRescaled, bg=cols, xlab='GDP (Billion $)', ylab='GDP Per Capita ($)')
text(wealth$gdp, wealth$perCapGdp, labels=as.character(wealth$country),
pos=4, xpd=NA)
legendGrad(
x='bottomright',
y = NULL,
inset = 0.02,
width = 0.23,
height = 0.3,
labels = 100 * pretty(wealth$hivPerc),
labAdjX = 0.4,
col = c('white', 'red'),
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',
labels='NA',
cex=1.2,
border='black',
lwd=2,
lty='dotted'
)
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.