flex_legend: Create a legend where you want it

Description Usage Arguments Details Value Examples

Description

Creates a vertical or horizontal legend in a location of your choosing

Usage

1
2
3
flex_legend(ncuts, legend.text, legend.vals, legend.pos, longdims, shortdims,
  colvec, leg.adj = c(0, 0), cex.val = 1, srt.val = 0, horiz = TRUE,
  textside = "bottom", textcol = "black", bordercol = "black")

Arguments

ncuts

Number of intervals for legend

legend.text

Label for the legend (e.g. units)

legend.vals

Vector of labels for the legend

legend.pos

A 1 or 2 element vector that specifies where along vector legend.text should be (details)

longdims

Vector specifying the user coordinates for the legend's long axis (e.g. c(0.2, 0.8))

shortdims

1) anchor user coordinate for the short axis; 2) width of short axis (e.g. c(0.05, 0.02))

colvec

Vector of colors to fill each polygon in vector, equal in length to ncuts

leg.adj

Tweak position(s) of legend.vals and/or legend.text (see details)

cex.val

Adjusts the text size of legend.vals and/or legend.text (see details)

srt.val

Rotation of legend.vals and/or legend.text (see details)

horiz

TRUE (default) or FALSE for horizontal or vertical legend

textside

Specify "bottom" (default) or "top" for side of legend.vals on legend

textcol

Vector of colors for legend.vals and legend.text

bordercol

Outline color for polygon borders in legend

Details

Legend.pos uses a two parameter vector, with the second value specifying the polygon number in the legend next to/above/below which the legend.text will appear, and the first which of the four corners of the polygon it will be closest to. Default values are chosen if just one (which then specifies polygon number)vor no values are provided. leg.adj is passed as either a list of two two element vectors, or a single two element vector. If a list, the first element specifies adjustments for legend.values, and the second element adjustments for the legend.text. If just a vector, then these values are used for both. e.g. list(c(0, 0), c(-1, -0.5)); c(0, -0.5).

Value

A polygon based legend

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(raster)
r1 <- raster(nrow = 20, ncol = 20)
r1[] <- sample(0:75, ncell(r1), replace = TRUE)
brks <- round(quantile(r1, seq(0, 1, 0.1)))
cols <- colorRampPalette(c("red", "antiquewhite", "blue"))
par(mar = c(5, 4, 0, 0))
plot(r1, axes = FALSE, box = FALSE, legend = FALSE, breaks = brks, col = cols(length(brks) - 1))
flex_legend(ncuts = length(brks) - 1, legend.text = "vals", leg.adj = list(c(0.5, 1), c(0, -0.5)), 
            legend.vals = brks, legend.pos = c(4, 5), longdims = c(0.2, 0.8), shortdims = c(0.08, 0.03), 
            colvec = cols(length(brks) - 1))
dev.off()

par(mar = c(0, 4, 0, 4))
plot(r1, axes = FALSE, box = FALSE, legend = FALSE, breaks = brks, col = cols(length(brks) - 1))
flex_legend(ncuts = length(brks) - 1, legend.text = "vals", textside = "right", 
            legend.pos = c(3, 10), leg.adj = list(c(0.2, 0.5), c(0, -1)), 
            horiz = FALSE, legend.vals = brks, longdims = c(0.2, 0.8), 
            shortdims = c(0.85, 0.02), colvec = cols(length(brks) - 1))
            dev.off()

PrincetonUniversity/lmisc documentation built on May 8, 2019, 3:23 a.m.