将R绘图导出到shapefile

6
我对R比ArcView更陌生。我正在绘制一些二模数据,并希望将图形转换为shapefile格式。如果可能的话,我想转换顶点和边缘,以便在ArcView中显示相同的图形及其属性。
我已经安装了“shapefiles”包,看到了convert.to.shapefile命令,但是帮助文档没有说明如何为顶点分配XY坐标。
谢谢,
Tim

1
你的数据目前是什么格式?提供一个示例文件或 dput() 可以帮助我们回答你的问题。 - Ari B. Friedman
1
听起来你想要“线条”,但也许是“多边形”?请至少发布一个描述图和生成它的代码的图片。 - mdsumner
@gsk3,@mdsumner 数据最初是一个包含两列的CSV文件。第一列是一个数字字段,表示一个人的唯一编号。第二列是一个字符字段,表示事件。将其转换为矩阵,然后转换为网络后,得到的图形是由线连接的顶点,这就是我想要的shapefile表示形式。我无法理解有关附加示例的帮助文件,否则我会发送给您一个绘图的PDF文件。 - Tim
1
最简单的包含绘图的方法是将其输出为png格式,然后在编辑帖子时附加图像。包含小对象的最简单方法是使用dput。包含大对象的最简单方法是将它们上传到Dropbox或某个FTP站点并链接到它们。 - Ari B. Friedman
2个回答

4

好的,我做了一些假设,认为您想要给一个二分图分配空间坐标,并将顶点和边作为点形状文件和折线图导出以在 ArcGIS 中使用。

这个解决方案有点不太优雅,但可以制作具有 xmin、ymin 和 xmax、ymax 坐标限制为 -0.5 和 +0.5 的 shapefile。您需要自行决定图形布局算法(例如 Kamada-Kawai),并按照 @gsk3 的建议在将 shapefile 导入 ArcGIS 后将 shapefile 投影到所需的坐标系中。在创建 points.data 和 edge.data 数据帧时,可以添加顶点和边的其他属性。

library(igraph)
library(shapefiles)

# Create dummy incidence matrix
inc <- matrix(sample(0:1, 15, repl=TRUE), 3, 5)
colnames(inc) <- c(1:5) # Person ID
rownames(inc) <- letters[1:3] # Event 

# Create bipartite graph
g.bipartite <- graph.incidence(inc, mode="in", add.names=TRUE)

# Plot figure to get xy coordinates for vertices
tk <- tkplot(g.bipartite, canvas.width=500, canvas.height=500)
tkcoords <- tkplot.getcoords(1, norm=TRUE) # Get coordinates of nodes centered on 0 with +/-0.5 for max and min values

# Create point shapefile for nodes
n.points <- nrow(tkcoords)
points.attr <- data.frame(Id=1:n.points, X=tkcoords[,1], Y=tkcoords[,2])
points.data <- data.frame(Id=points.attr$Id, Name=paste("Vertex", 1:n.points, sep=""))
points.shp <- convert.to.shapefile(points.attr, points.data, "Id", 1)
write.shapefile(points.shp, "~/Desktop/points", arcgis=TRUE)


# Create polylines for edges in this example from incidence matrix
n.edges <- sum(inc) # number of edges based on incidence matrix
Id <- rep(1:n.edges,each=2) # Generate Id number for edges.
From.nodes <- g.bipartite[[4]]+1 # Get position of "From" vertices in incidence matrix
To.nodes <- g.bipartite[[3]]-max(From.nodes)+1 # Get position of "To" vertices in incidence matrix

# Generate index where position alternates between "From.node" to "To.node"
node.index <- matrix(t(matrix(c(From.nodes, To.nodes), ncol=2))) 

edge.attr <- data.frame(Id, X=tkcoords[node.index, 1], Y=tkcoords[node.index, 2])
edge.data <- data.frame(Id=1:n.edges, Name=paste("Edge", 1:n.edges, sep=""))
edge.shp <- convert.to.shapefile(edge.attr, edge.data, "Id", 3)
write.shapefile(edge.shp, "~/Desktop/edges", arcgis=TRUE)

希望这有所帮助。

你好,我这里有一个类似的问题:我尝试调整数据属性的XY,但是对我来说效果不佳。你能帮忙解决一下吗?https://dev59.com/-2DVa4cB1Zd3GeqPbDiw - Seen

2

根据我对您的数据的猜测,我会尝试着进行翻译。

基本上,您需要将数据强制转换为一个包含X和Y坐标(或经纬度等)的两列数据框(data.frame)。

library(sp)
data(meuse.grid)
class(meuse.grid)
coordinates(meuse.grid) <- ~x+y
class(meuse.grid)

一旦将其转换为SpatialPointsDataFrame格式,sp包提供了一些不错的功能,包括导出shapefiles:

writePointsShape(meuse.grid,"/home/myfiles/wherever/myshape.shp")

相关的帮助文件示例来自:

  • 坐标
  • SpatialPointsDataFrame
  • readShapePoints

至少在几年前我最后一次使用sp时,它在投影方面表现出色,但在将投影信息写入shapefile方面非常糟糕。因此,最好将坐标保持未转换状态,并手动告诉Arc它是哪种投影方式。或者使用writeOGR而不是writePointsShape


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接