2017-01-11 16 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軸のインデクシングを削除することができます。しかし、私は1つのグラフとして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

関連する問題