[R] xyplot, type="b"

Paul Murrell p.murrell at auckland.ac.nz
Fri Jun 23 04:33:21 CEST 2006


Hi


Benjamin Tyner wrote:
> 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.)


Here's one way to vectorize the lines ...

my.panel2 <- function(x, y, cex=1, ...){
    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)
    dx <- diff(x)
    dy <- diff(y)
    f <- 1 / sqrt((dx/a)^2 + (dy/b)^2)
    n <- length(x)
    x1 <- x[1:(n-1)] + f*dx
    x2 <- x[2:n] - f*dx
    y1 <- y[1:(n-1)] + f*dy
    y2 <- y[2:n] - f*dy
    i <- f < 0.5
    panel.segments(x1[i], y1[i], x2[i], y2[i])
}

Paul


> 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
>> 
>>
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list