Skip to content
Prev 11206 / 29559 Next

simple spplot problem (colorkey inside the plotting area)

I'm forwarding this to r-sig-geo on Deepayan's request.

Thanks, Deepayan!

-------- Original Message --------
Subject: Re: [R-sig-Geo] simple spplot problem (colorkey inside the
plotting area)
Date: Wed, 16 Mar 2011 18:53:46 +0530
From: Deepayan Sarkar <deepayan.sarkar at gmail.com>
To: Edzer Pebesma <edzer.pebesma at uni-muenster.de>
CC: r-sig-geo at r-project.org

On Wed, Mar 16, 2011 at 5:17 PM, Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote:
One option is to draw the legend in the panel function:

require(grid)

pleg <- levelplot(volcano)$legend ## cheat to get default legend details

levelplot(volcano, colorkey = FALSE,
          panel = function(...) {
              panel.levelplot(...)
              draw.colorkey(key = c(pleg$right$args$key, height = 0.6),
                            draw = TRUE,
                            vp = viewport(0.8, 0.5, default.units = "npc"))
          })


Adding support for (x, y) in colorkey is not technically that
challenging. You can produce the equivalent as follows:

p <- levelplot(volcano)
names(p$legend) <- "inside"
p$legend$inside$x <- 0.8
p$legend$inside$y <- 0.7
plot(p)

The only thing is that colorkeys are "expanding" (unlike regular keys
as in xyplot), and I have never been able to figure out a reasonable
expansion rule when it's placed in an arbitrary location. Maybe I
should just implement it as is and let users figure out something that
works for them.

-Deepayan