addContour: Add contourlines to a map

View source: R/addContour.R

addContourR Documentation

Add contourlines to a map

Description

Convenience function to add contourlines to a map, specifically geared towards suggesting boundaries to the result of weightedMap. Internally based on a krige-interpolation.

Usage

addContour(heights, points, window, crs, add = TRUE, polygons = FALSE,
           levels = c(0.4, 0.45, 0.5), grid = 50000, ...)

Arguments

heights

Numeric vector with the same length as points. Typically a 0/1 vector describing presence or absence of a features.

points

Locations of the datapoints as sfc_POINTS or an sf object with such a geometry.

window

Window for the interpolation as sfc_POLYGON or an sf object with such a geometry.

crs

A crs in WKT format.

add

By default, contourlines are added to the previous plot. Otherwise, they are returns as sfc_LINESTRINGS or as polygons (see next).

polygons

Should polygons be returned instead of linestrings? Only applicable when add = FALSE.

levels

Levels on which to draw the contourlines. Multiple lines get thicker towards higher values to suggest a center. These levels have to be related relative to the heights.

grid

Number of points inside the window for the krige-interpolation. Higher numbers lead to nicers contourlines, but take longer to evaluate.

...

Additional specifications passed internally to contour.

Details

Internally, a grid is made inside the window and the height is interpolated using ordinary kriging from [gstat]{krige} with a model suggested by autofitVariogram.

Value

Contourlines are added to the current plot or (when add=FALSE) they are returned as an sf object with a sfc_LINESTRING geometry.

Note

This is a preliminary convenience function that will be used to overhaul levelMap

Author(s)

Michael Cysouw <cysouw@mac.com>

See Also

weightedMap for more involved example

Examples

data(hessen)

# continuous variable between 0 and 1
data <- hessen$data[,1:3]
heights <- round(data[,1]/rowSums(data), digits = 1)
cols <- heat.colors(11)
names(cols) <- names(table(heights))

# boundary as sf
w <- sf::st_as_sf(hessen$boundary)
sf::st_crs(w) <- 4326
w <- sf::st_transform(w, 2397)

# points as sf
p <- sf::st_as_sf(hessen$villages, coords = c("longitude", "latitude"))
sf::st_crs(p) <- 4326
p <- sf::st_transform(p, 2397)

# plot map
plot(sf::st_geometry(p), col = cols[as.character(heights)], pch = 19)
plot(sf::st_geometry(w), add = TRUE, border = "grey")

# add boundary
addContour(heights, points = p, window = w, crs = 2397, grid = 1000,
            levels = c(0.25, 0.35, 0.45, 0.55), col = "blue")

cysouw/qlcVisualize documentation built on Sept. 29, 2024, 2:47 p.m.