Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lattice Plot - Add lines through mean of y values

Tags:

r

lattice

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)

enter image description here

Just adding lines is a real mess:

xyplot(y ~ x, data=d, groups=g, type=c('p','l'))

enter image description here

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'))

enter image description here

like image 478
Matthew Lundberg Avatar asked Mar 14 '23 03:03

Matthew Lundberg


1 Answers

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

enter image description here

like image 120
MrFlick Avatar answered Mar 16 '23 00:03

MrFlick