[R] How to overlay a vector map (polygon) on raster maps?

Kristi Glover kr|@t|@g|over @end|ng |rom hotm@||@com
Thu Apr 11 17:32:53 CEST 2019

Dear Ben,
Thank you very much for the message. I run it and it produced three separate images with X and y Axis  and a legend for each image. I was thinking to plot all of these three images with a single legend and only X axis value at the bottom's image and y values for each image.

I added the following code on your code

But, as I mentioned above, I can get three separate images with its own legend and X and Y axix

From: Ben Tupper <btupper using bigelow.org>
Sent: April 11, 2019 7:13 AM
To: Kristi Glover
Cc: r-help using r-project.org
Subject: Re: [R] How to overlay a vector map (polygon) on raster maps?


I think you want to build a levelplot object with polygon overlaid for each layer.  Like the link below shows but with the added layer per your example.


Also, you will get bucket loads of spatial-centric help using the r-sig-geo mailing list; check it out here https://stat.ethz.ch/mailman/listinfo/r-sig-geo.



S <- raster::stack(system.file("external/rlogo.grd", package="raster"))

# make a polygon by shrinking the extent and casting object type
Poly <- as(raster::extent(S) + c(15, -32, 25, -10), "SpatialPolygons")

# build the layers in a list, adding the polygon to each layer
themes <- c("Reds", "Greens", "Blues")
PP <- lapply(seq_len(nlayers(S)),
        function(i) {
                        par.settings = rasterVis::rasterTheme(region = RColorBrewer::brewer.pal(9, themes[i])),
                        margin=FALSE) +
                layer(sp.polygons(Poly, col = "orange", lwd = 2))

# print each layer, but specify the location within a layout scheme
print(PP[[1]], split=c(1, 1, 1, 3), more = TRUE)
print(PP[[2]], split=c(1, 2, 1, 3), more = TRUE)
print(PP[[3]], split=c(1, 3, 1, 3), more = FALSE)
### END

> On Apr 11, 2019, at 7:28 AM, Kristi Glover <kristi.glover using hotmail.com> wrote:
> He R users,
> I have been struggling to plot a boundary map over the raster maps. I tried using the following example, but the boundary map could not be displayed over the three raster maps.
> It works if we plot for a single raster. However when I want to plot the three maps using "levelplot" and add the boundary map it did not work.  I wanted to plot three raster same time because the "levelplot" so that we can compare the maps as they have only one legend.
> My example code is given below, do you have any suggestions?
> library(gridExtra)
> library(raster)
> library(sp)
> library(rasterVis)
> library(rgdal)
> library(maptools)
> boundary<- readShapeSpatial("boundrymap.shp")
> minTemp<-raster("minTemp.tif")
> maxTemp<-raster("maxTemp.tif")
> averageTemp<-raster("averageTemp.tif")
> Temp<-stack(minTemp,maxTemp,averageTemp)
> levelplot(Temp, layout=c(1,3))+ layer(sp.polygons(boundary, col = "yellow"))
> thanks
>        [[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544

Ecological Forecasting: https://eco.bigelow.org/

	[[alternative HTML version deleted]]

More information about the R-help mailing list