R - 用栅格创建boxplot

问题描述 投票:1回答:4

这可能相当简单,但我是R的新手。我已经尝试了一段时间,使用包光栅中的boxplot相互绘制两个栅格。

我有一个DEM栅格和一个包含4个群集组的分类栅格,我想将其用作手册中描述的“区域”:

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

x Raster *对象

y如果x是RasterLayer对象,则y可以是另一个RasterLayer,用于将x的值分组为“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

更新: 我刚刚找到了一个实际的例子,它完全符合我的要求。但是,如果我使用自己的数据运行它,我总是会遇到错误。也许有人可以解释错误信息。真的很感激。

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)
r boxplot r-raster
4个回答
1
投票

哦,我找到了答案,我不确切知道为什么,但它对我有用: 我必须创建一个砖,然后我可以使用上面提到的boxplot。

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

导致这个情节boxplot DEM against cluster groups 谢谢大家的帮助!


0
投票

您可以在bwplot库中使用rasterVis函数。以下是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))

0
投票

我不清楚你为什么会得到这个错误。也许您可以运行下面的代码并亲眼看看:

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)

0
投票

也许你应该只提供你的栅格值作为向量,让boxplot()函数完成其余的工作:

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

请注意,这仅在DEM和Cluster具有完全相同的范围和分辨率时才有效。

© www.soinside.com 2019 - 2024. All rights reserved.