R xyplot & ldquo; Steps & rdquo; Centered on data points

advertisements

the type argument to xyplot() can take "s" for "steps." From help(plot):

The two step types differ in their x-y preference: Going from (x1,y1) to (x2,y2) with x1 < x2, 'type = "s"' moves first horizontal, then vertical, whereas 'type = "S"' moves the other way around.

i.e. if you use type="s", the horizontal part of the step has its left end attached to the data point, while type="S" has its right end attached to the data point.

library(lattice)
set.seed(12345)
num.points <- 10
my.df <- data.frame(x=sort(sample(1:100, num.points)),
                    y=sample(1:40, num.points, replace=TRUE))

xyplot(y~x, data=my.df, type=c("p","s"), col="blue", main='type="s"')
xyplot(y~x, data=my.df, type=c("p","S"), col="red", main='type="S"')

How could one achieve a "step" plot, where the vertical motion happens between data points points, i.e. at x1 + (x2-x1)/2, so that the horizontal part of the step is centered on the data point?

Edited to include some example code. better late than never I suppose.


I am using excellent @nico answer to give its lattice version. Even I am ok with @Dwin because the question don't supply a reproducible example, but customizing lattice panel is sometimes challenging. The idea is to use panel.segments which is the equivalent of segments of base graphics.

library(lattice)
xyplot(y~x,
       panel =function(...){
         ll <- list(...)
         x <- ll$x
         y <- ll$y
         x.start <- x - (c(0, diff(x)/2))
         x.end   <- x + (c(diff(x)/2, 0))
         panel.segments(x.start, y, x.end, y, col="orange", lwd=2)
         panel.segments(x.end[-length(x.end)], y[1:(length(y)-1)],
                        x.end[-length(x.end)], y[-1], col="orange", lwd=2)
         ## this is optional just to compare with type s
         panel.xyplot(...,type='s')
         ## and type S
         panel.xyplot(...,type='S')
       })