将颜色与数字匹配 Raster Map R

Match color with numbers Raster Map R

我想将颜色与光栅匹配 class。假设我有以下颜色和这些光栅:

library(rasterVis)
mycolors=c("darkred","red3", "orange", "yellow", "lightskyblue", 
          "royalblue3","darkblue")
s <- stack(replicate(6, raster(matrix(runif(100), 10))))
levelplot(s, layout=c(3, 2), col.regions=mycolors, index.cond=list(c(1, 3, 5, 2, 4, 6)))

我想 class 化 "s" 中的栅格,这样 0 到 0.1 的值在 "darkred" 中着色,0.9 到 1 的值在 [=20 中着色=].基本上,我想将我的数据 class 化为 "n" classes,然后将每个 class 分配给我选择的颜色。

最后,colorkey 应该有各种光栅 classes 作为标签。可以在此处找到一些见解 1 and 2

您必须使用 cut 函数将每一层转换为具有 ratify 的因子以定义中断。

library(rasterVis)

s <- stack(replicate(6,
                     raster(matrix(runif(100), 10))))

brks <- seq(0, 1, .1)
## These are the labels to be included as levels in the RAT
labs <- levels(cut(s[], brks))

## Auxiliary function to convert each layer to a factor.
makeFactor <- function(r){
    r <- ratify(cut(r, brks))
    rat <- levels(r)[[1]]
    rat$int <- labs
    levels(r)[[1]] <- rat
    r
}

lFactor <- lapply(seq_len(nlayers(s)),
                  FUN = function(i) makeFactor(s[[i]]))
sFactor <- stack(lFactor)

现在选择你最喜欢的颜色就完成了。

levelplot(sFactor, col.regions = mycolors)