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?

  • Can you add screengrabs to show us what's happening? Its possible that if your numbers have few discrete values then the pattern of the histogram is very sensitive to where the "bins" are.
    – Spacedman
    Nov 14, 2017 at 13:30

2 Answers 2


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


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

## 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.


You are invariably going to get different results based on how sampling of the raster is conducted. The size and type of sample will produce different results in the histogram. This is commonly done via sampling because rasters can get into millions or even billions of cells/values.

A random sample can normally tell you everything that you need to know about the statistical distribution of the data. Sampling populations is the cornerstone of statistics! Unless your analysis requires an actual count of each class, it is overkill to use the population.

One should ask exactly what they are after in exploratory data analysis. Do you need the distribution of the data or the actual class counts, for additional analysis? These decisions are certainly driven by your data. If your data is categorical, what exactly will a histogram tell your analysis? If the data is truly categorical perhaps the class-counts would be more informative. Variation in the histogram is expected when using a sample but, remains relative. The size and number of bins, if not defined, are derived using an algorithm. Depending on the algorithm implemented in a given software, the histogram can be quite different across software(s).

The bin size/number methods available in R are" "Sturges" (bases bin size on range), "Scott" (assumes Gaussian distribution and uses standard error) or "Freedman-Diaconis" (uses inter-quartile range). The method can have a notable effect on the resulting histogram, or not, it just depends on the underlying distribution of the data.


Let's create a Gaussian raster and examine the sampled, population histograms as well as the probability density function (PDF).

r <- raster(nrows=50, ncols=50)
  r <- ( setValues(r, runif(ncell(r))) + setValues(r, runif(ncell(r))) )

  hist(r, main = "sampled hist") 
  hist( getValues(r), main="population hist")

You can see that the PDF is as informative as the histogram(s). In this case there is no real difference in the sampled and population histograms because of a fairly small raster. Also, a random sample on a normal distribution, if of adequate size, will capture the characteristics of the distribution in a histogram or PDF/CDF. For data with more continuous distributions, a PDF is a more stable way to examine the characteristics of the distribution. Since a histogram has to bin the data it is sensitive to bin numbers and size and certain distributions can be mischaracterized.

Now, let's create a binomial raster [0,1] and look at the histogram as well as a contingency table of value counts.

b <- raster(nrows=180, ncols=360, xmn=571823.6, xmx=616763.6, ymn=4423540, 
             ymx=4453690, resolution=270, crs = CRS("+proj=utm +zone=12 +datum=NAD83 
             +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
b[] <- rpois(ncell(b), lambda=1)
b <- calc(b, fun=function(x) { x[x >= 1] <- 1; return(x) } )  

hist( getValues(b), main="population hist")


Here, I would imagine that for most analysis, the contingency table would be much more informative. If you generated a contingency table via a random sample, the relative counts should be comparable. The trick is finding the balance between the sample size capturing the characteristics of the population and it not being overly large.

Here is a random sample of 10% producing relative counts.

n = round(ncell(b) * 0.10, 0)
  table( sampleRandom(b, n) )

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.