[S] trellis: xyplot(y~x, type ="l", subset=grp=="a") : bug or feature?

Jacob A. Wegelin (wegelin@stat.washington.edu)
Tue, 29 Sep 1998 12:54:23 -0700 (PDT)


The following function demonstrates unexpected behavior in xyplot.
Unexpected by me, at any rate.

> "fn980928a"<-
function()
{
# test the trellis bug.
x <- c(0:2, 0:2, c(0,2))
y <- c(1, 1, 1, 0.5, 0.5,0.5, 0, 0 )
grp <- factor(c("a", "a","a", "b", "b","b", "a", "a"))
DATA <- data.frame(x, y, grp)
print( xyplot(y ~ x, type = "l", subset = grp == "a", data = DATA))
DATA
}

> junk<-fn980928a()
> junk
x y grp
1 0 1.0 a
2 1 1.0 a
3 2 1.0 a
4 0 0.5 b
5 1 0.5 b
6 2 0.5 b
7 0 0.0 a
8 2 0.0 a

I would expect two horizontal lines to be drawn, one at y=1 and one
at y=0, period. Instead, an additional diagonal line is drawn, making
a Z.

One solution is to stick NA's at the beginning and end of every piece
that you want to plot. For instance:

> "stickNAatBoundaries"<-
function(x, group, toMakeNA)
{
# x is a data frame
assign("toMakeNA", toMakeNA, frame = 1)
stuff <- as.numeric(x[[group]])
stuff <- factor(as.character(cumsum(abs(c(0, diff(stuff))))))
x <- cbind(x, newGroup = stuff)
newx <- by(x, x$newGroup, function(chunk)
{
answer <- rbind(chunk[1, ], chunk, chunk[nrow(chunk), ])
answer[1, toMakeNA] <- answer[nrow(answer), toMakeNA] <- NA
answer$newGroup <- NULL
answer
}
)
newx <- do.call("rbind", newx)
row.names(newx) <- 1:nrow(newx)
newx
}

> junk2<-stickNAatBoundaries(x=junk, group="grp", toMakeNA="y")
> junk2
x y grp
1 0 NA a
2 0 1.0 a
3 1 1.0 a
4 2 1.0 a
5 2 NA a
6 0 NA b
7 0 0.5 b
8 1 0.5 b
9 2 0.5 b
10 2 NA b
11 0 NA a
12 0 0.0 a
13 2 0.0 a
14 2 NA a

> print( xyplot(y ~ x, type = "l", subset = grp == "a", data = junk2))

The resulting plot looks more like one would expect.

Is the "problem" I encountered a bug or a feature?

Is there a more elegant or less computationally demanding solution?

Thanks for your patience and for any comments.

Jake Wegelin

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news