0

I am brand new to R and GIS and I ultimately am not doing anything to complicated (for now) but have been struggling with even the most basic task! I'm simply trying to plot a map of the US states. I was following along with a tutorial and decided to do it with my own data. I import (using readOGR) the shapefile and it shows up as a "Large SpatialPolygonsDataFrame (56 elements, 6.6MB). Then I tried graphing it: plot(state) and it took about 45 minutes to produce a graph that didn't even look quite right.

I figured it was a ram issue on my computer but then I tried it with the shapefile specified in the tutorial. It came in as a formal class spatialpolygonsdataframe and plotting it took around a second.

Therefore I figure it's something with the shapefile.

Is there way to compress it/ change it so it's not a "large" dataframe or just speed up operations in general?

1 Answer 1

4

The best way in short order is to use sf instead. Rather than 'rgdal::readOGR' instead use 'sf::read_sf' - you don't need to worry about the layer name, just do

library(sf)
x <- read_sf("/path/to/my.shp")

plot(st_geometry(x))

(Make sure to do the st_geometry thing first time you plot, otherwise it will provide a faceted plot with a panel for every column up to a maximum, which can be very slow. )

An extra bonus is that the next version of ggplot2 will understand these data frames from sf, and also dplyr idioms already largely work as well. See the vignettes on CRAN for sf for details

1
  • What's the speed difference?
    – jebyrnes
    Commented Jan 3, 2018 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.