2017-04-24 8 views
0

パッケージを使用してbreakout関数を正常に実行しました。この関数の可能な製品の1つは$ plotです。 breakoutの実行後にリストに埋め込まれた複数のプロットを組み合わせることは可能ですか?私は成功なしでpar()関数を試しました。私はggplotで時系列を再現する前に任意のアイデアですか?複数のブレークアウトプロットをまとめて組み合わせる[r]

再現例:

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 

Iは、AA 2×2マトリックスまたは他の3×1の行列で(他のデータに基づく)他のプロットと一緒ts.b$plot要素を組み合わせることができるようにしたい...でまた、私はそれだけでブレイクアウト検出の出力であなたのdf、物事をプロットするggplot2を使用しています、あなたがthe source code of this packageを見に行く場合は

+0

[再現可能な例]を提供してください(http://stackoverflow.com/questions/5963269/how-to-make-a-サンプルの入力データとコードを使用して、あなたが何をしようとしているかを確認することができます。 – 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

関連する問題