[R-sig-Geo] Problems with plotting large factor-type rasters using the raster package

Tomislav Hengl hengl at spatial-analyst.net
Thu Jan 23 10:56:45 CET 2014


Hi Robert,

I am having problems with plotting factor-type rasters using the raster 
package (v. 2.1-49). Here is the error message I get:

library(raster)
library(plotKML)
## smaller raster:
data(eberg_grid)
gridded(eberg_grid) <- ~x+y
eberg_grid$CL <- cut(eberg_grid$DEMSRT6, breaks=c(200,250,300,350))
r <- raster(eberg_grid["CL"])
image(r, col=c("red","green","blue"))
## This works fine; now lets try with a larger raster (>5e5 pixels):
sp <- SpatialPixelsDataFrame(spsample(eberg_grid, n=1e6, 
type="regular"), data=data.frame(v=runif(1000^2)))
sp$CL <- cut(sp$v, breaks=c(0.1,0.2,0.3,0.4))
r2 <- raster(sp["CL"])
image(r2)

Error in .checkLevels(levels(x)[[1]], value) :
   new raster attributes (factor values) should be in a data.frame 
(inside a list)

Somehow, image function in the raster package does not want to plot 
large rasters that are of factor type. I've looked at the source code 
(https://r-forge.r-project.org/scm/viewvc.php/pkg/raster/R/image.R?view=markup&root=raster), 
and it looks as the problem is with the "maxpixels" argument (if the new 
raster has >5e5 pixels, the function breaks).

thank you,

T. (Tom) Hengl
Url: http://www.wageningenur.nl/en/Persons/dr.-T-Tom-Hengl.htm
Network: http://profiles.google.com/tom.hengl
Publications: http://scholar.google.com/citations?user=2oYU7S8AAAAJ



More information about the R-sig-Geo mailing list