Skip to content
Prev 25796 / 398502 Next

grid package and lines

Hi
"Willemsen, Sten" wrote:
I think you are getting bitten by the fact that the locations for
drawing points and lines can be specified in a number of different
coordinate systems.  There is a default coordinate system for each
graphical primitive;  for points the default is "native" which means
that points are drawn relative to the x-scale and y-scale of the current
viewport, but for lines the default is "npc" which means that lines are
drawn relative to a "normalised" coordinate system [bottom-left corner
of the viewport is (0,0), top-right corner is (1,1)].  The defaults are
there so that you can ignore the use of "units" to specify a coordinate
system in some cases, but if the default isn't what you want then you
will have to get your hands dirty :)  [Complaints about selection of
inappropriate default units should be directed to me :(]

I hope the following example (comments included) helps to show what is
going on ...

    library(grid)
    x <- rnorm(10)
    y <- rnorm(10)
    push.viewport(plotViewport(c(5, 5, 3, 2)))
    push.viewport(dataViewport(x, y))
    grid.rect(gp=gpar(lty="dashed"))
    grid.xaxis()
    grid.yaxis()
    # default units for grid.points are "native"
    grid.points(x, y)
    # can specify other units
    grid.points(c(0, 0, 1, 1),
                c(0, 1, 1, 0),
                default.units="npc",
                pch=16)
    # default units for grid.lines are "npc"
    grid.lines(c(0, 0.5, 1, 0.5, 0),
               c(0.5, 1, 0.5, 0, 0.5),
               gp=gpar(col="green"))
    # can specify other units
    grid.lines(unit(x, "native"),
               unit(y, "native"))
    

Paul