How do I reverse the colour key in a spplot so the that values are decreasing from the top to the bottom? Demo from https://edzer.github.io/sp/ below.

demo(meuse, ask = FALSE, echo = FALSE) 
spplot(meuse, "zinc", do.log = TRUE,
    key.space=list(x = 0.1, y = 0.95, corner = c(0, 1)),
    scales=list(draw = TRUE)) 

enter image description here

1 Answer 1


Create a factor column before plotting, then you have full control over the order. E.g. for your example:

vals <- meuse$zinc
# compute breaks (equivalent to `do.log = TRUE`)
breaks <- c(min(vals),
            tail(head(exp(seq(min(log(vals)), max(log(vals)), length=6)), -1), -1),
categories <- cut(vals, breaks, dig.lab=4, include.lowest=TRUE)

# add factor column to `SpatialPointsDataFrame`
meuse$zinc_cat <- forcats::fct_rev(categories)  # reverse order

spplot(meuse, "zinc_cat", key.space = list(x = 0.1, y = 0.95, corner = c(0, 1)))


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.