2014-10-10 7 views
3

각 그룹의 첫 번째 값에 기호를 중첩하는 그룹 및 패널을 사용하여 선 그림을 작성하려고합니다. 이 시도는 첫 번째 그룹의 첫 번째 지점 만 플롯하고 다른 두 그룹은 수행하지 않습니다.격자 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), ...) 
     }) 

일반적인 솔루션은 각각의 그룹 및 패널 (예를 들면, 우선, 중앙 및 엔드 포인트)의 특정 포인트의 플롯을 허용하도록 이해 될 것이다.

enter image description here

답변

5

(이 좋은 lattice 질문 주셔서 감사합니다.)이 패널에 대한 개별 데이터 포인트를 따기위한 메커니즘이기 때문에 당신은 첨자를 사용한다 : groups[subscripts] : 여기에 패널로 groups을 선택하려면이.

## 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.superpose보다 높은 수준) panel.points를 사용하여 포인트를 플롯 : 당신이 변수를 그룹화 권리가되면 당신은 당신의 데이터를 분할하고 각 그룹의 첫 번째 요소를 선택하는 데 사용할 수 있습니다. 당신은 색상 그룹을 따라 포인트 색상을 원하는 경우

enter image description here

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에 그룹 인수를 추가해야합니다. (나는 이것을 운동으로 남겨둔다)

+0

첫 번째 점에 대한 해결책은 완벽하게 작동합니다. 엔드 포인트에 대한 솔루션이 예상대로 작동하지 않습니다. 패널 함수 추가 : panel.points (tapply (x, groups [subscripts], '[', length (x)), tapply (y, groups [subscripts], '[', length (y)), cex = 2 , pch = 20, ...)은 어떠한 포인트도 산출하지 않습니다. – Bryan

+0

@ 브라이언 당신 말이 맞아요. 나는 그것을 편집한다. wrks fien 지금해야합니다. 분할 된 그룹에 적용된 올바른 함수를 작성하는 방법을 알기 위해서는'tapply'를 읽을 필요가 있습니다. – agstudy

관련 문제