Learn R Programming

tmaptools (version 2.0-2)

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, target = "metric", total.area = NULL,
  suffix = NA, drop = TRUE)

Arguments

shp

a shape object, i.e., an sf object or a SpatialPolygons(DataFrame)

var

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

target

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

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.

Examples

Run this code
# NOT RUN {
if (require(tmap) && packageVersion("tmap") >= "2.0") {
    data(NLD_muni)

    NLD_muni_pop_per_km2 <- calc_densities(NLD_muni,
        target = "km 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_women_km^2", "pop_women_km^2"),
            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