gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:36:41 30 4
gpt4 key购买 nike

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

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com