简体   繁体   中英

R - create boxplot with rasters

this might be rather simple, but I am a novice in R. I have tried for awhile now to plot two rasters against each other using boxplot from the package raster.

I have a DEM raster and a categorical raster that contains 4 cluster groups, which I would like to use as 'zones' as described in the manual:

boxplot(x, y=NULL, maxpixels=100000, ...)

x Raster* object

y If x is a RasterLayer object, y can be an additional RasterLayer to group the values of x by 'zone'

> DEM
class       : RasterLayer 
dimensions  : 12381, 61922, 766656282  (nrow, ncol, ncell)
resolution  : 0.1, 0.1  (x, y)
extent      : 478307.4, 484499.6, 6131862, 6133100  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs 
data source : /Users/Yvonne/Desktop/Boxplot/Ribe_DEM_0.1m.tif 
names       : Ribe_DEM_0.1m 
values      : -7.523334, -0.36  (min, max)

> Cluster
class       : RasterLayer 
dimensions  : 12381, 61922, 766656282  (nrow, ncol, ncell)
resolution  : 0.1, 0.1  (x, y)
extent      : 478307.4, 484499.6, 6131862, 6133100  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs 
data source : /Users/Yvonne/Desktop/Boxplot/final_cluster.tif 
names       : final_cluster 
values      : 1, 4  (min, max)
attributes  :
 ID Rowid   COUNT
  1     0  463524
  2     1 4118997
  3     2 3390160
  4     3 3218998

> boxplot(DEM, Cluster, xlab="Cluster", ylab="Elevation")
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:0: unexpected end of input
1:  ~ 
   ^
In addition: Warning message:
In .local(x, ...) : taking a sample of 1e+05 cells

Update:
I just found a working example, which does exactly what I want. However if I run it with my own data I always get above error. Maybe someone could explain the error message. Would be really appreciated.

r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
r1[] <- rnorm(ncell(r1), 100, 40)
r2[] <- rnorm(ncell(r1), 80, 10)
r3[] <- rnorm(ncell(r1), 120, 30)
s <- stack(r1, r2, r3)
names(s) <- c('A', 'B', 'C')  

rc <- round(r1[[1]]/100)

hist(rc)
summary(rc)

boxplot(s[[1]],rc)

Okey I found an answer, I don't know exactly why but it works for me:
I had to create a brick and then I could use the boxplot as mentioned above.

s <- stack(DEM, Cluster)
sbrick <- brick(s)  
boxplot(sbrick[[1]], sbrick[[2]], xlab="Cluster", ylab="Elevation")

Resulting in this plot boxplot DEM against cluster groups
Thanks everyone for their help!

You can use bwplot function in rasterVis library. Here is an example from rasterVis:

library(raster)
library(rasterVis)

r <- raster(system.file("external/test.grd", package="raster"))
s <- stack(r, r*2)
bwplot(s,violin=FALSE,strip=strip.custom(strip.levels=TRUE))

It is not clear to me why you get that error. Perhaps you can run the code below and see for yourself:

x <- stack(DEM, Cluster)
s <- sampleRegular(s, 100000, useGDAL=TRUE)
cn <- colnames(s)
f <- as.formula(paste(cn[1], '~', cn[2]))
boxplot(f, data=s)

Perhaps you should only provide your raster values as a vector and let the boxplot() function do the rest by:

boxplot(values(DEM) ~ values(Cluster), xlab="Cluster", ylab="Elevation")

Note that this will only work if both DEM and Cluster are exactly the same extent and resolution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM