Extract feature coordinates from SpatialPolygons and other sp classes

 ̄綄美尐妖づ 提交于 2019-12-03 23:40:10

The best way I can think of is to use the fortify function from ggplot2. fortify is a generic function which has methods to convert generic R objects (e.g. lm, etc) to a data.frame which ggplot2 can use for plotting. A full list gives:

> ggplot2:::fortify.
ggplot2:::fortify.cld                                                                   
ggplot2:::fortify.confint.glht                                                          
ggplot2:::fortify.data.frame                                                            
ggplot2:::fortify.default                                                               
ggplot2:::fortify.glht                                                                  
ggplot2:::fortify.Line
ggplot2:::fortify.Lines
ggplot2:::fortify.lm
ggplot2:::fortify.map
ggplot2:::fortify.NULL
ggplot2:::fortify.Polygon
ggplot2:::fortify.Polygons
ggplot2:::fortify.SpatialLinesDataFrame
ggplot2:::fortify.SpatialPolygons
ggplot2:::fortify.SpatialPolygonsDataFrame
ggplot2:::fortify.summary.glht

You can see this includes a fortify function for SpatialPolygons* objects. Using your example data:

> obj = fortify(SpP)
   long lat order  hole piece  group   id
1     2   2     1 FALSE     1   s1.1   s1
2     1   4     2 FALSE     1   s1.1   s1
3     4   5     3 FALSE     1   s1.1   s1
4     4   3     4 FALSE     1   s1.1   s1
5     2   2     5 FALSE     1   s1.1   s1
6     5   2     1 FALSE     1   s2.1   s2
7     2   2     2 FALSE     1   s2.1   s2
8     4   3     3 FALSE     1   s2.1   s2
9     5   2     4 FALSE     1   s2.1   s2
10    4   5     1 FALSE     1 s3/4.1 s3/4
11   10   5     2 FALSE     1 s3/4.1 s3/4
12    5   2     3 FALSE     1 s3/4.1 s3/4
13    4   3     4 FALSE     1 s3/4.1 s3/4
14    4   5     5 FALSE     1 s3/4.1 s3/4
15    5   4     6  TRUE     2 s3/4.2 s3/4
16    5   3     7  TRUE     2 s3/4.2 s3/4
17    6   3     8  TRUE     2 s3/4.2 s3/4
18    6   4     9  TRUE     2 s3/4.2 s3/4
19    5   4    10  TRUE     2 s3/4.2 s3/4

and plotting the result:

require(ggplot2); theme_set(theme_bw())
ggplot(aes(x = long, y = lat, group = group), data = obj) + geom_path()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!