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

8
  • This is not a reproducible example. Please provide an example that others can run. We do not have access to these data. Commented Apr 6, 2022 at 16:14
  • I have made changes and try to make it reproducible please have a look on it. Commented Apr 7, 2022 at 8: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 r-spatial.github.io/sf/articles/sf5.html Commented Apr 7, 2022 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. Commented Apr 7, 2022 at 13:22
  • 1
    Could you please share your 1aa.shp file? So we can reproduce your problem. Commented Apr 8, 2022 at 11:03

1 Answer 1

3
+50

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

Sign up to request clarification or add additional context in comments.

4 Comments

@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
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
I think you will need 9 colors with your current setup, so one more
Its really work for me and thanks for detail answering @dieghernan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.