【发布时间】:2024-01-08 17:40:01
【问题描述】:
这类似于previous post,但它仍然让我摸不着头脑。
对于每个组和每个面板,我想识别并绘制原始 xy 线图上的最大 y 值点。使用tapply可能有一种方法可以做到这一点,但我一直找不到。这是我的尝试和卡住的地方:
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, groups, subscripts, ...) {
panel.xyplot(x, y, subscripts=subscripts, type='l',groups=groups, ...)
# get the index of the maximum y-value for each group
max_ind <- tapply(y, groups[subscripts], function(x) { which(x==min(x))[1]} )
# splits the data into groups
x_g <- tapply(x, groups[subscripts], function(x){x})
y_g <- tapply(y, groups[subscripts], function(x){x})
# something goes here to extract the x- and y- values corresponding
# to the maximum index of each group
#x_max = ???
#y_max = ???
panel.points(x_max, y_max, cex=2, pch=16,...)
} )
【问题讨论】: