在R中,我如何将SpatialPolygons *转换为映射对象 [英] In R, how can I convert SpatialPolygons* to map objects

查看:311
本文介绍了在R中,我如何将SpatialPolygons *转换为映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图利用 ProportionalSymbolMap map =nofollow> JSS论文

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



然而,我通常使用方法返回SpatialPolygonDataFrame。有没有什么软件包或方法可以帮到你?

去做吧。我只是使用这组命令拆分SpatialPolygons *对象,并将它重新放在一起,作为类 map 的对象。我希望你喜欢它:

 #正常读入shapefile SpatialPolygons 
xx< - readShapePoly(system.file (shapes / sids.shp,package =maptools)[1],IDvar =FIPSNO,proj4string = CRS(+ proj = longlat + ellps = clrk66))

#格式化数据
需要(重新设置)
#标识符列将数据分割到
xx @ data $ id < - rownames(xx @ data)

#转换到数据帧
xx.df< - as.data.frame(xx)

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

#连接操作 - 每个坐标向量一行
xx< - 连接(xx.fort,xx.df,by =id)

#按ID分割,因为我们需要在每个多边形坐标集的末端添加NA以折断该行
xxSp< - split(xx,xx $ id)

#需要在每个多边形形状的末端插入NA以切断该形状
xxL < - do.call(rbind,(lapply(xxSp,function(x){j < - x [nrow(x),] ; j [1:2] < - c(NA,NA); rbind(x,j)})))


#创建与地图对象具有相同结构的列表对象
xxMap < - list(x = xxL $ long,y = xxL $ lat,range = c(range(xxL $ long),range(xxL $ lat) ),names = as.character(unique(xxL $ NAME)))

#定义为地图类对象
attr(xxMap,class)< - map

#剧情!
map(xxMap)


I'm trying to make use of ProportionalSymbolMap map as defined in this JSS paper.

In order to plot proportional symbols I first need an object of map class.

The methods I normally use however return SpatialPolygonDataFrame. Is there any package or method that could be of help here?

解决方案

Hack is the way to go about it. I just used this set of commands to take apart a SpatialPolygons* object and put it back together again as an object of class map. I hope you like it:

# 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 )

这篇关于在R中,我如何将SpatialPolygons *转换为映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆