在格子xyplot中绘制每组面板数据的最大点
plot the maximum point of each group of panel data in lattice xyplot
这类似于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,...)
} )
这是一种解决方法,使用 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, ...)
}
)
}
)
这类似于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,...)
} )
这是一种解决方法,使用 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, ...)
}
)
}
)