0

我怎样才能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))
4

2 回答 2

1

grid.edit您可以在调用levelplot函数并更改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"))
于 2017-10-16T19:45:08.373 回答
1

原因是在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))
于 2017-09-29T19:00:29.543 回答