目的:
在加泰罗尼亚使用人口创建市政区域的地图。
可复制的数据:
好的,所以我的第一步是下载人口和市政府shapefile。
人口:https://www.idescat.cat/cat/idescat/biblioteca/docs/publicacions/gridpoblacio01012016.zip
市政边界:http://auriga.icc.cat/bseccen_etrs89/bseccenv10sh1f1_2002a2016_0.zip
到目前为止的步骤:
导入两者,给它们相同的坐标:
catapop<-readOGR("location","rp2016_qtree_level2_ofus_allvar")
catasense<-readOGR("location","bseccenv10sh1f1_20160101_0")
catapop<-spTransform(catapop,CRSobj = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+towsgs84=0,0,0")
catasense<-spTransform(catasense,CRSobj = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+towsgs84=0,0,0")
问题
当我查看shapefile中的数据时,它们包含了我所需的内容:
但是,当我查看catasense中的ID与catapop中的ID相比时,我不知道发生了什么以及如何将catapop的ID与Catsense相匹配。
我想保留触觉识别码“MUNICIPI”,因为它们似乎是加泰罗尼亚公共数据中最标准的。
任何有关如何匹配ID并为人口“TOTAL”创建一个叶绿素图的想法,将不胜感激!
如果需要任何澄清,请告诉我!
最佳答案
library(readxl)
pop <- read_excel("Downloads/rp2016/pop.xlsx")
cod <- read_excel("Downloads/rp2016/cod.xlsx")
names(cod) <- c("Codi", "Nom2", "Codi comarca", "Nom comarca")
codf <- merge(cod, pop, by.x = "Nom2", by.y = "Nom", all.x = TRUE)
#I make a treatment in the Codi field to put 0 in front of a code that starts with 8.
b <- codf$Codi
b[grep("^8", b)] <- paste0("0",b[grep("^8", b)])
codf$Codi <- b
data2 <- catasense@data
codf2 <- merge(data2, codf, by.x = "MUNICIPI", by.y = "Codi", all.x = TRUE, sort = FALSE)
catasense@data$pop <- codf2$`Població (2016)`
catasense@data$name <- codf2$Nom2
library(leaflet)
pal <- colorNumeric("viridis", NULL)
map <- leaflet(catasense) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 1,
fillColor = ~pal(log10(as.numeric(pop))),
popup = ~paste0("<b>", name, "</b>", " <br> ", "pop:", pop, "<br>"
),
label = ~paste0(name),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE)) %>%
addLegend(pal = pal, values = ~log10(pop), opacity = 1.0,
labFormat = labelFormat(transform = function(x) round(10^x)))
map
#you can save leaflet map in html
library(htmlwidgets)
saveWidget(map, file="cata2.html")
download and open this html file and see the map
关于r - 合并两个空间对象的ID以创建一个Choropleth贴图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49378619/