89_Simple
89_Simple

Reputation: 3805

Adding a shapefile to raster

EDIT

x <- raster::getData('worldclim', var='tmin', res = 10)
s <- stack(x$tmin1,x$tmin12)
sp <- as(s, 'SpatialGridDataFrame')

fra <- raster::getData('GADM',country = 'FRA', level = 2)
my.layer <- list("sp.polygons", fra, col = "black")    
spplot(sp, names.attr = c("tmin1","tmin12"), sp.layout = my.layer)

enter image description here

How do I plot fra on top of the raster and not underneath.

Upvotes: 0

Views: 985

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47091

You can do this:

library(latticeExtra)
spplot(s, names.attr = c("tmin1","tmin12")) + layer(sp.polygons(fra, lwd = 2))

Note that I use RasterStack s, there is no need to create a SpatialGridDataFrame.

You can also do

p <- spplot(s, names.attr = c("tmin1","tmin12")) 
p + layer(sp.polygons(fra, lwd = 2))

If you have ggplot2 loaded, this will trigger an error: Error: Attempted to create layer with no geom.

To avoid that, use the latticeExtra namespace prefix.

 p + latticeExtra::layer(sp.polygons(fra, lwd = 2))

Upvotes: 4

Related Questions