Kristi Glover
2019-Apr-11  11:28 UTC
[R] How to overlay a vector map (polygon) on raster maps?
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]]
Ben Tupper
2019-Apr-11  13:13 UTC
[R] How to overlay a vector map (polygon) on raster maps?
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 at
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 at 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/
Kristi Glover
2019-Apr-11  15:32 UTC
[R] How to overlay a vector map (polygon) on raster maps?
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
grid.arrange(PP[[1]],PP[[2]],PP[[3]])
But, as I mentioned above, I can get three separate images with its own legend
and X and Y axix
Thanks,
________________________________
From: Ben Tupper <btupper at bigelow.org>
Sent: April 11, 2019 7:13 AM
To: Kristi Glover
Cc: r-help at r-project.org
Subject: Re: [R] How to overlay a vector map (polygon) on raster maps?
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 at
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 at 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/
	[[alternative HTML version deleted]]