我正在使用
我正在使用由@jbaums编写的一个代码片段(以下包括代码)。请问如何调整此代码中的颜色条,以使断点遵循2^x规则,但最小值和最大值保持不变?更改
rasterVis
软件包中的levelplot()
函数来绘制三个栅格图层,并采用单一发散颜色条。我想要改变栅格颜色条的比例尺,使得地图突出显示较低值之间的差异。这可以通过非线性分组实现。我正在使用由@jbaums编写的一个代码片段(以下包括代码)。请问如何调整此代码中的颜色条,以使断点遵循2^x规则,但最小值和最大值保持不变?更改
s
序列似乎会产生预期的效果。diverge0 <- function(p, ramp) {
# p: a trellis object resulting from rasterVis::levelplot
# ramp: the name of an RColorBrewer palette (as character), a character
# vector of colour names to interpolate, or a colorRampPalette.
require(RColorBrewer)
require(rasterVis)
if(length(ramp)==1 && is.character(ramp) && ramp %in%
row.names(brewer.pal.info)) {
ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp)))
} else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) {
ramp <- colorRampPalette(ramp)
} else if(!is.function(ramp))
stop('ramp should be either the name of a RColorBrewer palette, ',
'a vector of colours to be interpolated, or a colorRampPalette.')
rng <- range(p$legend[[1]]$args$key$at)
s <- seq(-max(abs(rng)), max(abs(rng)), len=1001)
i <- findInterval(rng[which.min(abs(rng))], s)
zlim <- switch(which.min(abs(rng)), `1`=i:(1000+1), `2`=1:(i+1))
p$legend[[1]]$args$key$at <- s[zlim]
p$par.settings$regions$col <- ramp(1000)[zlim[-length(zlim)]]
p
}
下面是应用该函数的一些代码:
library (rasterVis)
ras1 <- raster(nrow=10,ncol=10)
set.seed(1)
ras1[] <- rchisq(df=10,n=10*10)
ras2 <- ras1*(-1)/2
s <- stack(ras1,ras2)
p <- levelplot(s, par.settings=RdBuTheme())
diverge0(p, ramp='RdBu')
raster
对象完成这个任务,然后将它们传递到 ggplot 中。例如:as.data.frame(my_raster, xy = TRUE) %>% ggplot(aes(x,y, fill = cut_bins)) + geom_raster() + scale_fill_manual() + theme_void()
。然后在scale_fill_manual
中,您可以为每个区间指定值、断点和标签。完全掌控! - Rich Pauloo