我试图用tmap制作地图,显示每个国家的人权指数得分。tmap只有几个可以使用的形状(新西兰的“nz”,整个世界的“world”,还有一两种),所以我使用的是一个来自rnaturalearth的shapefile,它与我现有的数据文件合并,这样我就可以使用自然土形状上的数据制作地图。然而,当我使用函数tm_shape(Afghanistan)和tm(fill)绘制阿富汗的基本地图时,我会得到错误。
Error in as.list.environment(environment()) :
object 'Afghanistan' not found下载rnaturalearth shapefile的代码,我将其命名为“country50”。
install.packages("rnaturalearth")
install.packages("rnaturalearthdata")
library(rnaturalearth)
library(rnaturalearthdata)
country50 <- ne_download(scale = 50, type = 'countries', category = 'cultural')将“country50”与我先前在“NAME_LONG”中存在的数据合并,创建“ciri2014”。
ciri2014.country50.merged = merge(country50, CIRI_Data_1981_2011_2014_04_14, by = 'NAME_LONG', duplicateGeoms = TRUE)此时,这应该可以工作,因为变量“阿富汗”在我与'country50‘合并的原始数据文件中,但是我得到了以下内容
tm_shape(Afghanistan) + tm_fill()
Error in as.list.environment(environment()) :
object 'Afghanistan' not found然后,我意识到‘ciri2014. realized 50合并’是一个S4文件,所以我这样做是为了使它成为sf文件。
st_as_sf(ciri2014.country50.merged)
Simple feature collection with 5492 features and 195 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961
Geodetic CRS: WGS 84
First 10 features:
# and so on. 接下来,我做了这个
sf.ciri2014.country50.merged = st_as_sf(ciri2014.country50.merged)
> View(sf.ciri2014.country50.merged)
> tm_shape(Afghanistan) + tm_fill()
Error in as.list.environment(environment()) :
object 'Afghanistan' not found
> map.sf.ciri2014.country50.merged = st_as_sf(ciri2014.country50.merged, fill = TRUE, group = TRUE)
> View(map.sf.ciri2014.country50.merged)
> tm_shape(Afghanistan) + tm_fill()
Error in as.list.environment(environment()) :
object 'Afghanistan' not found“阿富汗”这个物体肯定存在,但我不知道为什么tmap找不到它。我也尝试了国家代码和缩写的多个国家,有相同的结果。我能做的唯一地图是新西兰或者整个世界。我是不是错过了一些显而易见的痛苦?谢谢!
发布于 2022-09-29 19:48:21
您没有一个名为Afghanistan的“对象”--您有一个名为country50的“对象”,它有一个代表阿富汗的一行(或一个特性)。若要绘制此图,请将其解压缩到一个具有您想要的名称的对象中,Afghanistan将这样做,并将其映射为:
> Afghanistan = country50[country50$NAME=="Afghanistan",]
> library(tmap);tm_shape(Afghanistan) + tm_fill()

https://stackoverflow.com/questions/73900152
复制相似问题