calc_densities: Calculate densities

Description Usage Arguments Value References Examples

View source: R/calc_densities.R

Description

Transpose quantitative variables to density variables, which are often needed for choropleths. For example, the colors of a population density map should correspond population density counts rather than absolute population numbers.

Usage

1
2
calc_densities(shp, var, target = "metric", orig = NA, to = NA,
  total.area = NA, suffix = NA, drop = TRUE)

Arguments

shp

a shape object, i.e., a SpatialPolygons(DataFrame), or a sf object that can be coerced as such.

var

name(s) of a quantitative variable name contained in the shp data

target

the target unit, see approx_areas. Density values are calculated in var/target^2.

orig

original units, i.e. by which the coordinates are defined, see approx_areas.

to

multiplier used as follows: orig * to = target. See approx_areas.

total.area

total area size of shp in number of target units (defined by unit), approx_areas.

suffix

character that is appended to the variable names. The resulting names are used as column names of the returned data.frame. By default, _sq_<target>, where target corresponds to the target unit, e.g. _sq_km

drop

boolean that determines whether an one-column data-frame should be returned as a vector

Value

Vector or data.frame (depending on whether length(var)==1 with density values. This can be appended directly to the shape file with append_data with fixed.order=TRUE.

References

Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39, DOI

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if (require(tmap)) {
    data(NLD_muni)

    NLD_muni_pop_per_km2 <- calc_densities(NLD_muni, target = "km", var = c("pop_men", "pop_women"))
    NLD_muni <- append_data(NLD_muni, NLD_muni_pop_per_km2, fixed=TRUE)

    tm_shape(NLD_muni) +
    	tm_polygons(c("pop_men_sq_km", "pop_women_sq_km"),
            title=expression("Population per " * km^2), style="quantile") +
    tm_facets(free.scales = FALSE) +
    tm_layout(panel.show = TRUE, panel.labels=c("Men", "Women"))
}

tmaptools documentation built on April 10, 2018, 9:07 a.m.