这类似于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,...)
} )
答案 0 :(得分:1)
使用panel.superpose
和panel.groups
,这是一种方法:
xyplot(y ~ x | y > 0, foo, groups=cut(x, 3),
panel = function(x, y, ...) {
panel.superpose(x, y, pch=20, cex=1.5, ...,
panel.groups = function(x, y, col.symbol, ...) {
panel.lines(x, y, col=col.symbol)
panel.points(x[which.max(y)], max(y),
col.symbol=col.symbol, ...)
}
)
}
)