在R中,如何将SpatialPolygons *转换为地图对象

时间:2013-04-23 10:34:34

标签: r ggplot2 gis geospatial spatial

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

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

我通常使用的methods会返回SpatialPolygonDataFrame。是否有任何可以提供帮助的包裹或方法?

1 个答案:

答案 0 :(得分:4)

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 )

enter image description here