Learn R Programming

tmap (version 1.6-1)

calc_densities: Calculate densities

Description

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

Usage

calc_densities(shp, var, unit = "km", unit.size = 1000, total.area = NA, suffix = "", drop = TRUE)

Arguments

shp
a shape object, i.e., a SpatialPolygons(DataFrame)
var
name(s) of a qualtity variable name contained in the shp data
unit
the preferred unit, for instance, "km", "m", or "miles". Density values are calculated in var/unit^2.
unit.size
size of the unit in terms of coordinate units. The coordinate system of many projections is approximately in meters while thematic maps typically range many kilometers, so by default unit="km" and unit.size=1000 (meaning 1 kilometer equals 1000 coordinate units).
total.area
total area size of shp in number of squared units (by default kilometers). Useful if the total area of the shp differs from a reference total area value.
suffix
character that is appended to the variable names. The resulting names are used as column names of the returned data.frame.
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.

Examples

Run this code
data(NLD_muni)

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

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

Run the code above in your browser using DataLab