2014-12-10 1 views
9

latticeExtra의 panel.xyarea을 사용하여 임의의 색상으로 플롯의 영역을 채울 수 있음을 알고 있습니다.격자의 다양한 '유형'인수와 일치하는 영역 채우기

library(lattice) 
library(latticeExtra) 
data <- data.frame(time=1:24,value=rnorm(24)) 
xyplot(value~time, data, 
     panel=function(x,y,...){ 
      panel.xyarea(x,y,...) 
      panel.xyplot(x,y,...)}) 

enter image description here

이 모두 panel.xyarea을 나타내는 및 포인트가 panel.xyplot에서 기본적 type="p"에서 오는 : xyplottype 인수를 정의하지 않고, 같은 충전은 기본 type="p"의 경로를 따를 것이다. 이제 문제 나 플롯 라인의 type을 변경하고자 할 때이 기능 type="S" 단계 만들기 예를 들어, 발생 :

xyplot(value~time, data, type="S", 
     panel=function(x,y,...){ 
      panel.xyarea(x,y,...) 
      panel.xyplot(x,y,...)} 

enter image description here

위의 예에서 보듯이, panel.xyarea이 지역을 기입하지 않습니다 새 단계 기능 아래에서 대신 두 영역을 겹쳐서 표시합니다. type="S"panel.xyarea으로 옮기면 아무 것도 바뀌지 않습니다. 사실 실제로는 type을 등록하지 않고 거기에 없으므로 플롯합니다.

내가 정의 어떤 유형 내 플롯 작성하여이를 무시하고 panel.xyarea을 가질 수있는 방법이 있나요 -이 기능 (type="S"를) 단계 수는, 황토 (type="smooth") 또는 회귀 (type="r")? 아니면 이런 상황에서 panel.xyarea보다 좋은 것을 사용할 수 있을까요?

+0

아니요, 여러 가지 ** 격자 ** 기능을 수정하지 않고도 간단하고 완전히 일반적인 방법은 없습니다. 'type = '의 특정 값에 대한 완전히 일반적인 (그러나 단순한) 솔루션에 관심이 있습니까? –

+0

물론, 어딘가에서 시작할 수 있습니다. @Josh –

+0

좋아,'type = "S"'및'type = "smooth"'에 대한 몇 가지 빠른 예제를 정리해 보겠습니다. –

답변

6

각 값이 type 인 경우 맞춤 패널 기능을 만들어야합니다. 다행히도 기존의 격자 코드 (이 코드는 panel.xyplot으로 시작)에서 함수를 자세히 모델링하면 너무 어렵지 않습니다. 예를 들어, 아래의 두 개의 사용자 정의 패널 함수는 많은 코드 행을 포함하지만 작성해야하는 몇 줄 (주석으로 표시) 만 포함합니다.

library(lattice) 
library(latticeExtra) 
library(gridExtra) 
set.seed(100) 
data <- data.frame(time=1:24,value=rnorm(24)) 

## Filled version of xyplot(..., type="S") 
a <- xyplot(value~time, data, panel=panel.filled_S) 
## Filled version of xyplot(..., type="smooth") 
b <- xyplot(value~time, data, panel=panel.filled_smooth) 
grid.arrange(a, b, ncol = 2) 

enter image description here

type="S"의 작성 버전 : 당신이 패널 기능을 정의하면

과 같이이를 사용, (그림 다음 코드 블록에서 그들을에서 복사)

type="smooth"의 작성 버전에 대한
## Modeled on code in panel.xyplot, which is called when type=S" 
panel.filled_S <- 
function(x,y, ...) { 
    horizontal <- FALSE     ## Edited (may not want to hardcode) 
    ord <- if (horizontal) 
     sort.list(y) 
    else sort.list(x) 
    n <- length(x) 
    xx <- numeric(2 * n - 1) 
    yy <- numeric(2 * n - 1) 
    xx[2 * 1:n - 1] <- x[ord] 
    yy[2 * 1:n - 1] <- y[ord] 
    xx[2 * 1:(n - 1)] <- x[ord][-n] 
    yy[2 * 1:(n - 1)] <- y[ord][-1] 
    panel.xyarea(x = xx, y = yy, ...) ## Edited 
    panel.lines(x = xx, y = yy, ...)  ## Edited 
} 
xyplot(value~time, data, panel=panel.filled_S, type="o") 

:

## Modeled on code in panel.loess, called by panel.xyplot when type="smooth" 
panel.filled_smooth <- 
function (x, y, span = 2/3, degree = 1, family = c("symmetric", 
    "gaussian"), evaluation = 50, lwd = plot.line$lwd, lty = plot.line$lty, 
    col, col.line = plot.line$col, type, horizontal = FALSE, 
    ..., identifier = "loess") 
{ 
    x <- as.numeric(x) 
    y <- as.numeric(y) 
    ok <- is.finite(x) & is.finite(y) 
    if (sum(ok) < 1) 
     return() 
    if (!missing(col)) { 
     if (missing(col.line)) 
      col.line <- col 
    } 
    plot.line <- trellis.par.get("plot.line") 
    if (horizontal) { 
     smooth <- loess.smooth(y[ok], x[ok], span = span, family = family, 
      degree = degree, evaluation = evaluation) 
     panel.lines(x = smooth$y, y = smooth$x, col = col.line, 
      lty = lty, lwd = lwd, ..., identifier = identifier) 
     panel.xyarea(smooth$y, smooth$x, ...) ## Edited 
    } 
    else { 
     smooth <- loess.smooth(x[ok], y[ok], span = span, family = family, 
      degree = degree, evaluation = evaluation) 
     panel.lines(x = smooth$x, y = smooth$y, col = col.line, 
      lty = lty, lwd = lwd, ..., identifier = identifier) 
     panel.xyarea(smooth$x, smooth$y, ...) ## Edited 
    } 
    smooth 
} 
+0

왜 @Josh하지만'panel.filled_S' 함수가 내 컴퓨터에서 실행될 때 제대로 작동하지 않는지 모르겠습니다.'type = "S"'그러나 일반적인'type = "p"'를 그려 내지 않습니다. –

+0

@ GeekOnAcid 잘못된 코드를 통해 복사 한 것 같습니다. 나는 지금 막 게시물로 recopied, 그리고 그것은 나를 위해 작동합니다. 너는 어떠니? –

+0

예, 이제 정상적으로 작동합니다.당신은 꽤 많은 기능을 해부하여 그 기능을 작동 시키지만, 목표를 달성한다. –

관련 문제