r - 在 lattice xyplot 中绘制每组面板数据的第一个点

标签 r panel lattice

我正在尝试使用将符号叠加在每个组的第一个值上的组和面板来创建线图。此尝试仅绘制第一组的第一个点,对其他两组不执行任何操作。

library(lattice)
foo <- data.frame(x=-100:100, y=sin(c(-100:100)*pi/4))
xyplot( y~x | y>0,  foo, groups=cut(x,3),
        panel = function(x, y, subscripts, ...) {
          panel.xyplot(x, y, subscripts=subscripts, type='l', ...)
          # almost but not quite:
          panel.superpose(x[1], y[1], subscripts=subscripts, cex=c(1,0), ...) 
        } )

将不胜感激通用解决方案,以允许绘制每个组和面板中的特定点(例如,第一、中间和端点)。

enter image description here

最佳答案

(感谢这个很好的 lattice 问题。)您应该使用下标,因为它是为面板选择单个数据点的机制:在这里您要选择 groups按面板:groups[subscripts]。拥有正确的分组变量后,您可以使用它来拆分数据并选择每个组的第一个元素:

    ## first points
    xx = tapply(x,groups[subscripts],'[',1) 
    yy = tapply(y,groups[subscripts],'[',1)
    ## end points
    xx = tapply(x,groups[subscripts],tail,1) 
    yy = tapply(y,groups[subscripts],tail,1)

您使用 panel.points 绘制点(比基本的 panel.superpose 更高级别)。

library(lattice)
foo <- data.frame(x=-100:100, y=sin(c(-100:100)*pi/4))
xyplot( y~x | y>0,  foo, groups=cut(x,3),
        panel = function(x, y, subscripts, groups,...) {
          panel.xyplot(x, y, subscripts=subscripts, type='l',groups=groups, ...)
          # Note the use `panel.points` and the grouping using groups[subscripts]
          panel.points(tapply(x,groups[subscripts],'[',1), 
                       tapply(y,groups[subscripts],'[',1), 
                       cex=2,pch=20, ...) 
        } )

enter image description here

如果您想根据颜色组为点着色,您应该向 panel.points 添加一个 groups 参数。 (我把这个留给你作为练习)

关于r - 在 lattice xyplot 中绘制每组面板数据的第一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26308928/

相关文章:

javascript - 分机JS : Why can not call array from another class' property/function?

C# 在 Windows 窗体中滚动面板

r - 用格子绘制回归线

r - 如何在 auto.key (格子)中的符号后面放置文本?

r - 如何删除字符串的最后一个单词,但前提是其他单词至少有一个重复?

r - 如何在图例中重新排序项目?

css - 将 Extjs 面板背景样式更改为 Buttongroup 的背景样式

r - 如何使用R创建时间螺旋图

R 包 "translateR"和 Microsoft API

R-XGBoost : Error building DMatrix