R - 用栅格创建箱线图
R - create boxplot with rasters
这可能相当简单,但我是 R 的新手。我已经尝试了一段时间,使用 raster 包中的 boxplot 将两个栅格相互绘制。
我有一个 DEM 栅格和一个包含 4 个聚类组的分类栅格,我想将其用作 'zones',如手册中所述:
boxplot(x, y=NULL, maxpixels=100000, ...)
x 光栅* 对象
y 如果 x 是一个 RasterLayer 对象,y 可以是一个额外的 RasterLayer 来分组
‘zone’
的 x 值
> 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)
您可以使用 rasterVis
库中的 bwplot
函数。
这是来自 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))
我不清楚为什么会出现该错误。也许您可以 运行 下面的代码并亲自查看:
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)
好的,我找到了答案,我不知道为什么,但它对我有用:
我必须制作一块砖,然后才能使用上面提到的箱线图。
s <- stack(DEM, Cluster)
sbrick <- brick(s)
boxplot(sbrick[[1]], sbrick[[2]], xlab="Cluster", ylab="Elevation")
导致这个情节boxplot DEM against cluster groups
感谢大家的帮助!
也许你应该只提供你的栅格值作为一个矢量,让 boxplot()
函数通过以下方式完成剩下的工作:
boxplot(values(DEM) ~ values(Cluster), xlab="Cluster", ylab="Elevation")
请注意,这仅在 DEM 和 Cluster 的范围和分辨率完全相同时才有效。
这可能相当简单,但我是 R 的新手。我已经尝试了一段时间,使用 raster 包中的 boxplot 将两个栅格相互绘制。
我有一个 DEM 栅格和一个包含 4 个聚类组的分类栅格,我想将其用作 'zones',如手册中所述:
boxplot(x, y=NULL, maxpixels=100000, ...)
x 光栅* 对象
y 如果 x 是一个 RasterLayer 对象,y 可以是一个额外的 RasterLayer 来分组 ‘zone’
的 x 值> 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)
您可以使用 rasterVis
库中的 bwplot
函数。
这是来自 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))
我不清楚为什么会出现该错误。也许您可以 运行 下面的代码并亲自查看:
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)
好的,我找到了答案,我不知道为什么,但它对我有用:
我必须制作一块砖,然后才能使用上面提到的箱线图。
s <- stack(DEM, Cluster)
sbrick <- brick(s)
boxplot(sbrick[[1]], sbrick[[2]], xlab="Cluster", ylab="Elevation")
导致这个情节boxplot DEM against cluster groups
感谢大家的帮助!
也许你应该只提供你的栅格值作为一个矢量,让 boxplot()
函数通过以下方式完成剩下的工作:
boxplot(values(DEM) ~ values(Cluster), xlab="Cluster", ylab="Elevation")
请注意,这仅在 DEM 和 Cluster 的范围和分辨率完全相同时才有效。