Spatial data in ggplot2

by on July 12, 2016

I recently figured out how to use the ggplot2 framework to plot raster data, which led me to write a package extending ggplot2 for more convenient spatial data geometries. Ever wanted a geom_spatial() for those SpatialPolygonsDataFrames and RasterBricks you’ve got kicking around? Well…maybe you did or didn’t, but the ggspatial package is now here for your spatial data/ggplot-ing pleasure. It needs some work (and feedback) before it becomes a CRAN release, but in the meantime it should do the trick for most things spatial. Unfortunately, there is a bug when using coord_fixed() and scales="free" with facetting in ggplot, so facetting with different spatial extents is out. Luckily, that’s not particularly common (probably more common is to vary the aesthetic with the same spatial extent which still isn’t quite there yet in this package), so give it a shot!

install.packages("devtools") # if devtools isn't installed
data(longlake_waterdf, longlake_depthdf)
ggplot() + geom_spatial(longlake_waterdf[2,], fill="lightblue") +
  geom_spatial(longlake_depthdf, aes(col=DEPTH.M), size=2) + 
  scale_color_gradient(high="black", low="#56B1F7") + coord_fixed()
All the ggplot goodness with your spatial data

All the ggplot goodness with your spatial data

Leave a Reply

WP Facebook Like Send & Open Graph Meta powered by