Learn R Programming

surveillance (version 1.23.1)

layout.labels: Layout Items for spplot

Description

Generate sp.layout items for use by spplot or plot these items directly in the traditional graphics system. Function layout.labels draws labels at the coordinates of the spatial object, and layout.scalebar returns a labeled scale bar.

Usage

layout.labels(obj, labels = TRUE, plot = FALSE)

layout.scalebar(obj, corner = c(0.05, 0.95), scale = 1, labels = c(0, scale), height = 0.05, pos = 3, ..., plot = FALSE)

Value

For layout.labels, a single sp.layout item, which is a list with first element "panel.text" and subsequent elements being arguments to that function based on the labels

specification.

For layout.scalebar, a list of sp.layout items comprising the polygonal scale bar and the labels.

If these layout functions are called with plot = TRUE, the item is plotted directly using traditional graphics functions and NULL is returned.

Arguments

obj

an object inheriting from a "Spatial" class.

labels

specification of the labels. For layout.labels:

  • a FALSE or NULL value omits labels (NULL is returned),

  • labels = TRUE uses row.names(obj),

  • a character or numeric index for a column of obj@data which contains suitable labels,

  • a vector of length length(obj) with labels,

  • or a list of arguments for panel.text, where the optional labels component follows the same rules as above.

For layout.scalebar, a character vector of length two giving the labels to be put above the left and right ends of the scale bar.

corner

the location of the scale bar in the unit square, where c(0,0) refers to the bottom left corner. By default, the scale bar is placed in the top left corner (with a small buffer).

scale

the width of the scale bar in the units of proj4string(obj). If identical(FALSE, is.projected(obj)) (i.e., obj has longlat coordinates), scale is interpreted in kilometres.

height

the height of the scale bar, see layout.scale.bar.

pos

a position specifier for the labels (see text). By default, the labels are plotted above the scale bar.

...

further arguments for panel.text (if plot = FALSE) or text (if plot = TRUE) to change the style of the labels, e.g., cex, col, and font.

plot

logical indicating if the layout item should be plotted using the traditional graphics system. By default (FALSE), a list for subsequent use by spplot is returned.

Author

Sebastian Meyer

Examples

Run this code
if (requireNamespace("sf")) { # required by recent 'sp'

## districts in the Regierungsbezirk Weser-Ems (longlat coordinates)
data("measlesWeserEms")
mapWE <- measlesWeserEms@map
li1 <- layout.labels(mapWE, labels = list(font=2, labels="GEN"))
li2 <- layout.scalebar(mapWE, corner = c(0.05, 0.05), scale = 20,
                       labels = c("0", "20 km"))
spplot(mapWE, zcol = "AREA", sp.layout = c(list(li1), li2),
       col.regions = rev(heat.colors(100)), scales = list(draw = TRUE))

## districts in Bavaria (projected coordinates)
load(system.file("shapes", "districtsD.RData", package = "surveillance"))
bavaria <- districtsD[substr(row.names(districtsD), 1, 2) == "09", ]
sb <- layout.scalebar(bavaria, corner = c(0.75,0.9), scale = 50,
                      labels = c("0", "50 km"), cex = 0.8)
spplot(bavaria, zcol = "POPULATION", sp.layout = sb,
       xlab = "x [km]", ylab = "y [km]", scales = list(draw = TRUE),
       col.regions = rev(heat.colors(100)))

## these layout functions also work in the traditional graphics system
par(mar = c(0,0,0,0))
plot(bavaria, col = "lavender")
layout.scalebar(bavaria, corner = c(0.75, 0.9), scale = 50,
                labels = c("0", "50 km"), plot = TRUE)
layout.labels(bavaria, labels = list(cex = 0.8,
              labels = substr(bavaria$GEN, 1, 3)), plot = TRUE)

}

Run the code above in your browser using DataLab