【问题标题】:In R, how can I convert SpatialPolygons* to map objects在 R 中,如何将 SpatialPolygons* 转换为地图对象
【发布时间】:2013-04-23 10:34:34
【问题描述】:

我正在尝试使用此JSS paper 中定义的ProportionalSymbolMap 映射。

为了绘制比例符号,我首先需要一个地图类的对象。

methods 我通常使用但返回 SpatialPolygonDataFrame。这里有什么包或方法可以帮助吗?

【问题讨论】:

  • 你用的是什么包?
  • @SimonO101 我希望在 ggplot 中实现比例符号映射,但我愿意接受任何解决方案。

标签: r ggplot2 gis geospatial spatial


【解决方案1】:

Hack 是解决问题的方法。我只是使用这组命令来拆开一个 SpatialPolygons* 对象,然后将其重新组合在一起作为 map 类的对象。我希望你喜欢它:

# read in shapefile as normal SpatialPolygons
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

# Formatting the data
require(reshape)
# Identifier column to split data on
xx@data$id <- rownames(xx@data)

# Convert to dataframe
xx.df <- as.data.frame(xx)

#Fortfy automagic
xx.fort <- fortify(xx, region="id")

# Join operation - one row per coordinate vector
xx <- join(xx.fort, xx.df,by="id")

# Split by ID because we need to add NA at end of each set of polygon coordinates to 'break' the line
xxSp <- split(xx, xx$id)

# Need to insert NA at end of each polygon shape to cut off that shape
xxL <- do.call( rbind , (lapply( xxSp , function(x) { j <- x[ nrow(x) , ] ; j[1:2] <- c(NA,NA); rbind( x , j ) })) )


# Create list object with same structure as map object
xxMap <- list( x = xxL$long , y = xxL$lat , range = c( range(xxL$long) , range(xxL$lat) ) , names = as.character(unique( xxL$NAME ) ) )

# Define as a map class object
attr(xxMap , "class") <- "map"

# Plot!!
map( xxMap )

【讨论】:

  • @radek 您还需要什么东西才能让这个解决方案对您有用吗?干杯。
  • 抱歉回复晚了。为此非常感谢。它似乎确实有效,但是转换后的地图上有一些奇怪的文物。如果将其与原始文件进行比较 - 地图左边缘的多边形之间存在一些奇怪的连接。可能是什么原因造成的?
  • @radek 我没有注意到那些 - 可能是多边形中的孔,但几乎可以肯定与 x/y 坐标的排列有关。如果我有时间,我会进一步研究。对你来说,shapefile 可以吗?还是你有同样的问题?
  • 到目前为止,仅使用您的示例对其进行了测试。我怀疑它也可能会导致我的数据出现问题,因为我使用非常复杂的管理边界(孔、岛、多部分特征等)。会尽快试一试!
  • @radek 如果我有空的话,我今晚会再看一遍。干杯!
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
相关资源
最近更新 更多