可以使用不同模式创建多层对象吗?

时间:2013-04-30 00:05:49

标签: r raster

raster对象(在R中)是否有不同模式的层(数据类型)?

从表面上看,似乎我们总是被迫使用一种类型:

library(raster)
## create a SpatialPixelsDataFrame with (trivially) two different "layer" types
d <- data.frame(expand.grid(x = 1:10, y = 2:11), z = 1:100, a = sample(letters, 100, replace = TRUE), stringsAsFactors = FALSE)
coordinates(d) <- 1:2
gridded(d) <- TRUE

## now coerce this to a raster brick or stack and our "a" is crushed to numeric NA
all(is.na(getValues(brick(d)[[2]])))
[1] TRUE

是否有类似rasterDataFrame的内容?

另外,请注意,我们可能无法使用R因子,因为栅格@ data是一个矩阵,或者强制为数字/整数。我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

raster包提供了使用分类变量创建栅格的功能,rasterVis包中包含用于绘制它们的函数。 ratify函数允许栅格包含将基础栅格整数值与其他值(可以是字符)相关联的查找表。这直接允许在批准的栅格的级别部分中使用任何其他值模式。

这是一个例子。

library(rasterVis)


r <- raster(xmn = 0, xmx = 1, ymn = 0, ymx = 2, nrow = 10, ncol = 11, 
            crs = as.character(NA))
r[] <- sample(seq_along(letters[1:5]), ncell(r), replace = TRUE)

## ratify the raster, and set up the lookup table
r <- ratify(r)
rat <- levels(r)[[1]]
rat$value <- letters[1:5]
rat$code <- 1:5

## workaround for limitation as at 2013-05-01
## see https://stat.ethz.ch/pipermail/r-sig-geo/2013-May/018180.html
rat$code <- NULL
levels(r) <- rat

levelplot(r)

rasterVis即将发布更新,使上述解决方法变得不必要。