Search code examples
rgraphicslattice

spplot() - make color.key look nice


I'm afraid I have a spplot() question again.

I want the colors in my spplot() to represent absolute values, not automatic values as spplot does it by default.

I achieve this by making a factor out of the variable I want to draw (using the command cut()). This works very fine, but the color-key doesn't look good at all.

See it yourself:

library(sp)

data(meuse.grid)
gridded(meuse.grid) = ~x+y

meuse.grid$random <- rnorm(nrow(meuse.grid), 7, 2)
meuse.grid$random[meuse.grid$random < 0] <- 0
meuse.grid$random[meuse.grid$random > 10] <- 10
# making a factor out of meuse.grid$ random to have absolute values plotted
meuse.grid$random <- cut(meuse.grid$random, seq(0, 10, 0.1)) 

spplot(meuse.grid, c("random"), col.regions = rainbow(100, start = 4/6, end = 1))

How can I have the color.key on the right look good - I'd like to have fewer ticks and fewer labels (maybe just one label on each extreme of the color.key)

Thank you in advance!

[edit] To make clear what I mean with absolute values: Imagine a map where I want to display the sea height. Seaheight = 0 (which is the min-value) should always be displayed blue. Seaheight = 10 (which, just for the sake of the example, is the max-value) should always be displayed red. Even if there is no sea on the regions displayed on the map, this shouldn't change. I achieve this with the cut() command in my example. So this part works fine.

THIS IS WHAT MY QUESTION IS ABOUT What I don't like is the color description on the right side. There are 100 ticks and each tick has a label. I want fewer ticks and fewer labels.


Solution

  • The way to go is using the attribute colorkey. For example:

    ## labels
    labelat = c(1, 2, 3, 4, 5)
    labeltext = c("one", "two", "three", "four", "five")
    
    ## plot
    spplot(meuse.grid,
        c("random"),
        col.regions = rainbow(100, start = 4/6, end = 1),
        colorkey = list(
            labels=list(
                at = labelat,
                labels = labeltext
            )
        )
    )