我有一个关于尼日利亚不同行政级别的人口估计的档案,我想用它制作一张地图。
我使用了制图包,并尝试了以下步骤
library(cartogram)
admin_lvl2_cartogram <- cartogram(admin_level2_shape, "mean", itermax=5)然而,这给出了一个错误,说明" error :使用非投影地图。此函数不能为经度/纬度数据提供正确的质心和距离:使用"st_transform()“将坐标转换为另一个投影。”我不知道怎么解决这个问题
重新创建初始数据
使用wopr包下载数据
library(wopr)
catalogue <- getCatalogue()
# Select files from the catalogue by subsetting the data frame
selection <- subset(catalogue,
country == 'NGA' &
category == 'Population' &
version == 'v1.2')
# Download selected files
downloadData(selection)手动解压缩下载的zip文件(NGA_population_v1_2_admin.zip)并读取数据
library(rgdal)
library(here)
admin_level2_shape <- readOGR(here::here("wopr/NGA/population/v1.2/NGA_population_v1_2_admin/NGA_population_v1_2_admin_level2_boundaries.shp"))发布于 2021-01-30 21:23:26
spTransform包中的函数sp可能是最简单的,因为readOGR调用返回在该包中定义的空间多边形。
这里有一个完整的例子,转换成适合尼日利亚的投影,"+init=epsg:26331"。你可能得用谷歌来找出符合你需求的确切答案。
#devtools::install_github('wpgp/wopr')
library(wopr)
library(cartogram)
library(rgdal)
library(sp)
library(here)
catalogue <- getCatalogue()
# Select files from the catalogue by subsetting the data frame
selection <- subset(catalogue, country == 'NGA' & category == 'Population' & version == 'v1.2')
# Download selected files
downloadData(selection)
unzip(here::here("wopr/NGA/population/v1.2/NGA_population_v1_2_admin.zip"),
overwrite = T,
exdir = here::here("wopr/NGA/population/v1.2"))
admin_level2_shape <- readOGR(here::here("wopr/NGA/population/v1.2/NGA_population_v1_2_admin/NGA_population_v1_2_admin_level2_boundaries.shp"))
transformed <- spTransform(admin_level2_shape, CRS("+init=epsg:26331"))
admin_lvl2_cartogram <- cartogram(transformed, "mean", itermax=5)我承认我对特定的包一无所知,所以我不知道所产生的内容是否正确,但至少它是转换的。
https://stackoverflow.com/questions/65660080
复制相似问题