开发者

spplot() - make color.key look nice

开发者 https://www.devze.com 2023-02-14 09:40 出处:网络
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'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 comma开发者_JS百科nd 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.


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
        )
    )
)


First, it's not at all clear what you are wanting here. There are many ways to make the color.key look "nice" and that is to understand first the data being passed to spplot and what is being asked of it. cut() is providing fully formatted intervals like (2.3, 5.34] which will need to be handled a different way, increasing the margins in the plot, specific formatting and spacing for the labels, etc. etc. This just may not be what you ultimately want.

Perhaps you just want integer values, rounded from the input values?

library(sp)

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

meuse.grid$random <- rnorm(nrow(meuse.grid), 7, 2)

Round the values (or trunc(), ceil(), floor() them . . .)

meuse.grid$rclass <- round(meuse.grid$random)

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号