【问题标题】:Match particular values with specific color in colorkey levelplot R在colorkey levelplot R中将特定值与特定颜色匹配
【发布时间】:2015-03-24 20:19:18
【问题描述】:
library(raster)
library(rasterVis)

我想为我的地图制作一个自定义颜色键。我的数据涵盖负值和正值。这是我尝试做的:

    myat = unique(c(seq( -0.1497458,0,length=6),seq(0,0.1665365,length=6)))
        themes<- colorRampPalette((c("darkred", "red3","red","orange", "yellow","yellow4")))#(length(myat)-1)

myColorkey <- list(at=myat,space = "bottom",labels=list(cex=1,font=1,at=myat),height=1,width=1)
if (dev.cur() == 1) x11(width=4,height=4)

#s <- stack(Precip_DJF,Precip_DJFsim)
levelplot(Precip_DJFtmindiff, layout=c(1, 1), col.regions=themes, 
          colorkey=myColorkey,margin=FALSE,xlab=NULL,ylab=NULL,par.strip.text=list(cex=0),scales=list(x=list(draw=FALSE),y=list(draw=FALSE)))

结果:

问题:如何将值 0 与黄色匹配,然后将颜色键标签放置在中断的中点而不是边缘?我需要cuts=10。

谢谢, AT。

reproducible example here

【问题讨论】:

    标签: r maps raster r-raster levelplot


    【解决方案1】:

    this book 我提供an example 来解决这个问题。我的回答主要是该示例的一部分的副本。

    由于您没有提供数据集来重现您的代码,我将使用书中建议的文件:

    library(rasterVis)
    
    setwd(tempdir())
    myURL <-'https://github.com/oscarperpinan/spacetime-vis/raw/master/data/'
    download.file(paste0(myURL,'SISav.grd'), 'SISav.grd', method = 'wget')
    download.file(paste0(myURL,'SISav.gri'), 'SISav.gri', method = 'wget')
    
    SISav <- raster('SISav')
    

    下一行定义发散调色板:

    meanRad <- cellStats(SISav, 'mean')
    SISav <- SISav - meanRad
    ## Modify the palette with your colors
    divPal <- brewer.pal(n=9, 'PuOr')
    divPal[5] <- "#FFFFFF"
    divTheme <- rasterTheme(region=divPal)
    
    rng <- range(SISav[])
    ## Number of desired intervals
    nInt <- 15
    ## Increment corresponding to the range and nInt
    inc0 <- diff(rng)/nInt
    ## Number of intervals from the negative extreme to zero
    n0 <- floor(abs(rng[1])/inc0)
    ## Update the increment adding 1/2 to position zero in the center of an interval
    inc <- abs(rng[1])/(n0 + 1/2)
    ## Number of intervals from zero to the positive extreme
    n1 <- ceiling((rng[2]/inc - 1/2) + 1)
    ## Collection of breaks
    breaks <- seq(rng[1], by=inc, length= n0 + 1 + n1)
    ## Midpoints computed with the median of each interval
    idx <- findInterval(SISav[], breaks, rightmost.closed=TRUE)
    mids <- tapply(SISav[], idx, median)
    ## Maximum of the absolute value both limits
    mx <- max(abs(breaks))
    ## Interpolating function that maps colors with [0, 1]
    ## rgb(divRamp(0.5), maxColorValue=255) gives "#FFFFFF" (white)
    break2pal <- function(x, mx, pal){
        ## x = mx gives y = 1
        ## x = 0 gives y = 0.5
        y <- 1/2*(x/mx + 1)
        rgb(pal(y), maxColorValue=255)
    }
    divRamp <- colorRamp(divPal)
    ## Diverging palette where white is associated with the interval
    ## containing the zero
    pal <- break2pal(mids, mx, divRamp)
    

    最后levelplot 显示结果:

    levelplot(SISav, par.settings=rasterTheme(region=pal),
              at=breaks, contour=TRUE)
    

    【讨论】:

    • 酷!为了重现这一点,我必须在下载行之后添加library(rasterVis)。如果这可以做得更简单,那就太好了。
    • @EdzerPebesma 已编辑。谢谢。我同意,但我找不到更简单的方法。也许我会将它集成到levelplot 代码中作为附加选项。
    • 问题在于atregion 输入了两条必须一致的信息。也许我们可以使用一个新参数,例如diverge = 0(默认为NA?)来指定发散色带的中点;将此添加到 sp 以供 sp:image、spplot、raster、rasterVis:levelplot 等统一重用?
    • 那就完美了。
    • @OscarPerpiñán 图像看起来非常好。您解决了部分问题(指定颜色),但我从 Edzer Pebesma 的 cmets 了解到,目前不可能将标签放在颜色中断的中心。请使用我的数据(参见编辑后的帖子)来执行您的脚本。我尝试使用您的数据运行脚本,但无法访问数据。
    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2015-06-06
    • 2019-10-22
    相关资源
    最近更新 更多