[英]Match color with numbers Raster Map R
I would like to match color to raster class. 我想将颜色匹配到栅格类。 Assume I have the following colors and these rasters: 假设我有以下颜色和这些栅格:
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)))
I would like to classify the rasters in "s" such that values from 0 to 0.1 are colored in "darkred" and values from 0.9 to 1 are colored in "darkblue". 我想将栅格分类为“ s”,以使从0到0.1的值以“深红色”着色,从0.9到1的值以“深蓝色”着色。 Basically, I would like to classify my data to "n" classes and then assign each class to a color of my choice. 基本上,我想将数据分类为“ n”个类别,然后将每个类别分配给我选择的颜色。
Finally,the colorkey should have as labels the various raster classes. 最后,colorkey应该具有各种栅格类的标签。 Some insights are found here 1 and 2 在这里找到一些见解1和2
You have to convert each layer to a factor with ratify
using the cut
function to define the breaks. 您必须使用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)
Now choose your favorite colors and you are done. 现在选择您喜欢的颜色,您就完成了。
levelplot(sFactor, col.regions = mycolors)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.