[R] xyplot, type="b"

Benjamin Tyner btyner at gmail.com
Fri Jun 23 00:35:18 CEST 2006


For those who are interested, here is a solution using grid. This
preserves some of the original spirit of do_plot_xy, but is more
satisfactory when x and y are on different scales:

my.panel <- function(x, y, cex=1, ...){
require(grid)
panel.xyplot(x, y, cex = cex, ...)
a <- 0.5 * cex * convertWidth(unit(1, "char"), "native", valueOnly=TRUE)
b <- 0.5 * cex * convertHeight(unit(1, "char"), "native",valueOnly=TRUE)
for(i in 1:length(x)){
dx <- x[i] - x[i - 1]
dy <- y[i] - y[i - 1]
f <- 1 / sqrt((dx/a)^2 + (dy/b)^2)
if((i > 1) & identical(f < 0.5, TRUE)){
panel.lines(x = c(x[i - 1] + f * dx, x[i] - f * dx),
y = c(y[i - 1] + f * dy, y[i] - f * dy))
}
}
}

# example
n <- 50
dat <- data.frame(x = runif(n), y = runif(n))
dat <- dat[order(dat$x), ]
myplot <- xyplot(y~x, data = dat, pch=".", panel = my.panel)
print(myplot)

(It's a little slow due to looping panel.lines.)

Ben

Benjamin Tyner wrote:

>Unfortunately this is not quite right; to do it correctly it seems one
>has to address two problems:
>
>1. how to get the size of the default 'cex' to use for 'd' (do_plot_xy uses 'GConvertYUnits' to accomplish this)
>2. figure out how to achieve the same effect as what 'GConvert(&xx, &yy, USER, INCHES, dd)' does in do_plot_xy. Otherwise, the gap sizes are not constant.
>
>(1) sounds easy but I don't know the answer offhand. (2) seems more subtle. Any suggestions would be greatly appreciated.
>
>Ben
>  
>



More information about the R-help mailing list