【问题标题】:Move levelplot colorkey labels closer to colorkey将 levelplot colorkey 标签移近 colorkey
【发布时间】:2017-10-20 01:30:34
【问题描述】:

如何将colorkey lables 移近colorkey?颜色中断的标签离颜色键太远。我希望它们非常接近颜色键。有什么想法吗?

library(raster)
library(rasterVis)
library(colorRamps)

set.seed(100)
ras <- raster(ncol=100, nrow=100)
ras1 <- setValues(ras, (1:ncell(ras))/100 + rnorm(ncell(ras)) - 50)

s=stack(ras1,ras1,ras1,ras1)

color_levels=14 #the number of colors to use
max_abolute_value=max(abs(c(cellStats(s, min), cellStats(s, max)))) #what is the maximum absolute value of raster?
    color_sequence=unique(round(seq(-max_abolute_value,max_abolute_value,length.out=color_levels+1),0))

myColorkey <- list(at=color_sequence,space = "bottom",tck = c(0,0), ## where the colors change 
                       labels=list(axis.line = list(col = NA),at=color_sequence,rot=0,cex=0.9,font=6,
                                   fontface=1),height=1,width=1.4)

col1 <- colorRampPalette(c("darkred", "red3","red", "gray96", 
                           "lightskyblue", "royalblue3", "darkblue"))

levelplot(s,contour=F, layout=c(4, 1),  col.regions = col1,colorkey=myColorkey,margin=FALSE,xlab=NULL,ylab=NULL,par.strip.text=list(cex=0))

【问题讨论】:

    标签: r maps raster levelplot rastervis


    【解决方案1】:

    你可以在调用levelplot函数后使用grid.edit来缩短距离并改变y参数:

    library(raster)
    library(rasterVis)
    library(colorRamps)
    library(grid)
    
    set.seed(100)
    ras <- raster(ncol=100, nrow=100)
    ras1 <- setValues(ras, (1:ncell(ras))/100 + rnorm(ncell(ras)) - 50)
    
    s=stack(ras1,ras1,ras1,ras1)
    
    color_levels=14 #the number of colors to use
    max_abolute_value=max(abs(c(cellStats(s, min), cellStats(s, max)))) #what is the maximum absolute value of raster?
    color_sequence=unique(round(seq(-max_abolute_value,max_abolute_value,length.out=color_levels+1),0))
    
    myColorkey <- list(at=color_sequence,space = "bottom",tck = c(0,0), ## where the colors change 
                       labels=list(axis.line = list(col = NA),at=color_sequence,rot=0,cex=0.9,font=6,
                                   fontface=1),height=1,width=1.4)
    
    col1 <- colorRampPalette(c("darkred", "red3","red", "gray96", 
                               "lightskyblue", "royalblue3", "darkblue"))
    
    levelplot(s,contour=F, layout=c(4, 1),  col.regions = col1,colorkey=myColorkey,margin=FALSE,xlab=NULL,ylab=NULL,par.strip.text=list(cex=0))
    
    grid.edit("[.]colorkey.labels$", grep=TRUE, global=TRUE, y=unit(1.5, "mm"))
    

    【讨论】:

    • 您可以使用包含的示例来演示您的解决方案吗?谢谢。
    • @code123:更新了我的答案,包括您的示例代码。
    • 这很甜蜜。非常感谢。
    【解决方案2】:

    原因是在layout 参数中有4 column3 rows。减少rows 的数量应该可以解决问题。

    levelplot(s,contour=F, layout=c(4, 1),  col.regions = col1,colorkey=myColorkey,margin=FALSE,xlab=NULL,ylab=NULL,par.strip.text=list(cex=0))
    

    【讨论】:

    • @Geo-sp 我猜问题是“将 levelplot colorkey 标签移近 colorkey”。如何减少颜色键下方的值与颜色键本身之间的距离?
    • 哦,很抱歉;你检查了axis.margin参数吗?我认为这是@Oscar Perpiñán 可以回答的问题。
    • @Geo-sp 哦!还没有。我不知道axis.margin 参数存在。希望奥斯卡能提供帮助。
    • 抱歉,axis.marginargument 仅对边距有用(顺便说一下,它现在已被弃用,因为参数 margin 是一个列表)。很遗憾,我无法为您的问题提供任何解决方案。
    猜你喜欢
    • 2014-03-05
    • 2016-12-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多