Update:
OP称,他们需要每个联邦议院的多重肤色。 假设他们有一份有名的清单或有色人的数据框架,他们可以加入其形状档案。 在这方面,我正在制造一套任意的颜色。 我将颜色分配给每个国家(向其中一些国家转让多种颜色)。
Then I would divide each state that has multiple colors to equal parts.
最后,我策划了分裂的多角(没有边界),并将把原地块混凝土块地上,只划定一个白色边界(如果我选择黑色显示这些边界),以显示原来的分区/州边界。 我不敢肯定,为什么背景不奏效。
I 认为这里应当有一个更好的办法,因此,应该有一个放弃。
#### libraries ####
library(sf)
library(sp)
library(dismo)
library(deldir)
library(dplyr)
#### downloading the data ####
download.file("https://geodata.ucdavis.edu/gadm/gadm4.1/shp/gadm41_DEU_shp.zip",
"gadm41_DEU_shp.zip")
unzip("gadm41_DEU_shp.zip", exdir= "DEU_adm")
Germany <- st_read(dsn="DEU_adm\gadm41_DEU_1.shp", quiet = TRUE)
#### pre-processing the data and defining the split funciton ####
## creating a set of random colors, you should use your desired colors
set.seed(123)
cp <- sample(grDevices::colors()[grep( gr(a|e)y|white ,
grDevices::colors(), invert = T)], 24)
Germany$COLORS <- I(list(cp[1], cp[2:3], cp[3:5], cp[6:9],
cp[10], cp[11], cp[12], cp[13],
cp[14], cp[15], cp[16], cp[17],
cp[18], cp[19], cp[20], cp[21:24]))
wgs84 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
## taken from https://gis.stackexchange.com/a/440698/93948
## split polygons
split_poly <- function(sf_poly, n_areas) {
# Create random points
points_rnd <- st_sample(sf_poly, size = 10000)
# k-means clustering
points <- do.call(rbind, st_geometry(points_rnd)) %>%
as_tibble() %>% setNames(c("lon","lat"))
k_means <- kmeans(points, centers = n_areas)
# Create voronoi polygons
voronoi_polys <- dismo::voronoi(k_means$centers, ext = sf_poly)
# Clip to sf_poly
crs(voronoi_polys) <- wgs84 ## hardcoding crs
voronoi_sf <- st_as_sf(voronoi_polys)
equal_areas <- st_intersection(voronoi_sf, sf_poly)
#equal_areas$area <- st_area(equal_areas)
return(equal_areas)
}
#### splitting the polygons and plotting ####
## split polygons for each Bundesländer based on number of colors provided
pol_areas <- lapply(seq_len(nrow(Germany)), function(i)
if (length(unlist(Germany[i,]$COLORS)) == 1) {Germany[i,]}
else {split_poly(Germany[i,], length(unlist(Germany[i,]$COLORS)))})
## combine the splited polygons to one sf object
bind_rows(pol_areas) %>%
rowwise() %>%
mutate(Color = ifelse(is.na(id), COLORS, COLORS[id])) -> mapdata_deu
## plot
plot(mapdata_deu[,"geometry"],
col = mapdata_deu$Color,
bg = "#aadaff", border = NA)
plot(Germany[,"geometry"],
col = NA,
bg = NA, border = "black", add = T)
reprex v2.0.2
我并不十分肯定你为每个国家(即联邦议院)所希望的颜色,但下面的法典确实做了工作。 您可按要求修改<代码>Colors。
library(sf)
library(sp)
library(raster)
library(rgdal)
Germany <- getData(country = "Germany", level = 1)
Colors <- c("#D7191C", "#FDAE61", "#FFFFBF", "#ABDDA4",
"#D7191C", "#FDAE61", "#ABDDA4", "#D7191C",
"#FDAE61", "#FFFFBF", "#ABDDA4", "#2B83BA",
"#D7191C", "#FDAE61", "#ABDDA4", "#D7191C")
plot(Germany,
col = Colors[as.numeric(as.factor(Germany$NAME_1))],
border = "white", bg = "#aadaff")
https://i.imgur.com/v8Uy8Ij.png” alt=">
Created on 2024-03-24 with reprex v2.0.2.