簡體   English   中英

繪制格子xyplot中每組面板數據的第一個點

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

我正在嘗試使用組和面板創建線圖,這些組和面板將符號疊加在每個組的第一個值上。 此嘗試僅繪制第一組的第一個點,而不對其他兩個組執行任何操作。

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, subscripts, ...) {
          panel.xyplot(x, y, subscripts=subscripts, type='l', ...)
          # almost but not quite:
          panel.superpose(x[1], y[1], subscripts=subscripts, cex=c(1,0), ...) 
        } )

可以理解一般解決方案,以允許繪制每個組和面板內的特定點(例如,第一,中間和端點)。

在此輸入圖像描述

(感謝這個好lattice問題。)你應該使用下標,因為它是為面板選擇單個數據點的機制:在這里你想按面板選擇groupsgroups[subscripts] 獲得正確的分組變量后,您可以使用它來分割數據並選擇每個組的第一個元素:

    ## first points
    xx = tapply(x,groups[subscripts],'[',1) 
    yy = tapply(y,groups[subscripts],'[',1)
    ## end points
    xx = tapply(x,groups[subscripts],tail,1) 
    yy = tapply(y,groups[subscripts],tail,1)

您使用panel.points繪制點(比基本panel.superpose更高級別)。

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, subscripts, groups,...) {
          panel.xyplot(x, y, subscripts=subscripts, type='l',groups=groups, ...)
          # Note the use `panel.points` and the grouping using groups[subscripts]
          panel.points(tapply(x,groups[subscripts],'[',1), 
                       tapply(y,groups[subscripts],'[',1), 
                       cex=2,pch=20, ...) 
        } )

在此輸入圖像描述

如果要根據顏色組對點進行着色,則應向panel.points添加groups參數。 (我把這作為練習留給你)

暫無
暫無

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

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