I want to print a lattice::xyplot
with grouped points and lines, but I have multiple y
values for many of the individual x
values in each group. I want a segmented line printed, so that for each x
value, it passes through the mean of the relevant y
values in each group.
Here's an example:
Using this data:
set.seed(1)
d <- data.frame(x=sample(6, 20, replace=TRUE), y=rnorm(20), g=factor(sample(2, 20, replace=TRUE)))
# Shift one group
d$y[d$g==2] = d$y[d$g==2] + 5
I've shifted one group so the lines are more visually appealing.
A scatter plot looks like this:
xyplot(y ~ x, data=d, groups=g)
Just adding lines is a real mess:
xyplot(y ~ x, data=d, groups=g, type=c('p','l'))
It's a bit better if you sort the x
values, but still not what I want:
xyplot(y ~ x, data=d[order(d$x),], groups=g, type=c('p','l'))
I would use panel.superpose
and then do the aggregation in the group panel function. For example
xyplot(y ~ x, data=d, groups=g, panel=function(...) {
panel.xyplot(...);
panel.superpose(..., panel.groups=function(x, y, col.line, ...) {
dd <- aggregate(y~x, data.frame(x,y), mean)
panel.xyplot(x=dd$x, y=dd$y, col=col.line, type="l")
})
})
This results in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With