[R-sig-Geo] Fwd: Re: simple spplot problem (colorkey inside the plotting area)

Stowasser Rainer rainer.stowasser at wissenschaftsrat.ac.at
Wed Mar 16 15:06:04 CET 2011


p <- spplot(...)
names(p$legend) <- "inside"
p$legend$inside$x <- 0.8
p$legend$inside$y <- 0.7
plot(p)

works perfect

Many thx for the fast solution

Rainer

-----Ursprüngliche Nachricht-----
Von: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] Im Auftrag von Edzer Pebesma
Gesendet: Mittwoch, 16. März 2011 14:33
An: sig-geo
Cc: deepayan.sarkar at gmail.com
Betreff: [R-sig-Geo] Fwd: Re: 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:
> Dear Rainer,
>
> spplot is a wrapper function around levelplot (for grids, polygons and
> lines) or xyplot (for points), both in package lattice.
>
> As the documentation mentions, the key.space argument is only available
> for points, where it is passed (as key argument) to xyplot.
>
> I looked up the documentation of ?levelplot (after loading lattice), and
> although it does have x and y componenents for the colorkey, it does
> explicitly mention that these are not implemented. The only things you
> can modify are the key height/width, and the space argument ('bottom',
> 'left', 'right', 'top'). I do not see a possibility to move the key to
> wherever you want.
>
> I cc:'d this message to Deepayan Sarkar, the lattice author, maybe he
> has a suggestion where to look next.

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



>
> On 03/16/2011 10:48 AM, Stowasser Rainer wrote:
>> Hy Georg
>> For me it does not work
>>
>> Here s the Code
>>
>> # Rdata from http://gadm.org/countryies for Austria Bundesländer
>> load("AUT_adm1.RData")
>> library(sp)
>> library(RColorBrewer)
>> #percent per Bundesland in the correct order
>> proznormal09=c(3.76,6.57,24.52,18.05,6.59,13.94,6.91,3.74,15.91)
>> #produce factor
>>  bundeslfakt <- as.factor(as.numeric(cut(proznormal09, c(0,4,7,10,13,16,19,100)))
>> levels(bundeslfakt) <- c("<4%", "4-7%", "13-16%", "16-19%",">19%")
>> gadm$bundeslfakt <-bundeslfakt
>> #the plot
>> spplot(gadm, "bundeslfakt", col.regions=brewer.pal(5,"Reds"), main="Prozentverteilung nach Herkunftsbundesl\u00E4nder",key.space=list(x=0,y=0,corner=c(0,0)))
>>
>> produces a plot with the colorkey on the right side
>> key.space is ignored
>>
>> I hope you can reproduce the Effect ;-)
>> or tell me where I went wrong
>>
>> Rainer
>>
>> -----Ursprüngliche Nachricht-----
>> Von: Georg Ruß [mailto:research at georgruss.de]
>> Gesendet: Dienstag, 15. März 2011 20:08
>> An: Stowasser Rainer
>> Cc: r-sig-geo at r-project.org
>> Betreff: Re: [R-sig-Geo] simple spplot problem (colorkey inside the plotting area)
>>
>> On 15/03/11 16:42:15, Stowasser Rainer wrote:
>>> I'm using spplot to color regions of Austria
>>> Everything works fine
>>> But I like to have the colorkey INSIDE the plotting area (in the Bavaria
>>> window :-)
>>>
>>> space just puts it outside (top, bottom, left, right)
>>>
>>> as the spplot is a lattice object it should be possible
>>>
>>> I've tried the examples from http://r-spatial.sourceforge.net/gallery/
>>> With the key.space=list(x=0.2,y=0.9,corner=c(0,1))
>>> But this is simply ignored
>>
>> Hi Rainer,
>>
>> I'm not sure whether I can help, but as soon as I include a
>> "key.space=list(x=0, y=0, corner=c(0,0))" into an spplot call, the color
>> key wanders into the plotting rectangle. The (minimal) full call here is:
>>
>> print(spplot(sppdf, zcol = i, key.space=list(x=0,y=0.01,corner=c(0,0))))
>>
>> (inside the postscript device for producing eps figures)
>>
>> If you have, maybe post some code that we can try; or the structure of
>> your data frame and some further lines to experiment with.
>>
>> Regards,
>> Georg.
>
> --
> Edzer Pebesma
> Institute for Geoinformatics (ifgi), University of Münster
> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
> http://www.52north.org/geostatistics      e.pebesma at wwu.de
>

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


More information about the R-sig-Geo mailing list