if (FALSE) {
library(sf)
library(tmap)
## Load the Spain colorectal cancer mortality data ##
data(Carto_SpainMUN)
## Define a spatial partition using the DBSC algorithm ##
Carto_SpainMUN$logSMR <- log(Carto_SpainMUN$obs/Carto_SpainMUN$exp+0.0001)
carto.new <- clustering_partition(carto=Carto_SpainMUN, ID.area="ID", var="logSMR",
n.cluster=20, l=2, min.size=100, verbose=TRUE)
table(carto.new$ID.group)
## Plot of the grouping variable 'ID.group' ##
carto.data <- st_set_geometry(carto.new, NULL)
carto.partition <- aggregate(carto.new[,"geometry"], list(ID.group=carto.data[,"ID.group"]), head)
tmap4 <- packageVersion("tmap") >= "3.99"
if(tmap4){
tm_shape(carto.new) +
tm_polygons(fill="ID.group", fill.scale=tm_scale(values="brewer.set3")) +
tm_shape(carto.partition) +
tm_borders(col="black", lwd=2) +
tm_layout(legend.outside=TRUE, legend.frame=FALSE)
}else{
tm_shape(carto.new) +
tm_polygons(col="ID.group") +
tm_shape(carto.partition) +
tm_borders(col="black", lwd=2) +
tm_layout(legend.outside=TRUE)
}
}
Run the code above in your browser using DataLab