SpatialPolygonsDataFrame_to_df | R Documentation |
Convert SpatialPolygonsDataFrame
or SpatialPixelsDataFrame
object to data frame.
SpatialPolygonsDataFrame_to_df(sp_polys, vars = names(sp_polys))
sp_polys |
object of class |
vars |
variables to put into data frame (by default all of them) |
This function is mainly used for plotting SpatialPolygonsDataFrame
objects with ggplot
rather than spplot
. The coordinates of each polygon are extracted and concatenated into one long data frame. The attributes of each polygon are then attached to this data frame as variables that vary by polygon id
(the rownames of the object).
library(sp)
library(ggplot2)
opts_FRK$set("parallel",0L)
df <- data.frame(id = c(rep(1,4),rep(2,4)),
x = c(0,1,0,0,2,3,2,2),
y=c(0,0,1,0,0,1,1,0))
pols <- df_to_SpatialPolygons(df,"id",c("x","y"),CRS())
polsdf <- SpatialPolygonsDataFrame(pols,data.frame(p = c(1,2),row.names=row.names(pols)))
df2 <- SpatialPolygonsDataFrame_to_df(polsdf)
## Not run: ggplot(df2,aes(x=x,y=y,group=id)) + geom_polygon()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.