绘制具有指数色或分位数色带偏差在零附近的栅格 [英] Plotting a raster with exponential or quantile color ramp diverging around zero

查看:142
本文介绍了绘制具有指数色或分位数色带偏差在零附近的栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rasterVis包中的R函数levelplot()来绘制三个栅格的堆栈,并带有一个发散的色带.我想更改光栅色带的比例,以使地图突出显示较低值的差异.这可以通过颜色中断的非线性合并来完成.

I am using the R function levelplot() from the rasterVis package to plot a stack of three rasters with a single diverging color ramp. I would like to change the scale of a raster color ramp so that the map accentuates differences in lower values. This can be done by non-linear binning of the color breaks.

我正在使用要点中的代码://stackoverflow.com/users/489704/jbaums> @jbaums (下面包含代码).关于如何在此代码中调整色带以使中断遵循2 ^ x,但保留了最小值和最大值的任何建议?似乎改变s(如下)的序列将具有预期的效果.

I'm using the code from a gist written by @jbaums (code included below). Any suggestions on how to adjust the color ramp in this code so that the breaks follow 2^x but the min and max values are preserved? It would seem that changing the sequences of s (below) would have the desired effect.

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
}

这是一些应用此功能的代码:

And here is some code that applies this function:

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')

推荐答案

为此,您可以避免使用diverge0,而可以定义中断向量并让levelplot负责其余部分.

You can probably avoid diverge0 for this, instead defining your vector of breaks and letting levelplot take care of the rest.

library(rasterVis)

您可以手动定义中断:

b <- c(-10.289, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 20.578)

或使其自动化:

rng <- range(cellStats(s, range))
lim <- ceiling(log(abs(rng), 2))
b <- sort(c(0, unique(unlist(mapply(function(x, y) y*2^(0:x), lim, sign(rng))))))
b[1] <- rng[1]
b[length(b)] <- rng[2]

然后将中断点传递给at,并将刻度位置/标签传递给colorkey$labels:

Then pass the breaks to at, and the tick locations/labels to colorkey$labels:

p <- levelplot(s, par.settings=RdBuTheme(), at=b, 
               colorkey=list(height=0.8, labels=list(at=b, labels=round(b, 2))))
p

这篇关于绘制具有指数色或分位数色带偏差在零附近的栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆