Re: [R] make an own (different) color legend with spplot()

From: Dieter Menne <>
Date: Fri, 04 Mar 2011 09:52:56 -0800 (PST)

Marcel J. wrote:
> Is there a way to manually costumize the color legend in an spplot() -
> especially where to draw ticks and labels for the ticks?

spplot calls levelplot, so the documentation there gives some help. In theory, the simplest approach would be to add a colorkey/ticknumber combination as shown below, but it's always a bit of trial-and-error what works. In similar cases, I have been bitten several times in the past by believing the whole approach is wrong, when only certain combinations don't work.

In the example, setting the width of the legend works, but trying to position it with space="right" fails, and tick.number alone also. Setting the items by hand, works. You will probably find a better labeling, I only added the hx to show you how-to.


gridded(meuse.grid) = ~x+y

meuse.grid$random <- rnorm(nrow(meuse.grid), 7, 2) # generate random data
meuse.grid$random[meuse.grid$random < 0] <- 0 # make sure there is no
meuse.grid$random[meuse.grid$random > 10] <- 10 # and bigger than ten
meuse.grid$random <- cut(meuse.grid$random, seq(0, 10, 0.1)) # here I

labelat = seq(0,100,by=10)
labeltext = paste("Hx",labelat)
spplot(meuse.grid, c("random"), col.regions = rainbow(100, start = 4/6,   end = 1),

    colorkey=list(width=0.3,     # works
                  space="right", # not honoured
                  tick.number=5, # not honoured, can be left out
                  labels=list( # so we must do it by hand
                    labels=labeltext )))

View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 04 Mar 2011 - 18:53:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Mar 2011 - 19:00:21 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive