对带有点和线的分组数据使用点阵 xyplot

时间:2021-01-19 16:12:28

标签: r lattice

我希望在点阵中制作一个单面板图,显示来自多个组 (g) 的数据 (y),并带有显示预测值 (y_pred) 的叠加线。我在下面生成示例数据:

d <- data.frame(x = rep(1:100,2), g = factor(rep(c('a','b'), each = 100))) 
d$y_pred <- -0.1*x + 0.001*x^2
d$y_pred <- with(d, ifelse(g=='a', y_pred+2,y_pred))
d$y <- d$y_pred + rnorm(nrow(d),0,1)

使用 'type=c('p','l'),distribute.type=TRUE 不起作用,我也没有尝试制作面板:

xyplot(y + y_pred ~ x, data = d,
    groups = g,
    panel = panel.superpose,
    panel.groups=function(...){
        panel.xyplot(x, y, type='p')
        panel.xyplot(x, y_pred, type='l')
    }
    )

我应该在这里做什么?

1 个答案:

答案 0 :(得分:0)

好的,你可以用latticeExtra来做到这一点

dat <- xyplot(y ~ x, data=d,
    groups = g,
    type="p"
    )
dat

dat + layer(panel.xyplot(x=x, y=y_pred,
        groups = g,
        type="l",
        subscripts=TRUE),
    data=d)

但这真的很挑剔而且不够健壮。如果“dat + layer()”代码是我在启动程序后运行的第一件事,它会正确呈现,但之后它会经常呈现不同组的几行缺失。

相关问题