[R-sig-Geo] Adding rasters to a 'plotRGB' plot
Gregovich, Dave P (DFG)
dave.gregovich at alaska.gov
Wed Dec 20 21:49:47 CET 2017
Hello,
I would like to add rasters to a plotRGB plot, but they don't seem to register correctly with the initial (=background) raster plotted, as illustrated with the following code.
Does anybody out there know of a way to ensure that additional overlain rasters register spatially with the initially plotted background raster? If not possible with plotRGB, what other function would you use for this task?
(it also appears that SPDF's and their ilk have the same issue when trying to add to a plotRGB plot)
Thanks kindly.
library(raster)
#create initial background raster
side.length <- 100
r1 <- raster(nrow = side.length, ncol = side.length, ext = extent(0,side.length, 0, side.length))
r2 <- r3 <- r1
r1[]<- 200; r2[]<- 210; r3[]<- 230
stack1 <- stack(r1, r2, r3)
#create extents of rasters to plot on top of background
ex.1 <- extent(side.length * 0.1, side.length * 0.9, side.length * 0.1, side.length * 0.9)
ex.2 <- extent(side.length * 0.05, side.length * 0.95, side.length * 0.05, side.length * 0.95)
ex.3 <- extent(side.length * 0.02, side.length * 0.98, side.length * 0.02, side.length * 0.98)
rast1 <- crop(t.stack, ex.1)[[1]]
rast2 <- crop(t.stack, ex.2)[[1]]
rast3 <- crop(t.stack, ex.3)[[1]]
#plot and inspect region covered by each raster...
x11(10, 10)
plotRGB(stack1)
#first raster looks centered as it should be...
plot(rast1, add = T, col = '#ffff334D', legend = F)
plot(ex.1, add = T)
#second raster does not look centered, and does not align with its plotted extent
plot(rast2, add = T, col = '#ff1a754D', legend = F)
plot(ex.2, add = T)
#each successive raster should be larger than the first, but they aren't
plot(rast3, add = T, col = '#1a75ff4D', legend = F)
plot(ex.3, add = T)
___________________________________________
Dave Gregovich
Research Analyst
Alaska Department of Fish and Game
Division of Wildlife Conservation
802 3rd Street
Douglas, AK 99824
907-465-4291
___________________________________________
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list