2012-11-13 5 views
3

r의 격자 패키지를 사용하여 누적 분포 함수를 나타내는 막대 그래프와 xyplot을 겹치기를 원합니다.히스토그램과 xyplot을 겹치기

사용자 패널 기능으로이 작업을 수행하려고 시도했지만 올바른 결과를 얻지 못하는 것 같습니다. 한 변이가 단 변이이고 한 변이가 두 변이라고 생각합니다.

는 여기에 수직으로 쌓아 원하는 두 개의 플롯과 예입니다 :

set.seed(1) 
x <- rnorm(100, 0, 1) 

discrete.cdf <- function(x, decreasing=FALSE){ 
    x <- x[order(x,decreasing=FALSE)] 
    result <- data.frame(rank=1:length(x),x=x) 
    result$cdf <- result$rank/nrow(result) 
    return(result) 
} 

my.df <- discrete.cdf(x) 

chart.hist <- histogram(~x, data=my.df, xlab="") 
chart.cdf <- xyplot(100*cdf~x, data=my.df, type="s", 
        ylab="Cumulative Percent of Total") 

graphics.off() 
trellis.device(width = 6, height = 8) 
print(chart.hist, split = c(1,1,1,2), more = TRUE) 
print(chart.cdf, split = c(1,2,1,2)) 

stacked plots

나는 이러한 동일한 프레임에서 중첩보다는 스택 싶습니다.

제 1, 작업도 내가 시도 그것의 간단한 변형을 수행하지 않는 코드 다음 더 나은 응답이 올 때까지

xyplot(cdf~x,data=cdf, 
      panel=function(...){ 
       panel.xyplot(...) 
       panel.histogram(~x) 
      }) 

답변

4

사용자 정의 패널 기능을 제대로 수행했는지 확인하십시오. 트릭은 올바른 인수를 panel. - 함수에 전달하는 것입니다. panel.histogram, 이것은하지breaks 인자에 적절한 값을 수식을 통과하고 공급 수단 :

EDIT 적절한 퍼센트의 Y 축상의 값 플롯 type

xyplot(100*cdf~x,data=my.df, 
      panel=function(...){ 
       panel.histogram(..., breaks = do.breaks(range(x), nint = 8), 
       type = "percent") 
       panel.xyplot(..., type = "s") 
      }) 

enter image description here

3

이 답변은 단지 자리 표시 자입니다.

graphics 패키지의 hist() 기능에는 add이라는 옵션이 있습니다. 다음은 "클래식"방식으로 원하는 것을 수행합니다.

plot(my.df$x, my.df$cdf * 100, type= "l") 
hist(my.df$x, add= T) 
+0

+1 - 나는 이것이 꽤 좋은 대답이라고 생각합니다. 때때로 간단한 해결책이 필요한 것입니다. – thelatemail

관련 문제