2

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(t((volcano[,ncol(volcano):1] - 94) * 4.95))
   pg <- readOGR("E:/park/1aa.shp") # loadshapfile 
   plot(r)
   plot(pg, add= TRUE,) # it appears here like first picture (left).

enter image description here

But how can I add this polygons o parks in my ggplot 2. My code of ggplot 2 is attached below.

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

   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()

Help is needed how to add ** pg (polygon) ** in my ggplot2 code.

Update 1 Description of polygon data

enter image description here

user_3264
  • 107
  • 8
  • This is not a reproducible example. Please provide an example that others can run. We do not have access to these data. – thus__ Apr 06 '22 at 16:14
  • I have made changes and try to make it reproducible please have a look on it. – user_3264 Apr 07 '22 at 08:02
  • 1
    Because the developer of `sp` has basically deprecated that package and is focusing all new development on the newer `sf` package, it's now easier to plot polygons on `ggplot2` plots using `sf` instead of `sp`. Try reading your shapefile in with `sf::st_read()` instead of `readOGR`, then you can add a `geom_sf()` to your plot. See https://r-spatial.github.io/sf/articles/sf5.html – qdread Apr 07 '22 at 13:01
  • I understand but my question is how I write **geom_sf()** in my code of ggplot? In which line of code I need to add this function to plot my shapefile on raster image. Sorry for poor English and poor programming sense @qdread. – user_3264 Apr 07 '22 at 13:22
  • It's hard to know without reproducible example (we don't have access to your shapefile) but try `pg <- st_read('1aa.shp`)` then add `+ geom_sf(data = pg, fill = NA)` to your ggplot. – qdread Apr 07 '22 at 13:45
  • Shapefile is just ** Spatialpolygondataframe**. Picture of polygon is attached above with their description. If possible can you use any random raster image and overlaid any shape file on it and then run this above code by some editing to make in ggplot function? @gdread – user_3264 Apr 07 '22 at 14:16
  • I have made changes in code you can see again @gdread – user_3264 Apr 07 '22 at 16:54
  • 1
    Could you please share your `1aa.shp` file? So we can reproduce your problem. – Quinten Apr 08 '22 at 11:03

1 Answers1

3

As explained, it is much handy to work with sf than sp, on top of that sf is meant to superseed sp.

Find here a reproducible example. The first part is just for mocking your file "E:/park/1aa.shp". Since it was not provided it was not possible for me to use your real data, but let's just pretend it is the same dataset...:

library(raster)
library(sf)
library(sp)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Let's mock your shapefile
poly <- st_as_sfc(st_bbox(st_as_sf(rasterToPolygons(r))))

# Sample 4 points
set.seed(3456)

sample <- st_sample(poly, 4)
sample <- st_buffer(sample, c(0.01, 0.02, 0.03))
sample <- st_sf(x=1:4, sample)
st_write(sample, "1aa.shp", append = FALSE)
# Mocked data

# Now let's start with your code -------
library(raster)
library(sf)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Use sf!!
pg <- st_read("1aa.shp") # loadshapfile 
plot(r)
plot(st_geometry(pg), add= TRUE,) # it appears here like first picture (left).

enter image description here

Now work with geom_sf() on your pg object:



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

library(ggplot2)

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) +
  # And here we have it
  geom_sf(data=pg, fill="black", inherit.aes = FALSE) +
   scale_fill_manual(values = hcl.colors(length(mybreaks)-1, "Zissou1", rev = FALSE)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_classic() +
  theme()

enter image description here

dieghernan
  • 2,690
  • 8
  • 16
  • @diegernan It really works for me. Can you tell me last one thing please how I can add the different colors manually in my this code. The color scheme of **Zissou1** is not good for my data. I want to add this colour scheme **"#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"** these colours. Need your help in this last matter – user_3264 Apr 08 '22 at 15:54
  • 3
    Sure, try using `scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"))` but be sure that the number of colors matches the number of elements on `my_breaks`. You may need to adjust either the breaks or the colors – dieghernan Apr 08 '22 at 17:04
  • I think you will need 9 colors with your current setup, so one more – dieghernan Apr 08 '22 at 17:08
  • Its really work for me and thanks for detail answering @dieghernan – user_3264 Apr 09 '22 at 13:58