Description Usage Arguments Details Value Examples
Aggregate spatial polygons, spatial lines or raster objects. For spatial polygons and lines, the units will be merged with the by
variable. For rasters, the fact
parameter determined how many rasters cells are aggregated both horizontally and vertically. Per data variable, an aggregation formula can be specified, by default mean for numeric and modal for categorical varaibles. Note that this function supports sf
objects, but still uses sp-based methods (see details).
1 2 |
shp |
shape object, which is one of
|
by |
variable by which polygons or lines are merged. Does not apply to raster objects. |
fact |
number that specifies how many cells in both horizontal and vertical direction are merged. Only applied to raster objects. |
agg.fun |
aggregation function(s). One of the following formats:
These predefined functions can be used: |
weights |
name of a numeric variable in |
na.rm |
passed on to the aggregation function(s) |
... |
other arguments passed on to the aggregation function(s) |
This function is similar to aggregate
from the raster
package. However, the aggregation can be specified in more detail: weights can be used (e.g. polygon area sizes). Also, an aggregation function can be specified per variable or raster layer. It is also possible to specify a general function for numeric data and a function for categorical data.
By default, the data is not aggregated. In this case, this function is similar to unionSpatialPolygons
from the maptools
package. The only difference is way the aggregate-by variable is specified. When using unionSpatialPolygons
, the values have to be assigned to IDs
whereas when using aggregate_map
the data variable name can be assigned to by
.
The underlying functions of aggregate_map
for sp
objects are gUnaryUnion
, gUnionCascaded
, and gLineMerge
. For Raster
objects, the aggregate
is used.
This function supports sf
objects, but still uses sp-based methods, from the packages sp, rgeos, and/or rgdal. Alternatively, the tidyverse
methods group_by
and summarize
can be used.
A shape object, in the same format as shp
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 | ## Not run:
if (require(tmap) && packageVersion("tmap") >= "2.0") {
data(land)
# original map
qtm(land, raster="cover_cls")
# map decreased by factor 4 for each dimension
land4 <- aggregate_map(land, fact=4, agg.fun="modal")
qtm(land4, raster="cover_cls")
# map decreased by factor 8, where the variable trees is
# aggregated with mean, min, and max
land_trees <- aggregate_map(land, fact=8,
agg.fun=list(trees="mean", trees="min", trees="max"))
tm_shape(land_trees) +
tm_raster(c("trees.1", "trees.2", "trees.3"), title="Trees (%)") +
tm_facets(free.scales=FALSE) +
tm_layout(panel.labels = c("mean", "min", "max"))
data(NLD_muni, NLD_prov)
# aggregate Dutch municipalities to provinces
NLD_prov2 <- aggregate_map(NLD_muni, by="province",
agg.fun = list(population="sum", origin_native="mean", origin_west="mean",
origin_non_west="mean", name="modal"), weights = "population")
# see original provinces data
as.data.frame(NLD_prov)[, c("name", "population", "origin_native",
"origin_west", "origin_non_west")]
# see aggregates data (the last column corresponds to the most populated municipalities)
sf::st_set_geometry(NLD_prov2, NULL)
# largest municipalities in area per province
NLD_largest_muni <- aggregate_map(NLD_muni, by="province",
agg.fun = list(name="modal"), weights = "AREA")
sf::st_set_geometry(NLD_largest_muni, NULL)
}
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.