make an own (different) color legend with spplot()

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

make an own (different) color legend with spplot()

Marcel J.
Hi!

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

The reason I'm asking: Usually spplot() automatically divides the data
into fitting slices and makes a color legend (also automatically).

I want to assign the slices myself and have a fixed scale instead of an
automatic/dynamic scale.

I think what I want gets clear in this example:

library(sp)

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

## DATA GENERATION
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
value is smaller than zero ...
meuse.grid$random[meuse.grid$random > 10] <- 10 # and bigger than ten
## DATA GENERATION FINISHED

## 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)) # here I
assign the levels I want to use in my plot!!!

spplot(meuse.grid, c("random"), col.regions = rainbow(100, start = 4/6,
end = 1)) # look at the color-legend - not so good.

The graphic itself is like I want it, but the legend doesn't look too
good. Although I assign 100 factors, I want just a few ticks in the
legend (and also just a few labels).

How can this be achieved?

Thank you!

Marcel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Dieter Menne
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.

Dieter

library(sp)
data(meuse.grid)
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
                    at=labelat,
                    labels=labeltext )))