I converted a raster to an image and wanted to plot it. However, I then get the following error:
library(raster)
r
#class : RasterLayer
#dimensions : 23320, 37199, 867480680 (nrow, ncol, ncell)
#resolution : 0.02, 0.02 (x, y)
#extent : 341668.9, 342412.9, 5879602, 5880069 (xmin, xmax, ymin, ymax)
#crs : +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
#source : r_tmp_2022-07-21_113344_507_06340.grd
#names : layer
#values : 2.220446e-16, 0.2999999 (min, max)
x <- maptools::as.im.Rasterlayer(r)
x
#real-valued pixel image 23320 x 37199 pixel array (ny, nx)
#enclosing rectangle: [341670, 342410] x [5879600, 5880100] units
plot(x)
#Error in (function (x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1, :
#‘useRaster = TRUE’ can only be used with a regular grid
I've already tried to find this error with help(plot) or something but there is nothing about it anywhere.
In future, please provide a minimal working example - actual data and code that generates the error.
Whenever you get an error, I suggest you immediately type
to see the sequence of functions that were executed. (The printout is a traceback: it starts with the function that generated the error, then lists the function that called it, and so on, until it reaches the command line that you originally typed.) This will often tell you where to look.
Since you are plotting an object of class
im, andplotis a generic function, the relevant help file is for the methodplot.im, i.e. you should typehelp(plot.im)or?plot.im, nothelp(plot).Since we don't have a working example, we can't debug this for you. But this particular error is probably coming from the R base graphics function
image.defaultand it is apparently complaining that thexandycoordinates in the data do not appear to be equally spaced. You can try either settinguseRaster=FALSEin theplotcommand, or fixing the data.Some packages alter the
xandycoordinates of pixels in an image, by a very small amount, in such a way that other packages "think" the coordinates are not equally spaced. Inspatstatthere is a functionrepair.image.xycoordsthat can be used to repair such data. So, ifZis your image, useto fix the coordinates, and then plot
ZZ.