bleutner/RStoolbox

Fails to run ggRGB with stacked rasters

cdjemiel opened this issue · 4 comments

Hi,

I am trying to run ggRGB with stacked rasters and it returns me the following error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spatSample’ for signature ‘"RasterStack"’

Here is my code

r1 = raster("rgbr.tif")
r2 = raster("rgbg.tif")
r3 = raster("rgbb.tif")
s = raster::stack(r1, r2, r3)

RStoolbox::ggRGB(s, r = 1, g = 2, b = 3, stretch = "hist")

And with plotRGB funtion, it is works

plotRGB(s, stretch='hist')
Do you have an idea ?

Thanks

Best

Unfortunately we cannot reproduce your issue since we are not in the posession of the .tif files that you are reading in.

With the last example data (as Raster stack,) ggRGB works with the hist stretch:

library(RStoolbox)
library(terra)
library(raster)


s <- as(lsat, "Raster")
ggRGB(s, r = 1, g = 2, b = 3, stretch = "hist")

Please provide a reproducible example so that we can test the issue.

Thanks, yes sorry, i crop my rasters (because it is for a publication but the problem remains).

Here the three rasters

Archive.zip

Best

This should be solved by forcing ggRGB to not use annotation_raster with setting geom_raster = TRUE:

library(RStoolbox)
library(raster)

r1 = raster("rgbr.tif")
r2 = raster("rgbg.tif")
r3 = raster("rgbb.tif")
s = stack(r1, r2, r3)

ggRGB(s, r = 1, g = 2, b = 3, stretch = "hist", geom_raster = T)

image

In general, consider switching to terra since raster is deprecated:

library(RStoolbox)
library(terra)

r1 = rast("rgbr.tif")
r2 = rast("rgbg.tif")
r3 = rast("rgbb.tif")
s = c(r1, r2, r3)

ggRGB(s, r = 1, g = 2, b = 3, stretch = "hist", geom_raster = T)

Perfect, thanks, it works.