geom_vlines每个plot有多个vlines [英] geom_vlines multiple vlines per plot

查看:184
本文介绍了geom_vlines每个plot有多个vlines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让ggplot产生类似于



  library(ggplot2)
library(reshape2)
library(ecp)

synthetic_control.data< - read.table(/ path / synthetic_control.data.txt,quote =\,comment.char =)
n < - 2

s < - 样本(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)))

#计算变化点
changeP< - e.divisive (as.matrix(df [1]),k = 8,R = 400,alpha = 2,min.size = 3)
changeP = changeP $ estimated
changeP = changeP [-c(1 ,length(changeP))]

changePoints = data.frame(changeP,variable = colnames(df)[1])$ ​​b $ b for(series in 2:ncol(df)){$ (as.matrix(df [series]),k = 8,R = 400,alpha = 2,min.size = 3)
changeP = changeP $ estimated
changeP = changeP [-c(1,leng (changeP))]
changePoints = rbind(changePoints,data.frame(changeP,variable = colnames(df)[2]))
}

这是关于情节的有趣部分:

df $ id = 1:nrow(df )
dfMelt < - reshape2 :: melt(df,id.vars =id)
p = ggplot(dfMelt,aes(x = id,y = value))+ geom_line(color =












$ $ b

到目前为止,我的结果是: https://www.dropbox.com /s/mysadkruo946oox/changePoint.pdf ,这意味着我的数组传递给 geom_vlines 时出错。



你能指出我在正确的方向,为什么我只在前两个图中获得 vlines

解决方案

这是解决方案:

  libr ary(ggplot2)
library(reshape2)
library(ecp)

synthetic_control.data< - read.table(/ Users / geoHeil / Dropbox / 6.Semester /本科/研究/数据/ 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)))

#计算变化点
changeP< - e.divisive(as .matrix(df [1]),k = 8,R = 400,alpha = 2,min.size = 3)
changeP = changeP $ estimated
changeP = changeP [-c (changeP))]

changePoints = data.frame(changeP,variable = colnames(df)[1])$ ​​b $ b for(series in 2:ncol(df)){$ b $ (换言之,矩阵(df [系列]),k = 8,R = 400,α= 2,min.size = 3)
changeP = changeP $估计
changeP = changeP [-c(1,length(changeP))]
changePoints = rbind(changePoints,data.frame(changeP,variable = colnames(d f)[系列]))
}

#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',color ='darkgreen')


How can I get ggplot to produce something similar like

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 the 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 is the interesting part about the 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 my result is: https://www.dropbox.com/s/mysadkruo946oox/changePoint.pdf which means that there is something wrong with my array passed to the geom_vlines.

Could you point me in the right direction why I only get vlines in the first 2 plots?

解决方案

This is the 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 the 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')

这篇关于geom_vlines每个plot有多个vlines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆