2

I want to make a SpatialPolygonsDataFrame using the output from get_map in ggplot2. I found a way to do it after some trial and error but it seems convoluted to me. Is there a cleaner way?

library(ggplot2)
library(sp)
library(plyr)
pnw.df <- map_data("state",region=c("washington","oregon","idaho"))
# delete islands from subregions
pnw.df$subregion[is.na(pnw.df$subregion)] <- "main" 
pnw.df <- subset(pnw.df,subregion == "main") 

getPolygons <- function(x) {
  Polygons(list(Polygon(x[,c("long","lat")])),ID=unique(x$region))
}

pnw.sp <- SpatialPolygons(dlply(pnw.df, .(region), getPolygons))
pnw.sp <- as(pnw.sp,"SpatialPolygonsDataFrame")
proj4string(pnw.sp) <- "+proj=longlat +ellps=WGS84"
plot(pnw.sp)
user4100013
  • 111
  • 9
  • `ggplot2::map_data("state")` calls `maps::map("state", ...)` under the hood. Have a look at Josh O'Brien's answer [here](https://stackoverflow.com/questions/8751497/latitude-longitude-coordinates-to-state-code-in-r/8751965#8751965) for a very convenient way to convert its output to 'SpatialPolygons'. – fdetsch Aug 30 '18 at 07:37

1 Answers1

0

Look into maptools::map2SpatialPolygons, an try its examples. map_data reads the data from maps and puts them in a data.frame, which is not very helpful. The maptools function takes the same data from maps, and converts them into a SpatialPolygons object.

Edzer Pebesma
  • 3,674
  • 15
  • 24