Search code examples
rr-rasterrgdal

Error when using RColorBrewer to plot


I am trying to make a plot of a map from raster data. I am using this code:

library(raster)
library(rgdal)
library(classInt)
library(RColorBrewer)

NDII = raster("G:\\Sheyenne\\image_differencing\\NDII\\differenced.tif")
value.vector = values(NDII)
breaks.qt = classIntervals(value.vector, n = 6, style = "jenks", intervalClosure = "right")
print (plot(NDII, breaks=breaks.qt$brks, col = brewer.pal(6, "Set1")))

but this returns:

Error in print(plot(NDII, breaks = breaks.qt$brks, col = brewer.pal(6, : error in evaluating the argument 'x' in selecting a method for function 'print': Error in .asRaster(x, col, breaks, zrange, colNA, alpha = alpha) : could not find function "brewer.pal"


Solution

  • You provide no reproducible example, and I can't reproduce your error. The following code, which is the same as yours using the reproducible example of the R logo as a raster (and with the shortcut of using NDII[] instead of storing values(NDII) in a variable) works just fine for me...

    library(raster)
    library(rgdal)
    library(classInt)
    library(RColorBrewer)
    
    NDII = raster(system.file("external/rlogo.grd", package="raster"))
    # next line is really slow, I'd advise to run crop(NDII, extent(0,20,0,20)) 
    # before to make quick tests
    breaks.qt = classIntervals(NDII[], n = 6, style = "jenks", 
                               intervalClosure = "right")
    plot(NDII, breaks=breaks.qt$brks, col = brewer.pal(6, "Set1"))
    

    Do you reproduce your error with this code? Maybe you can start with a new, fresh session?

    enter image description here