Description Usage Arguments Details Value Examples
Create a smooth map from a shape object. A 2D kernel density estimator is applied to the shape, which can be a spatial points, polygons, or raster object. Various format are returned: a smooth raster, contour lines, and polygons. The covered area can be specified, i.e., the area outside of it is extracted from the output. Note that this function supports sf
objects, but still uses sp-based methods (see details).
1 2 3 4 5 6 | smooth_map(shp, var = NULL, nrow = NA, ncol = NA, N = 250000,
unit = "km", unit.size = 1000, smooth.raster = TRUE, nlevels = 5,
style = ifelse(is.null(breaks), "pretty", "fixed"), breaks = NULL,
bandwidth = NA, threshold = 0, cover.type = NA, cover = NULL,
cover.threshold = 0.6, weight = 1, extracting.method = "full",
buffer.width = NA, to.Raster = NULL)
|
shp |
shape object of class |
var |
variable name. Not needed for |
nrow |
number of rows in the raster that is used to smooth the shape object. Only applicable if shp is not a |
ncol |
number of rows in the raster that is used to smooth the shape object. Only applicable if shp is not a |
N |
preferred number of points in the raster that is used to smooth the shape object. Only applicable if shp is not a |
unit |
unit specification. Needed when calculating density values. When set to |
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 |
smooth.raster |
logical that determines whether 2D kernel density smoothing is applied to the raster shape object. Not applicable when |
nlevels |
preferred number of levels |
style |
method to cut the color scale: e.g. "fixed", "equal", "pretty", "quantile", or "kmeans". See the details in |
breaks |
in case |
bandwidth |
single numeric value or vector of two numeric values that specifiy the bandwidth of the kernal density estimator. By default, it is 1/50th of the shortest side in units (specified with |
threshold |
threshold value when a 2D kernel density is applied. Density values below this threshold will be set to |
cover.type |
character value that specifies the type of raster cover, in other words, how the boundaries are specified. Options: |
cover |
|
cover.threshold |
numeric value between 0 and 1 that determines which part of the estimated 2D kernal density is returned as cover. Only applicable when |
weight |
single number that specifies the weight of a single point. Only applicable if |
extracting.method |
Method of how coordinates are extracted from the kernel density polygons. Options are: |
buffer.width |
Buffer width of the iso lines to cut kernel density polygons. Should be small enough to let the polygons touch each other without space in between. However, too low values may cause geometric errors. |
to.Raster |
not used anymore, since the "raster" output is always a |
For the estimation of the 2D kernal density, code is borrowed from bkde2D
. This implemention is slightly different: bkde2D
takes point coordinates and applies linear binning, whereas in this function, the data is already binned, with values 1 if the values of var
are not missing and 0 if values of var
are missing.
This function supports sf
objects, but still uses sp-based methods, from the packages sp, rgeos, and/or rgdal.
List with the following items:
"raster"
A smooth raster, which is either a SpatialGridDataFrame
or a RasterLayer
(see to.Raster
)
"iso"
Contour lines, which is an sf
object of spatial lines.
"polygons"
Kernel density polygons, which is an sf
object of spatial polygons
"bbox"
Bounding box of the used raster
"ncol"
Number of rows in the raster
"nrow"
Number of columns in the raster
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | ## Not run:
if (require(tmap)) {
# set mode to plotting mode
current.mode <- tmap_mode("plot")
####################################
## Already smoothed raster
####################################
vol <- raster::raster(t(volcano[, ncol(volcano):1]), xmn=0, xmx=870, ymn=0, ymx=610)
vol_smooth <- smooth_map(vol, smooth.raster = FALSE, nlevels = 10)
tm_shape(vol_smooth$polygons) +
tm_fill("level", palette=terrain.colors(11), title="Elevation") +
tm_shape(vol_smooth$iso) +
tm_iso(col = "black", size = .7, fontcolor="black") +
tm_layout("Maunga Whau volcano (Auckland)", title.position=c("left", "bottom"),
inner.margins=0) +
tm_legend(width=.13, position=c("right", "top"), bg.color="gray80", frame = TRUE)
####################################
## Smooth polygons
####################################
data(NLD_muni)
NLD_muni$population_dens <- as.vector(calc_densities(NLD_muni, "population"))
qtm(NLD_muni, fill="population_dens")
NLD_smooth <- smooth_map(NLD_muni, var = "population_dens")
qtm(NLD_smooth$raster, style="grey")
qtm(NLD_smooth$polygons, format="NLD")
####################################
## Smooth points
####################################
# Approximate world population density as spatial points, one for each 1 million people,
# in the following way. Each metropolitan area of x million people will be represented
# by x dots. The remaining population per country will be represented by dots that are
# sampled across the country.
create_dot_per_1mln_people <- function() {
data(World, metro)
metro_eck <- set_projection(metro, projection = "eck4")
# aggregate metropolitan population per country
metro_per_country <- tapply(metro_eck$pop2010, INDEX = list(metro_eck$iso_a3), FUN=sum)
metro_per_country_in_World <- metro_per_country[names(metro_per_country) %in% World$iso_a3]
# assign to World shape
World$pop_metro <- 0
World$pop_metro[match(names(metro_per_country_in_World), World$iso_a3)] <-
metro_per_country_in_World
# define population density other than metropolitan areas
World$pop_est_dens_non_metro <- (World$pop_est - World$pop_metro) / World$area
# generate dots for metropolitan areas (1 dot = 1mln people)
repeats <- pmax(1, metro_eck$pop2010 %/% 1e6)
ids <- unlist(mapply(rep, 1:nrow(metro_eck), repeats, SIMPLIFY = FALSE))
metro_dots <- metro_eck[ids, ]
# sample population dots from non-metropolitan areas (1 dot = 1mln people)
World_pop <- sample_dots(World, vars="pop_est_dens_non_metro", w = 1e6,
npop = 7.3e9 - nrow(metro_dots)*1e6)
# combine
c(st_geometry(World_pop), st_geometry(metro_dots))
}
World_1mln_dots <- create_dot_per_1mln_people()
# dot map
tm_shape(World_1mln_dots) + tm_dots()
# create smooth map
World_list <- smooth_map(World_1mln_dots, cover = World, weight=1e6)
# plot smooth raster map
qtm(World_list$raster, style="grey")
# plot smooth raster map
qtm(World, bbox="India") + qtm(World_list$iso)
# plot kernel density map
qtm(World_list$polygons, style="grey", format="World")
####################################
## Smooth raster
####################################
data(land)
land_smooth <- smooth_map(land, var="trees", cover.type = "smooth")
qtm(land, raster="trees")
qtm(land_smooth$raster)
qtm(land_smooth$polygons, format="World", style="grey")
# reset current mode
tmap_mode(current.mode)
}
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.