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

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Thu Apr 11 15:13:49 CEST 2019


Hi,

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.  

https://oscarperpinan.github.io/rastervis/FAQ.html#several_rasters

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.

Cheers,
Ben

### START
library(sp)
library(raster)
library(rasterVis)
library(RColorBrewer)

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) {
		levelplot(S[[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
http://www.bigelow.org

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



More information about the R-help mailing list