2017-01-11 3 views
1

같은 음모에 다른 시간 계열에 대한 평활화 된 시간 계열을 플롯하는 해결 방법을 찾고 있습니다. par(mfrow(c(4,1))을 사용하면 같은 그래프에서 여러 개의 그림을 얻을 수 있습니다. 하지만 그래프에 대해 동일한 X 축을 갖고 싶습니다. 그래서 나는 아래와 같은 것을 가지고있다. Plot using cbind여러 줄의 여러 줄에 R

나는 cbind를 사용하여 데이터 세트를 결합한 후에 플롯했습니다. 이제는 같은 그래프에서 각 시계열에 해당하는 매끄러운 플롯을 만들고 싶습니다. par을 사용하면 다음과 같은 결과가 나타납니다.

using par

하지만 난 모든 곳에서 x 축 레이블을 원하지 않는다. 위의 결과를 얻기 위해 모든 플롯에서 선을 사용했습니다.

par(mfrow=c(4,1)) 
plot.ts(ts1,col="green") 
lines(SMA(ts1,n=10),col="red") 
plot.ts(ts2,col="green") 
lines(SMA(ts2,n=10),col="red") 
plot.ts(ts3,col="green") 
lines(SMA(ts3,n=10),col="red") 
plot.ts(ts4,col="green") 
lines(SMA(ts4,n=10),col="red") 

원하는 결과를 줄 수있는 방법이 있습니까? 예 : R? ggplot? ggplot

+0

을을 downvoted입니까? ? – Vini

+0

베이스 R에서 x 축 레이블이나'plot (1:10, 1:10, xlab = "", xaxt = "n")을 제거하려면'plot (1:10, 1:10, xlab = "")'을 눌러 레이블 및 눈금 표시 레이블을 제거하십시오. – lmo

+0

레이블과 X 축의 색인을 제거 할 수 있습니다. 그러나 나는 하나의 그래프로 4 개의 그림을 동시에 얻을 수는 없다. – Vini

답변

3

이 시도 :

ts1 <- rnorm(100) # randomly generated values for times series 
ts2 <- rnorm(100) 
ts3 <- rnorm(100) 
ts4 <- rnorm(100) 
library(TTR) 
df <- data.frame(time=rep(1:100, 8), 
       id=as.factor(rep(1:8, each=100)), id1=as.factor(rep(1:4, each=200)), 
       type=as.factor(rep(rep(1:2, each=100),4)), 
       value=c(ts1, SMA(ts1), ts2, SMA(ts2), ts3, SMA(ts3), ts4, SMA(ts4))) 
library(ggplot2) 
ggplot(df, aes(time, value, col=type, group=id)) + 
    geom_line() + facet_wrap(~id1, ncol=1) + 
    scale_color_manual(values=c('green', 'red'))+ 
    guides(color=FALSE) + theme_bw() + theme(strip.text = element_blank()) 

enter image description here

당신이 측면에 대해 서로 다른 Y 레이블을 원하는 경우,이 시도 : 왜 질문은

library(grid) 
library(gridExtra) 
grid.arrange(ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)), 
           type=as.factor(rep(1:2, each=100)), 
           ts1=c(ts1, SMA(ts1))), aes(time, ts1, col=type, group=id)) + 
       geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) + 
       theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''), 
      ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)), 
           type=as.factor(rep(1:2, each=100)), 
           ts2=c(ts2, SMA(ts2))), aes(time, ts2, col=type, group=id)) + 
       geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) + 
       theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''), 
      ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)), 
           type=as.factor(rep(1:2, each=100)), 
           ts3=c(ts3, SMA(ts3))), aes(time, ts3, col=type, group=id)) + 
       geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) + 
       theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''), 
      ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)), 
           type=as.factor(rep(1:2, each=100)), 
           ts4=c(ts4, SMA(ts4))), aes(time, ts4, col=type, group=id)) + 
       geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) + theme_bw(), ncol=1) 

enter image description here

+0

여기 또한 상단에있는 1,2,3,4에 레이블이있다 .. – Vini

+0

@Vini : 업데이트 됨. 이제 체크 아웃했다. –

+0

예. 나는 그것을 얻었다. 비록 내가 그것을 완전히 이해하지는 않지만, 그것은 효과가있다. 나는 ggplot()을 사용하는 데 익숙하지 않다. – Vini