【问题标题】:plot the maximum point of each group of panel data in lattice xyplot在lattice xyplot中绘制每组面板数据的最大点
【发布时间】: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,...)
    } )

【问题讨论】:

    标签: r panel lattice


    【解决方案1】:

    这是一种解决方法,使用 panel.superposepanel.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, ...)
                             }
             )
           }
    )
    

    【讨论】:

      最近更新 更多