[GIS] Raster histogram in R

histogramr

I need to do a histogram from a big raster, its a full region in Chile. But I don't like the QGIS's Histograms so I decided to make it in R. I installed sp, raster and rgdal packages, but R brings me this message when I do it:

"Warning message: In .hist1(x, maxpixels = maxpixels, main = main,
plot = plot, …) : 0% of the raster cells were used. 100000 values
used."

Then I compare both histograms (in QGIS and R) and they don't looks as the same frequency. Any idea how can I fix it?

Best Answer

Warning message: In .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) : 0% of the raster cells were used. 100000 values used.

Is not an error message, it's only a warning. Check ?raster::hist, the default function is:

hist(x, layer, maxpixels=100000, plot=TRUE, main, ...)

Where maxpixels is the subsample for large objects


An example with EGM2008 2.5' geoid model (EPSG::1027)

enter image description here

library(raster)

r <- raster('path/to/egm2008-2.5.tif') # egm2008 geoid 2.5'

hist(r)
## Warning message:
## In .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
##   0% of the raster cells were used. 100000 values used.

enter image description here

hist(r, maxpixels=1000000)
## Warning message:
## In .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
##   3% of the raster cells were used. 1000000 values used.

enter image description here

Plots are different because subsamples have different size (especially in y-axis). Be careful choosing the right size. If you pretend to use all values (hist(r, maxpixels=ncell(r))), you'll be waiting for a long time.

I recommend you to check also histogram() from rasterVis package.

Related Question