0

I have a stack of raster's and suppose to find the mean of it. I have done it. Now, I want to add polygon on the mean of raster image that I got. Then, from this raster I wants to save and make it colorful by using ggplot2 code. But I don't understand how to and where add polygon in ggplot2.

    fs <- list.files(path="directory.tiff", pattern = 
                        "tif$", full.names = TRUE)
    s<- raster::stack(fs)
    pg <- readOGR("parks.shp")
    plot(pg, add= T ) 
    se1 <- calc(s, fun = mean)       
    plot(se1)

They give me this attached below picture, Parks overlay on the mean of raster's. Its fine till now.

enter image description here

But when I used ggplot to change its color scheme. They gave me desired pattern what I want, but the problem is parks polygons doesn't overlay on the final picture (attached below after code what I get). So can anyone tell me where I need to change the code in ggplot to get park shp overlay on the picture 2.

   conti_col_pal <- pnw_palette("Bay",10,type="continuous")
   binary.cols <- c("1" = conti_col_pal[10], "0" = "white")

    cv.df <- as.data.frame(rasterToPoints(se1))
    ##above I give the comand of se1 whcih contain 
    p_cv <- ggplot(pg) +
    coord_fixed() +
    geom_raster(data = xy_FONDO, aes(lon, lat, fill = r)) +
   scale_fill_gradient(low = "gray56", high = "gray56", na.value = NA, guide = FALSE) +

   new_scale("fill") +
   geom_raster(data = cv.df, aes(x, y, fill = layer))+
   scale_fill_gradientn(colours = conti_col_pal,
                   breaks = seq(-1, 7, 2), limits = c(0, 10))+

    annotate(geom = "text", x = lonmin+2, y = latmax-2, vjust = 1, hjust = 0, 
     label = "",
     color = "black", angle = 0, size=4)+

   scale_x_continuous(limits = c(lonmin, lonmax), expand = c(0, 0)) +
   scale_y_continuous(limits = c(latmin, latmax), expand = c(0, 0))+
   theme_bw(base_family="")+
   theme(
   plot.margin = margin(0, 0, 0, 0, "cm"),
  #panel.background = element_rect(fill = col_pal_binary[1], colour = col_pal_binary[1], size 
 = 0.5, linetype = "solid"),
   panel.grid.major = element_blank(),
   panel.grid.minor = element_blank(),
  axis.title.x=element_blank(),
  axis.title.y=element_blank(),
  axis.text.x=element_blank(),
  axis.text.y=element_blank(),
  axis.ticks.x=element_blank(),
  axis.ticks.y=element_blank(),
  legend.background = element_rect(fill=NA, size=0.3, linetype="solid", color=NA),
  legend.position = c(0.8, 0.35),
  legend.title= element_blank(),
  legend.text = element_text(colour = "black", size = 12, angle = 0),
  legend.direction = "vertical", ##vertical; horizontal
  legend.title.align=0.5)+
  guides(fill = guide_colorbar(## label.position = "bottom",
  ## title.position = "left",
 label.vjust=0,
 # draw border around the legend
 frame.colour = "black",
 barwidth = 1.5,
 barheight = 8))


 tiff(filename = "bay.tiff", res = 600, 
 width = 4080, height = 3200, compression = "lzw")
 grid.arrange(p_cv,
         ncol = 1)
dev.off() 

enter image description here

1
  • You can use other color scheme for this. Why only ggplot?
    – Daniel
    Nov 9 '21 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.