Jack Aubrey - 3 months ago 16
R Question

# How do I generate a qqplot from raster data?

I am trying to generate a

`qqplot`
relating to a DEM that I produced by differencing two DEMs using R. I have generated several
`qqplot`
s in the past, but this is my first attempt to generate one from a raster.

When I attempt to generate the
`qqplot`
I am given a warning and error, of which I am unsure its meaning (see below).

How do produce a
`qqplot`
plot for my raster in R?

Input:

``````setwd("N:/x/y/data")
DEM1 <-raster("DEM1") ; DEM2<-raster("DEM2")
DEM_Diff= DEM2 - DEM1
qqplot(DEM_Diff, main="Q-Q Plot:DEM_Diff")
``````

Output:

``````Warning message:
In any(<S4 object of class "RasterLayer">, na.rm = FALSE) :
Nothing to summarize if you provide a single RasterLayer; see cellStats
Error in x[order(x, na.last = na.last, decreasing = decreasing)] :
error in evaluating the argument 'i' in selecting a method for function '[':Error in !is.na(na.last) && any(nas) : invalid 'y' type in 'x && y'
``````

`qqplot` requires 2 data sets as input (it plots one distribution against another). For a single data set, you can use `qqnorm` to plot against a normal distribution. You can pass the raster data to qqnorm either by specifying the slots in which the data are stored, or by extracting the values first:

``````library(raster)
r=raster(volcano)
qqnorm(values(r))
# qqnorm(r@data@values) also works
# qqnorm(as.matrix(r)) also works
``````

In case what you are actually attempting to do is to plot the distribution of one raster against another (this was not clear from the question) you can do `qqplot(values(r1), values(r2))`

Source (Stackoverflow)