Eric
Eric

Reputation: 147

levelplot: how to adjust scale levels (at) & scale labels (labels)

I have raster-objects with either categorical values (-1,0,1) or some values with skewed distributions that I want to plot with levelplot. I want to have different colors for negative values than for positive ones and white color for values around 0. I want the scale to show only few discrete steps as I want the spacing non-equi-distant and the ranges are sometimes very narrow. I use

colorkey=list(at=c(), labels=c())

to pass the arguments to levelplot. This works for the scale but the colorcoding of the actual values is not equal to what the scale displays. If I use the argument

at=c()

solely, scale-values and values in the lattice are corresponding. If I add labels=c() also independently they will not display at the scale but only at contour-lines. So my question:

How can I label my scale, set 'at', and have the values in the lattice correspond to the values of my scale all at the same time? Here is some example data:

library(raster)
library(colorspace)
library(rasterVis)
X <- raster(nrow=10,ncol=10)
set.seed(1)
X[] <- rchisq(df=10,n=10*10)*c(1,-1)
X[X[]>0] <- X[X[]>0]+seq(1,100,length.out=length(X[X[]>0]))
Uniques <- cellStats(X,stat=unique)
Uniques.max <- max(Uniques)
Uniques.min <- min(Uniques)
Acol.regions <- colorspace::diverge_hsv(n=9)

If I use colorkey:

levelplot(X, col.regions = Acol.regions,colorkey=list(
  at=round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2),
  labels=as.character(round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2)*2)),
          margin=F)

I end up with the correct way of displaying the scale but the values in the lattice are wrongly colored: correct scale but wrong values in the lattice

If I separately pass at=c(), labels=c() :

levelplot(X, col.regions = Acol.regions,
  at=round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2),
  labels=as.character(round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2)*2),
          margin=F)

I end up with corresponding values in the scale and lattice, but the labels are not displayed: enter image description here

Kind regards, Eric

Upvotes: 4

Views: 5881

Answers (1)

Eric
Eric

Reputation: 147

Solved it: I stated the at=c() argument twice. Once directly in levelplot and then in the colorkey-list

levelplot(X, col.regions = Acol.regions,at=round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2),
          colorkey=list(
  at=round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2),
  labels=as.character(round(c(seq(Uniques.min,0,length.out=5)[-5],0,seq(0,Uniques.max,length.out=5)[-1]),2)*2)),
          margin=F)

Thanks to anyone who might have had a try. Eric

Upvotes: 4

Related Questions