[R] lattice, xyplot, lend=1, type="h"

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Mar 31 18:44:56 CEST 2006


On 3/31/06, Sundar Dorai-Raj <sundar.dorai-raj at pdf.com> wrote:
>
>
> Andy Bunn wrote:
> > Is there is a way to make square bars in xyplot with type="h"?
> >
> > dat <- data.frame(foo = rep(1:10,2), bar = rep(1:10,2))
> > xyplot(foo~bar, data = dat, type="h",lwd=20)
> >
> > Thanks! Andy
> >
>
>
> Hi, Andy,
>
> I don't think lattice supports this since it uses
> trellis.par.get("plot.line") to determine the drawing parameters.
> However, you can use the grid package.
>
> library(lattice)
> library(grid)
> dat <- data.frame(foo = rep(1:10,2), bar = rep(1:10,2))
> xyplot(foo ~ bar, data = dat,
>         panel = function(x, y, ...) {
>          ## hacked from lattice::lplot.xy
>          ylim <- current.viewport()$yscale
>          zero <- if (ylim[1] > 0)
>              ylim[1]
>          else if (ylim[2] < 0)
>              ylim[2]
>          else 0
>          gp <- gpar(lty = 1, col = "blue", lwd = 20,
>                     alpha = 1, lineend = "square")
>          grid.segments(x0 = x, x1 = x, y0 = y, y1 = zero,
>                        gp = gp, default.units = "native")
>         })

Right. R 2.3.0 should have better support for grid parameters (that
are not anticipated by lattice), where both of the following should
work:

xyplot(foo~bar, data = dat, type="h",lwd=20,
       par.settings = list(grid.pars = list(lineend = 1)))

xyplot(foo~bar, data = dat, type="h",lwd=20, lineend = 1)

The first solution should already work in the current alpha of R
2.3.0, I'm testing the second one now.

Deepayan




More information about the R-help mailing list