2017-04-24 3 views
0

r에 breakoutDetection 패키지를 사용하여 breakout 함수를 성공적으로 실행했습니다. 이 함수의 가능한 제품 중 하나는 $ 음모입니다. breakout을 실행 한 후 목록에 포함 된 여러 개의 그림을 결합 할 수 있습니까? 나는 성공없이 par() 함수를 시도했다. 모든 아이디어를 전에 ggplot 함께 시계열을 재현?여러개의 브레이크 아웃 그림을 함께 사용하기 [r]

재현 예 : I 다른 플롯과 함께 결합 ts.b$plot 요소 수 있도록하려는

require(BreakoutDetection) 
df.date = c("01-01-2017", "02-01-2017", "03-01-2017", "04-01-2017", "05-01-2017", "06-01-2017", "07-01-2017", "08-01-2017", "09-01-2017", "10-01-2017") 
df.values = c(1,2,1,1,3,22,34,45,22, 10) 
ts = data.frame(df.date, df.values) 
ts.b = breakout(ts$df.values, min.size=3, method='multi', beta=.008, degree=1, plot=TRUE, xlab = "time") 
ts.b$plot 

AA의 2 * 2 매트릭스 또는 다른 3 * 1 매트릭스 (다른 데이터에 기초하여) 더 ... 또한 난 그냥 브레이크 아웃 검출 출력에 df과 일을 음모 ggplot2을 사용하고, 당신이 the source code of this package을 보러 경우

+0

A [재현 예]를 제공하십시오 (http://stackoverflow.com/questions/5963269/how-to-make-a- great-r-reproducible-example)을 샘플 입력 데이터 및 코드와 함께 사용하면 수행하려는 작업을 볼 수 있습니다. – MrFlick

답변

0

(시계열의 크기) 날짜가 아닌 정수를 보여주는 x 축 이름을 바꾸려면 . 여기

이 코드 다음 동작하는 예제입니다

# your code 
require(BreakoutDetection) 
df.date = c("01-01-2017", "02-01-2017", "03-01-2017", "04-01-2017", "05-01-2017", "06-01-2017", "07-01-2017", "08-01-2017", "09-01-2017", "10-01-2017") 
df.values = c(1,2,1,1,3,22,34,45,22, 10) 
ts = data.frame(df.date, df.values) 
ts.b = breakout(ts$df.values, min.size=3, method='multi', beta=.008, degree=1, plot=TRUE, xlab = "time") 
ts.b$plot 



# start: 
library(ggplot2) 

# the code in this function is copied from the source code of breakout detection (with small adjustment) 
# you can adjust things 
plot_breakout_detection = function(Z, retList, dateTime = T, x_lab = '', y_lab = '', title0 = ''){ 

    if(class(Z)%in%c('numeric','integer') || ncol(Z) == 1){ 
     dateTime = F 
     Z = data.frame(timestamp=1:length(Z), count = Z) 
    } 


    g = ggplot2::ggplot(Z, ggplot2::aes(x=timestamp, y=count)) + ggplot2::theme_bw() + 
     ggplot2::theme(panel.grid.minor=ggplot2::element_blank(), panel.grid.major=ggplot2::element_blank()) 


    g = g + ggplot2::xlab(x_lab) + ggplot2::ylab(y_lab) + ggplot2::ggtitle(title0) 

    g = g + ggplot2::geom_line() 

    if(!is.null(retList$loc)&& length(retList$loc)>0){ 
     v = retList$loc 
     v = c(0,v) 
     for(j in 2:length(v)){ 
      M = mean(Z$count[(v[j-1]+1):v[j]]) 
      df2 = data.frame(Z$timestamp[v[j]], Z$timestamp[v[j]], -Inf, M) 
      names(df2) = c('x','xend','y','yend') 
      g = g + ggplot2::geom_segment(data=df2,ggplot2::aes(x=x,y=y,xend=xend,yend=yend,color='2'),linetype=2,size=1.2) 
      g = g + ggplot2::guides(color=FALSE) 
     } 
    } 

    if(dateTime){ 
     g = g + scale_x_datetime(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) 
    } else { 
     g = g + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) 
    } 

} 



df = data.frame(timestamp = as.POSIXct(df.date, format = '%m-%d-%Y'), count = df.values) 
g = plot_breakout_detection(df, ts.b) 
g 

enter image description here

관련 문제