How to plot(add) shapefile in ggplot

I have a raster data and polygons of parks and I want to overlap it on the raster. When I add the polygon it shows here but on ggplot how I add polygons (polygons of parks is like round shapes)on my raster data through ggplot2,. My code is attached below

         r <- raster("E:/research/province.tif")
         pg <- readOGR("E:/park/1aa.shp") # loadshapfile 
        plot(r)
         plot(pg, add= TRUE,) # it appears like first picture (left).

    
when I run  on ggplot by this code polygons are not there on right hand side picture

     centile90 <- quantile(r, 0.90)
      df <- as.data.frame(as(r, "SpatialPixelsDataFrame"))
      colnames(df) <- c("value", "x", "y")

        mybreaks <- seq(0, 500, 50)
   
       ggplot(df, aes(x, y, z = value)) +
       geom_contour_filled(breaks = mybreaks) +
       geom_contour(breaks = centile90, colour = "pink",
          size = 0.5) +
       scale_fill_manual(values = hcl.colors(length(mybreaks) - 3, "Zissou1", rev = FALSE)) +
       scale_x_continuous(expand = c(0, 0)) +
     scale_y_continuous(expand = c(0, 0)) +
     theme_classic() +
    theme()

Your first plot uses r but the second uses pg, I have no idea how they're related.

I'd try this:

r <- raster("E:/research/province.tif")
pg <- readOGR("E:/park/1aa.shp") # loadshapfile 
plot(pg, add= TRUE,) # it appears like first picture (left).

ggplot(pg) + geom_sf()


r is for raster and pg (polygon) is for shapefile. So when i add them they give me picture like (left side) attached below. But my question is how I add polygon in ggplot. My code is work fine with raster but where and I write this in my code. Sorry for poor english and poor programing sense.

ggplot(pg) + geom_sf()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.