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

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Mar 31 18:37:20 CEST 2006



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")
        })

HTH,

--sundar




More information about the R-help mailing list