我可以使用 Lattice auto.key 或 key 来制作图例,其中一些数据是点,而另一些数据是线吗?

Can I use Lattice auto.key or key to make a legend with points for some data and lines for others?

我经常使用 distribute.type 分配图形类型,将观察到的数据表示为点,将模型预测的数据表示为线。有没有办法制作一个图例,只显示点数据的点,线数据的线? auto.key 默认值是点,如果我添加带有“list(lines=TRUE)”的行,图例会显示每个数据标签的点和线:

x <- seq(0, 8*pi, by=pi/6)

Y1pred <- sin(x)
Y1obs <- Y1pred + rnorm(length(x), mean=0, sd=0.2)
Y2pred <- cos(x)
Y2obs <- Y2pred + rnorm(length(x), mean=0, sd=0.4)
 
xyplot(Y1obs + Y2obs + Y1pred + Y2pred ~ x, 
        type=c('p','p','l','l'), 
        distribute.type=TRUE,
        auto.key=list(lines=TRUE, columns=2)
        )

第 1 页有一个使用 'key' 的相当复杂的示例。 Deepayans关于格子的书的158,我想知道是否有简单的选择?

是的,在 S 之后,keylines 组件支持不同的 type-s(但不支持 points)。使用 auto.key,你可以做到

xyplot(Y1obs + Y2obs + Y1pred + Y2pred ~ x, 
       type=c('p','p','l','l'), 
       distribute.type=TRUE,
       auto.key = list(points = FALSE, lines = TRUE,
                       columns = 2,
                       type = c('p','p','l','l')))

理想情况下,您希望将 type 仅放在 lines 组件中,如果您使用 key,则应该这样做。对于auto.key,反正只能一行,这样应该没问题。