【问题标题】:How to create a boxplot containing the values of a classified raster for only two classes?如何创建仅包含两个类的分类栅格值的箱线图?
【发布时间】:2019-02-18 11:36:48
【问题描述】:

我有一个分类栅格,其中包含 12 个名为“1”、“2”、“3”等的类别,代表土地利用。我有第二个栅格,其中包含一些代表蒸散率的值。 我正在尝试为 2 类和 10 类的蒸散率创建箱线图。

我能够创建箱线图,但它们包含所有类,但我只想获得类 2 和 10。

下面的代码: r,包含 12 个类别的分类栅格和 evapo,包含蒸发量值的栅格

 boxplot(evapo, r)

任何帮助都会很棒!

【问题讨论】:

标签: r classification raster boxplot


【解决方案1】:

这是 Paulo 解决方案的变体

library(raster)
r <- raster(nc=10, nr=5)
r[] <- runif(ncell(r), min=10, max=20) * 2
s <- setValues(r, sample(c(1:4), replace = T, size=50))

rs <- stack(r, s)
names(rs) <- c('r', 's')

d <- as.data.frame(rs)
# all classes
boxplot(r~s, data= d)
# only class 2 and 4
boxplot(r~s, data=d[d$s %in% c(2,4), ])

如果您因为栅格太大而无法生成d,您可以取一个大样本来获取近似结果

n <- 10000
d <- data.frame(sampleRegular(rs, n))

【讨论】:

    【解决方案2】:
    library(raster)
    library(ggplot2)
    library(dplyr)
    r <- raster(nc=10, nr=5)
    r[] <- runif(ncell(r), min=10, max=20) * 2
    #plot(r)
    s <- setValues(r,
                   sample(c(1:4), replace = T, size=50)
                   )
    plot(s)
    ct <- crosstab(r,s, useNA=TRUE, long=TRUE)
    ct2 <- ct[ rep( seq(dim(ct)[1]), ct$Freq), ]
    
    ggplot(filter(ct2, layer.2 %in% c(1,2)), aes(y=as.numeric(layer.1), x=layer.2)) +
      geom_boxplot()
    

    【讨论】:

      【解决方案3】:

      由于无法查看您的数据集,我无法确定此解决方案是否适合您,但这应该可以帮助您。

      boxplot(evapo[which(class == 2 | 10)], r[which(class == 2 | 10)])
      

      或者,您可以从您的 evapo 和 r 生成“新”栅格,其中仅包含类为 2 和 10 的信息,然后使用这些生成箱线图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 1970-01-01
        • 1970-01-01
        • 2016-11-26
        • 2018-03-18
        • 1970-01-01
        相关资源
        最近更新 更多