1

I have multiple individual band rasters in tif format, and I tried creating a RGB raster from the R, G, B rasters in R using the raster package and then wrote it to disk. Now, apparently the RGB raster created just fine, as it loaded just fine in ArcMap as seen below. But, now when I try to plot the save RGB raster via raster::plotRGB, I get this error:

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

And with RSToolbox::ggRGB, I get this error:

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'sampleRegular': not a valid subset

How can I fix this error? I even get the same error if I the write the raster stack to file and plot that. So, I think I am not writing the raster object to file correctly.

Code

library(raster)
library(RSToolbox)
R = raster("path/Red.tif")
G = raster("path/Green.tif")
B = raster("path/Blue.tif")

RGB = raster::stack(R, G, B)
RGB = raster::brick(RGB)

writeRaster(RGB , filename = "path/RGB.tif",overwrite  = TRUE)

RGB = raster("path/RGB.tif")

plotRGB(RGB) # Gives error

ggRGB(RGB) # Gives error

As shown in ArcMap

enter image description here

Basic raster layer info:

enter image description here

1 Answer 1

2

In stead of

RGB = raster("path/RGB.tif")

You should do

RGB = brick("path/RGB.tif")

Or even better:

library(terra)
r = rast("path/RGB.tif")
plotRGB(r)

# or
RGB(r) <- 1:3
plot(r) 
2
  • Is terra more preferred over raster now, just as sf is preferred over sp? Commented Jul 7, 2021 at 22:43
  • 1
    yes, it is --- if you ask me. One advantage is that there is only rast, no need to choose between raster, stack, and brick Commented Jul 7, 2021 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.