Overlay Polygon layer on a raster stack qplot

231 Views Asked by At

I want to plot 7 TIFF files side by side using gplot and I want to overlay a polygon layer over them. I tried reading the shapefile using st_read and then using geom_sf or geom_polygon to plot it. But it didn't work. It was throwing an error regarding aes. I finally used fortify function and then used geom_path to plot it. While it does get overlayed, it is not what I want. I just want the polygon boundaries to be overlaid.

P.S. I don't know how to create reproducible raster and vector layer. So, if required, I can upload the sample files.

shp = fortify(shapefile("E:/Vect/BLOCKS.shp"))
rfiles = list.files(path = "E:/Rast", pattern = "*.tif", full.names = TRUE)
pfiles = stack(rfiles[c(7:14)])

plot1 = gplot(pfiles1) + 
  geom_tile(aes(fill = value)) +
  geom_path(data=shp,aes(long, lat),colour="red") + 
  facet_wrap(~ variable, ncol = 7) +
  scale_fill_gradientn(colours = magma(30), na.value = "transparent") +
  theme_bw() +
  theme(axis.text = element_blank(), legend.position = "right", legend.direction = "vertical",
        legend.key.height = unit(5, "cm"), legend.key.width = unit(1, "cm"),
        legend.title = element_blank(), legend.text = element_text(size = 30), 
        strip.text = element_text(size = 28, face = "bold")) +
  coord_equal() 

enter image description here

1

There are 1 best solutions below

0
On

Just a small change. Add group = group in geom_path

geom_path(data=shp,aes(long, lat, group = group),colour="red")