[R] user coordinates and rug plots in lattice graphics

Paul Murrell p.murrell at auckland.ac.nz
Mon Apr 8 22:53:47 CEST 2002


Hi


> Using current.viewport()$yscale does the trick -- for example:


One note of warning about this approach -- this is poking around in the
internal structure of grid viewports, which are not guaranteed to stay that
way.  This is not your fault of course because I have failed to supply
decent (or any) support for querying grid's coordinate system state :)

OTOH, there is a "better" way to do this -- at least, there is a way to do
this which is more inkeeping with grid's philosophy.  If you want the rug
lines anchored at the bottom of the plot region, the easy way to specify
that is to use grid's "npc" coordinates -- bottom-left = (0, 0) and
top-right = (1, 1).  Furthermore, you could draw the rug lines as actual
line segments and make use of grid's other coordinate systems to control the
height of the rug lines properly.  Here are a couple of alternative xyplot
calls to show what I mean ...

     # rug lines are 3mm high
     xyplot(y~x|f,
            panel = function(x,y,...) {
              panel.xyplot(x,y,...)
              grid.segments(x, unit(0, "npc"), x, unit(3, "mm"),
                            default.units="native")
         })
     # rug lines are 1/20th of the height of the plot
     xyplot(y~x|f,
            panel = function(x,y,...) {
              panel.xyplot(x,y,...)
              grid.segments(x, unit(0, "npc"), x, unit(0.05, "npc"),
                            default.units="native")
         })

... of course, the downside is that you have to encounter the raw grid
functions to do this.

Paul


>      x <- rnorm(50)
>      y <- rnorm(50)
>      f <- factor(sample(c('a','b','c'),size=50, replace=T))
>      par(tck = 0.03)
>      z <- xyplot(y~x|f,
>          panel = function(x,y,...) {
>                  panel.xyplot(x,y,...)
>                  ymin <- current.viewport()$yscale[1]
>                  lpoints(x, rep(ymin, length(x)), pch="|", col='black')
>          })
>      z
>
> Thank you very much,
>   John
>
> At 09:09 AM 4/8/2002 -0700, Deepayan Sarkar wrote:
>
> >--- John Fox <jfox at mcmaster.ca> wrote:
> > > Dear R list members,
> > >
> > > I'd like to produce rug plots at the bottom of panels in a trellis
display
> > > (using the lattice package), but par("usr") doesn't return user
> > coordinates
> > > for panels, and consequently rug fails, as the following example
> > (suggested
> > > to me by Georges Monette) illustrates:
> > >
> > >      > x <- rnorm(50)
> > >      > y <- rnorm(50)
> > >      > f <- factor(sample(c('a','b','c'),size=50, replace=T))
> > >      > z <- xyplot(y~x|f,
> > >      +        panel = function(x,y,...) {
> > >      +                panel.xyplot(x,y,...)
> > >      +                rug(x)
> > >      +                print(par('usr'))
> > >      +        })
> > >      > z
> > >      [1] 0 1 0 1
> > >      [1] 0 1 0 1
> > >      [1] 0 1 0 1
> > >      Warning messages:
> > >      1: some values will be clipped in: rug(x)
> > >      2: some values will be clipped in: rug(x)
> > >      3: some values will be clipped in: rug(x)
> > >
> > > (R Version 1.4.1 on a Windows 2000 PC.)
> > >
> > > This code runs properly in S-PLUS, by the way, where par("usr")
returns
> > > user coordinates when invoked within a panel function.
> >
> >The problem arises because lattice is based on grid graphics, and
conventional
> >R graphics do not work under grid. The way around (to get the user
> >coordinates) here would be to use current.viewport()$xscale and
> >current.viewport()$yscale (both should be vectors of length 2).
> >
> > > I don't see a way around the problem. If I could determine the minimum
> > > value of y in a panel, I could make my own rug plot. Any help would be
> > > greatly appreciated.
> >
> >For this, you might want to use lsegments. (This may be slow now, but
should
> >be faster in the next release.)
>
> -----------------------------------------------------
> John Fox
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada L8S 4M4
> email: jfox at mcmaster.ca
> phone: 905-525-9140x23604
> web: www.socsci.mcmaster.ca/jfox
> -----------------------------------------------------
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list