[R] lattice garphs: combining multiple scatterplots and addinglegend

Peter Ehlers ehlers at ucalgary.ca
Mon Apr 12 21:10:38 CEST 2010


Actually, Bert, auto.key can be used. See below.

On 2010-04-12 11:41, Bert Gunter wrote:
> There us no "groups" argument in your xyplot call, so how is auto.key
> supposed to define a legend? (and note that auto.key should be a logical not
> a list).
>
> Please re-read the auto.key section in the xyplot man page.
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
>   -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Jannis
> Sent: Monday, April 12, 2010 10:20 AM
> To: R-help at r-project.org
> Subject: [R] lattice garphs: combining multiple scatterplots and
> addinglegend
>
> Dear List members,
>
>
> its me again, fighting with lattice graphics. I am trying to plot a world
> map, add some points on different locations with different colors and add a
> legend, but did not succeed yet with the legend. Here is my code:
>
>
> library(fields)
>
> # Data for demonstration
>
> data_x = c(0,50,60)
> data_y = c(0,0,0)
> cols   = c(1,2,3)
> data(world.dat)
>
> #print map
> all=xyplot(world.dat$y  ~ world.dat$x,
>     type=c('l'),col="black",xlab="",ylab="",
>     ylim=c(-55,80),xlim=c(-170,175),pch=20,cex=0.2,
>     auto.key = list(x=0,y=0,text=c('test1','test2','test3')))
> print(all,position=c(0,0,1,.7),more=T)
>
> #add points
> trellis.focus(highlight=FALSE)
> lpoints(data_x,data_y,pch=20,col=cols)
> update(all,auto.key = list(x=0,y=0,text=c('test1','test2','test3')))
>
>
> The plots are produced correctly, but the legend is still missing. Could
> anyone give me some hints?
> There is probably a much more elegant way how to combine the two plots but i
> did not manage to understand the usage of these different panel functions.
> There is most probably no way around the Trellis book, but I could not yet
> buy it....
>

(Well, yes, you should buy it.)
I wouldn't bother with the trellis.focus bit - just put your
lpoints() into an appropriate panel function.

all <- xyplot(world.dat$y  ~ world.dat$x,
    type=c('l'),col="black",xlab="",ylab="",
    ylim=c(-55,80),xlim=c(-170,175),
    panel = function(x, y, ...){
      panel.xyplot(x, y, ...)
      lpoints(data_x, data_y, pch=20, col=cols, cex=1.2)
    },
    ## the next line is just to control the points symbol
    ## in the key.
    par.settings = list(superpose.symbol =
                     list(pch=20, cex=1.2, col=cols)),
    auto.key = list(space="right",
                    text=c('test1','test2','test3'),
                    points=TRUE)
)
print(all, position=c(0,0,1,.7))

Or, for possibly more flexibility, use 'key' (and you won't
need the par.settings):

all <- xyplot(world.dat$y  ~ world.dat$x,
    type=c('l'),col="black",xlab="",ylab="",
    ylim=c(-55,80),xlim=c(-170,175),
    panel=function(x,y,...){
      panel.xyplot(x,y,...)
      lpoints(data_x, data_y, pch=20, col=cols, cex=1.2)
    },
    key = list(space="right",
               text=list(labels=c('test1','test2','test3')),
               points=list(pch=20, cex=1.2, col=cols))
)
print(all, position=c(0,0,1,.7))

  -Peter Ehlers

>
> Thanks for your help
> Jannis
>
>
> __________________________________________________
> Do You Yahoo!?
> Sie sind S
> ssenmails.
> http://mail.yahoo.com
>
> ______________________________________________
> R-help at r-project.org 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.
>
> ______________________________________________
> R-help at r-project.org 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.
>
>

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list