r - geom_vlines multiple vlines per plot -
how can ggplot produce similar
library(ggplot2) library(reshape2) library(ecp) synthetic_control.data <- read.table("/path/synthetic_control.data.txt", quote="\"", comment.char="") n <- 2 s <- sample(1:100, n) idx <- c(s, 100+s, 200+s, 300+s, 400+s, 500+s) sample2 <- synthetic_control.data[idx,] df = as.data.frame(t(as.matrix(sample2))) #calculate change points changep <- e.divisive(as.matrix(df[1]), k=8, r = 400, alpha = 2, min.size = 3) changep = changep$estimates changep = changep[-c(1,length(changep))] changepoints = data.frame(changep,variable=colnames(df)[1]) for(series in 2:ncol(df)){ changep <- e.divisive(as.matrix(df[series]), k=8, r = 400, alpha = 2, min.size = 3) changep = changep$estimates changep = changep[-c(1,length(changep))] changepoints = rbind(changepoints, data.frame(changep,variable=colnames(df)[2])) }
this interesting part plot:
df$id = 1:nrow(df) dfmelt <- reshape2::melt(df, id.vars = "id") p = ggplot(dfmelt,aes(x=id,y=value))+geom_line(color = "steelblue")+ facet_grid(variable ~ ., scales = 'free_y') p + geom_vline(aes(xintercept=changep), data=changepoints, linetype='dashed')
so far result is: https://www.dropbox.com/s/mysadkruo946oox/changepoint.pdf means there wrong array passed geom_vlines
.
could point me in right direction why vlines
in first 2 plots?
this solution:
library(ggplot2) library(reshape2) library(ecp) synthetic_control.data <- read.table("/users/geoheil/dropbox/6.semester/bachelorthesis/rresearch/data/synthetic_control.data.txt", quote="\"", comment.char="") n <- 2 s <- sample(1:100, n) idx <- c(s, 100+s, 200+s, 300+s, 400+s, 500+s) sample2 <- synthetic_control.data[idx,] df = as.data.frame(t(as.matrix(sample2))) #calculate change points changep <- e.divisive(as.matrix(df[1]), k=8, r = 400, alpha = 2, min.size = 3) changep = changep$estimates changep = changep[-c(1,length(changep))] changepoints = data.frame(changep,variable=colnames(df)[1]) for(series in 2:ncol(df)){ changep <- e.divisive(as.matrix(df[series]), k=8, r = 400, alpha = 2, min.size = 3) changep = changep$estimates changep = changep[-c(1,length(changep))] changepoints = rbind(changepoints, data.frame(changep,variable=colnames(df)[series])) } # plot df$id = 1:nrow(df) dfmelt <- reshape2::melt(df, id.vars = "id") p = ggplot(dfmelt,aes(x=id,y=value))+geom_line(color = "steelblue")+ facet_grid(variable ~ ., scales = 'free_y') p + geom_vline(aes(xintercept=changep), data=changepoints, linetype='dashed', colour='darkgreen')
Comments
Post a Comment