【发布时间】:2014-06-02 00:29:32
【问题描述】:
我正在尝试将 R 中的空间多边形对象转换为地图对象。我已经设法使用以前回答的问题来做到这一点,最具体地说是 (In R, how can I convert SpatialPolygons* to map objects),但我遇到了与此线程中的人相同的问题:我得到奇怪的多边形链接,导致地图无法使用。
我在下面包含了完整的复制代码。
感谢您的帮助。
library(sp)
library(spdep)
library(maps)
library(rgdal)
## load a file from GADM (you just have to specify the countries "special part" of the file name, like "ARG" for Argentina. Optionally you can specify which level you want to have
loadGADM <- function (fileName, level = 0, ...) {
load(url(paste("http://gadm.org/data/rda/", fileName, "_adm", level, ".RData", sep = "")))
gadm
}
## the maps objects get a prefix (like "ARG_" for Argentina)
changeGADMPrefix <- function (GADM, prefix) {
GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
GADM
}
## load file and change prefix
loadChangePrefix <- function (fileName, level = 0, ...) {
theFile <- loadGADM(fileName, level)
theFile <- changeGADMPrefix(theFile, fileName)
theFile
}
## this function creates a SpatialPolygonsDataFrame that contains all maps you specify in "fileNames".
## E.g.:
## spdf <- getCountries(c("ARG","BOL","CHL"))
## plot(spdf) # should draw a map with Brasil, Argentina and Chile on it.
getCountries <- function (fileNames, level = 0, ...) {
polygon <- sapply(fileNames, loadChangePrefix, level)
polyMap <- do.call("rbind", polygon)
polyMap
}
indiamap <- getCountries("IND",level=1)
xx <- indiamap
require(reshape)
xx@data$id <- rownames(xx@data)
# Convert to dataframe
xx.df <- as.data.frame(xx)
#Fortfy automagic
require(ggplot2)
xx.fort <- fortify(xx, region="id")
# Join operation - one row per coordinate vector
require(plyr)
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 )
【问题讨论】:
-
这一行出现错误:
indiamap <- getCountries("IND",level=1)也许我需要加载更多包。 -
当我使用
library(sp)时,上述错误消失了。 -
抱歉——我在复制和粘贴时忘记了那行。我已经加进去了。
-
最后一行出现另一个错误,但是包
sos发现太多可能的包,我无法猜测map函数需要哪个包。 -
再次道歉——我已经更正了(忘记库已经加载到我的编辑器的另一个脚本文件中)
标签: r maps gis geospatial spatial