Jack Aubrey Jack Aubrey - 11 months ago 68
R Question

How do I generate a qqplot from raster data?

I am trying to generate a

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

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

How do produce a
plot for my raster in R?


DEM1 <-raster("DEM1") ; DEM2<-raster("DEM2")
DEM_Diff= DEM2 - DEM1
qqplot(DEM_Diff, main="Q-Q Plot:DEM_Diff")


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'

dww dww
Answer Source

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:

# qqnorm(r@data@values) also works
# qqnorm(as.matrix(r)) also works

enter image description here

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))