0

How can I move the colorkey lables closer to the colorkey? The labels for the color breaks are too far from the colorkey. I would like them very close to the colorkey. Any thoughts?

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))
Christopher Stephan
  • 1,081
  • 16
  • 33
code123
  • 2,082
  • 4
  • 30
  • 53

2 Answers2

1

The reason is that in the layout argument you have 4 column and 3 rows. Reducing the number of rows should fix the problem.

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
  • 1,704
  • 3
  • 19
  • 42
  • @Geo-sp I guess the question states 'Move levelplot colorkey labels closer to colorkey'. How can I reduce the distance between the values below the colorkey and the colorkey itself? – code123 Sep 29 '17 at 23:03
  • Oh, sorry about that; did you check axis.margin argument? I think this this is something @Oscar Perpiñán can answer. – Geo-sp Sep 30 '17 at 21:29
  • @Geo-sp Oh! Not yet. I did not know axis.margin argument exists. Hopefully Oscar can help. – code123 Sep 30 '17 at 22:01
  • 1
    Sorry but the `axis.margin`argument is only useful for the margin (by the way, it is now deprecated because the argument `margin` is a list). Unfortunately, I cannot provide any solution for your question. – Oscar Perpiñán Oct 02 '17 at 11:16
1

You can reduce the distance by using grid.edit after calling the levelplot function and change the y parameter:

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"))
Christopher Stephan
  • 1,081
  • 16
  • 33