Hi, How might I use xyplot to plot segments where the segments are in the input data? (ie a directed acyclic forest). Here's an example in base graphics: n = data.frame(id = c(1,2,3,4), parent = c(0,1,2,2), value = c(5,5.5,7,3), date = c(1,2,3,3.5)) plot(n$date, n$value) do.call( segments, with( merge(n,n,by.x="parent", by.y="id"), data.frame(x0=date.x, y0=value.x, x1=date.y, y1=value.y) ) ) xyplot only seems to take an x and a y, and panels only seem to get x and y. I can see an lsegments, but I can't see how to use it in this example. -Alex
On Feb 8, 2008 9:36 AM, Alex Brown <fishtank at compsoc.man.ac.uk> wrote:> Hi, > > How might I use xyplot to plot segments where the segments are in the > input data? (ie a directed acyclic forest). > > Here's an example in base graphics: > > n = data.frame(id = c(1,2,3,4), parent = c(0,1,2,2), value > c(5,5.5,7,3), date = c(1,2,3,3.5)) > > plot(n$date, n$value) > > do.call( > segments, > with( > merge(n,n,by.x="parent", by.y="id"), > data.frame(x0=date.x, y0=value.x, x1=date.y, y1=value.y) > ) > ) > > xyplot only seems to take an x and a y, and panels only seem to get x > and y. > > I can see an lsegments, but I can't see how to use it in this example.It's straightforward with ggplot2: install.packages("ggplot2") library(ggplot2) df <- data.frame(id = c(1,2,3,4), parent = c(0,1,2,2), value c(5,5.5,7,3), date = c(1,2,3,3.5)) selfmerge <- merge(df, df, by.x="parent", by.y="id") qplot(date, value, data=df) + geom_segment(data=selfmerge, aes(x=date.x, xend=date.y, y=value.x, yend=value.y)) You can read more at http://had.co.nz/ggplot2/ Hadley -- http://had.co.nz/
On 2/8/08, Alex Brown <fishtank at compsoc.man.ac.uk> wrote:> Hi, > > How might I use xyplot to plot segments where the segments are in the > input data? (ie a directed acyclic forest). > > Here's an example in base graphics: > > n = data.frame(id = c(1,2,3,4), parent = c(0,1,2,2), value > c(5,5.5,7,3), date = c(1,2,3,3.5)) > > plot(n$date, n$value) > > do.call( > segments, > with( > merge(n,n,by.x="parent", by.y="id"), > data.frame(x0=date.x, y0=value.x, x1=date.y, y1=value.y) > ) > ) > > xyplot only seems to take an x and a y, and panels only seem to get x > and y. > > I can see an lsegments, but I can't see how to use it in this example.The direct translation would be xyplot(value ~ date, data = n, data.lines = merge(n,n,by.x="parent", by.y="id"), panel = function(x, y, data.lines, ...) { panel.xyplot(x, y, ...) with(data.lines, lsegments(x0=date.x, y0=value.x, x1=date.y, y1=value.y)) }) (lsegments seems unable to work with a list currently, so that part is slightly different.) Things would be a bit more interesting if you had a multipanel plot, in which case you would need a more general solution involving subscripts. -Deepayan