Skip to content

Instantly share code, notes, and snippets.

@bjurban
Created April 30, 2015 22:08
Show Gist options
  • Save bjurban/fd2c332fd4b6ae68e8a3 to your computer and use it in GitHub Desktop.
Save bjurban/fd2c332fd4b6ae68e8a3 to your computer and use it in GitHub Desktop.
levelplot with linear color spacing
# Make a levelplot with a custum linear scale
# Author: Bryan Urban
# Email: burban at fraunhofer dot org
# Date: 2015-04-30
library(lattice)
library(RColorBrewer)
# make some data
foo <- foo <- expand.grid(x = 1:100, y=1:10)
foo$z <- rnorm(100*10)*10
# select color palette
my.cols <- rev(brewer.pal(11, "RdYlGn"))
# pick levels so each gradation contains 10% of the data
# my.at <- quantile(foo$z, seq(0, 1, length.out=21), na.rm=TRUE)
# ncuts <- length(my.at)-1
# - or -
# pick levels so each gradation is equally spaced
ncuts <- 10 # number of cuts
my.rng <- range(foo$z)
by <- ceiling((ceiling(my.rng[2]) - floor(my.rng)[1])/(ncuts))
my.at <- seq(from = floor(my.rng[1]),
to = floor(my.rng[1])+(ncuts+1)*by,
by=by)
cols <- colorRampPalette(my.cols)(ncuts+1)
#lp_temp <-
levelplot(z~reorder(x, z, mean, na.rm=TRUE)*
reorder(y, z, mean, na.rm=TRUE),
foo, # xlim=c(0,10),
ylab="y", xlab="x",
cuts= ncuts,
at=my.at,
col.regions=cols,
scales=list(y=list(at=""), # x=list(at=seq(0,10,by=2)),
alternating=c(1,0,1,0)),
colorkey=list(at=my.at,
labels=list(at=my.at)
#space="left"
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment