簡體   English   中英

在點陣xyplot中繪制每組面板數據的最大點

[英]plot the maximum point of each group of panel data in lattice xyplot

這與以前的帖子類似,但是仍然讓我撓頭。

對於每個組和每個面板,我想在原始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.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, ...)
                         }
         )
       }
)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM