Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Last active February 15, 2024 09:42
Show Gist options
  • Save johnbaums/306e4b7e69c87b1826db to your computer and use it in GitHub Desktop.
Save johnbaums/306e4b7e69c87b1826db to your computer and use it in GitHub Desktop.
Plot a rasterVis::levelplot with a colour ramp diverging around zero
diverge0 <- function(p, ramp) {
# p: a trellis object resulting from rasterVis::levelplot
# ramp: the name of an RColorBrewer palette (as character), a character
# vector of colour names to interpolate, or a colorRampPalette.
require(RColorBrewer)
require(rasterVis)
if(length(ramp)==1 && is.character(ramp) && ramp %in%
row.names(brewer.pal.info)) {
ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp)))
} else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) {
ramp <- colorRampPalette(ramp)
} else if(!is.function(ramp))
stop('ramp should be either the name of a RColorBrewer palette, ',
'a vector of colours to be interpolated, or a colorRampPalette.')
rng <- range(p$legend[[1]]$args$key$at)
s <- seq(-max(abs(rng)), max(abs(rng)), len=1001)
i <- findInterval(rng[which.min(abs(rng))], s)
zlim <- switch(which.min(abs(rng)), `1`=i:(1000+1), `2`=1:(i+1))
p$legend[[1]]$args$key$at <- s[zlim]
p[[grep('^legend', names(p))]][[1]]$args$key$col <- ramp(1000)[zlim[-length(zlim)]]
p$panel.args.common$col.regions <- ramp(1000)[zlim[-length(zlim)]]
p
}
@johnbaums
Copy link
Author

Example

library(rasterVis)
r <- raster(matrix(runif(100, -10, 30), 10))
p <- levelplot(r, margin=FALSE, at=seq(-10, 30, 1))
diverge0(p, 'RdBu')

image

@FVFaleiro
Copy link

How can I revert the ramp from brewer.pal in the function?
For example:
rev(colorRampPalette(brewer.pal(11, "RdBu"))

@johnbaums
Copy link
Author

@FVFaleiro you can pass a colorRampPalette to the function, so just move the rev inside the colorRampPalette:

diverge0(p, colorRampPalette(rev(brewer.pal(11, "RdBu"))))

image

@johnbaums
Copy link
Author

Note that the tmap package has some useful functionality for this as well:

library(tmap)
tm_shape(r) +
  tm_raster(midpoint=0, style='cont', title='foo', palette='-RdBu',
            breaks=pretty(cellStats(r, range), 7), legend.reverse=TRUE) +
  tm_layout(legend.outside=TRUE)

image

@FVFaleiro
Copy link

Great, @johnbaums! Thanks!
How do you set the legend in the rasterVis to be in a continuous aspect? Aparently the default of levelplot have the colors like categories instead a smoth transition between the colors.

@johnbaums
Copy link
Author

johnbaums commented Nov 12, 2019

@FVFaleiro you just need to set enough breaks along the legend:

r <- raster::raster(matrix(runif(100), 10))
rasterVis::levelplot(r, at=seq(0, 1, length.out=100))

@FVFaleiro
Copy link

Thanks, @johnbaums!

@cjcarlson
Copy link

I just wanted you to know that I respect and cherish this gist more than any living family member or close friend of mine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment