首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用tm_shape时找不到对象'x‘,但存在对象吗?

使用tm_shape时找不到对象'x‘,但存在对象吗?
EN

Stack Overflow用户
提问于 2022-09-29 18:30:04
回答 1查看 65关注 0票数 0

我试图用tmap制作地图,显示每个国家的人权指数得分。tmap只有几个可以使用的形状(新西兰的“nz”,整个世界的“world”,还有一两种),所以我使用的是一个来自rnaturalearth的shapefile,它与我现有的数据文件合并,这样我就可以使用自然土形状上的数据制作地图。然而,当我使用函数tm_shape(Afghanistan)tm(fill)绘制阿富汗的基本地图时,我会得到错误。

代码语言:javascript
复制
Error in as.list.environment(environment()) : 
  object 'Afghanistan' not found

下载rnaturalearth shapefile的代码,我将其命名为“country50”。

代码语言:javascript
复制
install.packages("rnaturalearth")
install.packages("rnaturalearthdata")
library(rnaturalearth)
library(rnaturalearthdata) 
country50 <- ne_download(scale = 50, type = 'countries', category = 'cultural')

将“country50”与我先前在“NAME_LONG”中存在的数据合并,创建“ciri2014”。

代码语言:javascript
复制
ciri2014.country50.merged = merge(country50, CIRI_Data_1981_2011_2014_04_14, by = 'NAME_LONG', duplicateGeoms = TRUE)

此时,这应该可以工作,因为变量“阿富汗”在我与'country50‘合并的原始数据文件中,但是我得到了以下内容

代码语言:javascript
复制
tm_shape(Afghanistan) + tm_fill()
 Error in as.list.environment(environment()) : 
  object 'Afghanistan' not found

然后,我意识到‘ciri2014. realized 50合并’是一个S4文件,所以我这样做是为了使它成为sf文件。

代码语言:javascript
复制
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. 

接下来,我做了这个

代码语言:javascript
复制
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找不到它。我也尝试了国家代码和缩写的多个国家,有相同的结果。我能做的唯一地图是新西兰或者整个世界。我是不是错过了一些显而易见的痛苦?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-09-29 19:48:21

您没有一个名为Afghanistan的“对象”--您有一个名为country50的“对象”,它有一个代表阿富汗的一行(或一个特性)。若要绘制此图,请将其解压缩到一个具有您想要的名称的对象中,Afghanistan将这样做,并将其映射为:

代码语言:javascript
复制
> Afghanistan = country50[country50$NAME=="Afghanistan",]
> library(tmap);tm_shape(Afghanistan) + tm_fill()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73900152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档