[R-sig-Geo] Adding rasters to a 'plotRGB' plot

Benjamin Leutner benjamin.leutner at uni-wuerzburg.de
Thu Dec 21 19:28:59 CET 2017


Hi,

I think plotRGB() uses image() in the background, which is different 
from the standard plot(). Maybe that's were things go wrong.

Since you're also asking for an alternative, let me shamelessly plug 
RStoolbox::ggRGB() ,  a ggplot2 equivalent to raster::plotRGB().
ggRGB() is for colour composite rasters and ggR() is for single layers.
The ggLayer argument turns them in layers which can be added to an 
existing ggplot.

For example:

ggRGB(raster) +                                         # base layer
    ggR(raster, ggLayer = TRUE) +              # layer on  top
    ggRGB(raster, ggLayer = TRUE) +
    ...more_ggplot_stuff...

Cheers,
Benjamin

On 12/20/2017 09:49 PM, Gregovich, Dave P (DFG) wrote:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Benjamin Leutner

Department of Remote Sensing
University of Würzburg
Campus Hubland Nord 86
97074 Würzburg, Germany

Tel: +49-(0)931-31 89594
Email: benjamin.leutner at uni-wuerzburg.de
Web: www.geographie.uni-wuerzburg.de/fernerkundung



More information about the R-sig-Geo mailing list