Export R plot to shapefile
Asked Answered
C

2

6

I am fairly new to R, but not to ArcView. I am plotting some two-mode data, and want to convert the plot to a shapefile. Specifically, I would like to convert the vertices and the edges, if possible, so that I can get the same plot to display in ArcView, along with the attributes.

I've installed the package "shapefiles", and I see the convert.to.shapefile command, but the help doesn't talk about how to assign XY coords to the vertices.

Thank you,

Tim

Catamenia answered 17/5, 2011 at 22:41 Comment(4)
What format is your data in currently? An example file or dput() would help us answer your question.Merimerida
it sounds like you want "lines", but perhaps "polygons"? Please post at least an image of the plot you describe, and the code you run to generate it.Nicker
@gsk3, @Nicker The data started out as a CSV file with two columns. The first column is a numerical field with a unique number representing a person. The second column is a character field representing events. After converting it into a matrix, then a network, the resulting plot is vertices connected by lines, and that's what I'd like the shapefile to represent. I can't make heads or tails of the help file regarding attaching an example, otherwise I'd send you a PDF of the plot.Catamenia
Easiest way to include plots is to output as a png and just attach the image when editing your post. Easiest way to include small objects is to use dput. Easiest way to include large objects is to upload them to a dropbox or some FTP site and link to them.Merimerida
S
4

Ok, I'm making a couple of assumptions here, but I read the question as you're looking to assign spatial coordinates to a bipartite graph and export both the vertices and edges as point shapefiles and polylines for use in ArcGIS.

This solution is a little kludgey, but will make shapefiles with coordinate limits xmin, ymin and xmax, ymax of -0.5 and +0.5. It will be up to you to decide on the graph layout algorithm (e.g. Kamada-Kawai), and project the shapefiles in the desired coordinate system once the shapefiles are in ArcGIS as per @gsk3's suggestion. Additional attributes for the vertices and edges can be added where the points.data and edge.data data frames are created.

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)

Hope this helps.

Smallish answered 18/5, 2011 at 18:2 Comment(1)
Hello, I have a similar question here: I tried to tweak with the XY for data attributes but it does not work well for me. Could you help with this please? #9205727Friendship
M
2

I'm going to take a stab at this based on a wild guess as to what your data looks like.

Basically you'll want to coerce the data into a data.frame with two columns containing the x and y coordinates (or lat/long, or whatever).

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

Once you have it as a SpatialPointsDataFrame, sp provides some decent functionality, including exporting shapefiles:

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

Relevant help files examples are drawn from:

  • coordinates
  • SpatialPointsDataFrame
  • readShapePoints

At least a few years ago when I last used sp, it was great about projection and very bad about writing projection information to the shapefile. So it's best to leave the coordinates untransformed and manually tell Arc what projection it is. Or use writeOGR rather than writePointsShape.

Merimerida answered 18/5, 2011 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.