简体   繁体   English

如何从指定为列表数据类型的空间多边形在R中创建地图

[英]How to create map in R from spatial polygon given as list data type

I have a list (imported from Stata dta file) containing entries defining spacial polygons for a map, to give an idea: 我有一个列表(从Stata dta文件导入),其中包含为地图定义空间多边形的条目,以给出一个想法:

> typeof(aux3)
[1] "list"
> mode(aux3)
[1] "list"
> head(aux3)
  _ID        _X       _Y
1   1        NA       NA
2   1 -22.23933 64.56315
3   1 -22.25667 64.56053
4   1 -22.25026 64.56653
5   1 -22.27167 64.57319
6   1 -22.30311 64.55409
> tail(head(aux3,20000))
      _ID       _X       _Y
19995   2 21.21593 60.24696
19996   2 21.21650 60.24337
19997   2 21.23972 60.24913
19998   2 21.22203 60.23304
19999   2 21.21618 60.23332
20000   2 21.22092 60.23930
> #etc.

I would like to convert this into a data structure with which I can work to create a map (without larger difficulty and without needing extensive expertise, never having done that before) in R. I inferred that the SpacialPolygons type of the sp package is the easiest choice. 我想将其转换为数据结构,利用该数据结构可以在R中创建地图(没有更大的难度并且不需要广泛的专业知识,以前从未做过)。我推断出sp包的SpacialPolygons类型是最简单的选择。 Further, from the definition, it seemed that the SpacialPolygons method (defined in this package, see the package's documentation , page 79) is the correct method to convert from list to this data type. 此外,从定义来看,似乎SpacialPolygons方法(在此程序包中定义,请参阅程序包的文档 ,第79页)是将列表转换为该数据类型的正确方法。

Unfortunately, the method is not that straightforward to work with and I need some help. 不幸的是,该方法并不是那么简单,我需要一些帮助。 My (naive) attempt yields an error that I do not understand and that does not turn up any interesting results in a google search: 我的(天真)尝试产生了一个我不理解的错误,并且在Google搜索中没有出现任何有趣的结果:

> library(maptools)
Loading required package: sp
Checking rgeos availability: FALSE
    Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
    which has a restricted licence. It is disabled by default;
    to enable gpclib, type gpclibPermit()
> SP<-SpatialPolygons(aux3)
Error in SpatialPolygons(aux3) : 
  cannot get a slot ("area") from an object of type "integer"

Can the list above be converted to SpacialPolygon? 上面的列表可以转换为SpacialPolygon吗? If so, how? 如果是这样,怎么办? If not, what format should I choose instead? 如果没有,我应该选择哪种格式呢? Thanks. 谢谢。

If you just want to draw the map, you could use ggplot. 如果只想绘制地图,则可以使用ggplot。 I can't test this since I don't have your data, but should work. 由于我没有您的数据,因此无法测试,但应该可以。

library(ggplot2)
aux_df <- data.frame(aux3)
# as per comment: the list-to-dataframe conversion prepends the column names with X
ggplot(aux_df, aes(x=X_X, y=X_Y, group=X_ID) + 
  geom_polygon(color='black', fill=NA) +
  coord_map() +
  theme_classic()

The above assumes the list aux3 has longitude and latitude in _X and _Y and polygons defined by _ID . 上面假设列表aux3_X_Y以及由_ID定义的多边形中具有经度和纬度。 It applies a mercator projection by default, but you can change this using different arguments to coord_map() . 默认情况下,它会应用墨卡托投影,但您可以使用不同的参数来更改此coord_map()

Here's a working example using a map of ohio counties (arbitrary choice). 这是一个使用俄亥俄州地图(任意选择)的可行示例。

library(ggplot2)
ohio_poly <- map_data('county', region='ohio')
ggplot(ohio_poly, aes(x=long, y=lat, group=group)) +
  geom_polygon(color='black', fill=NA) +
  coord_map() +
  theme_classic()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM