2016-07-26 5 views
-1

私はXTSオブジェクトを持っており、それからggplotにいくつかの時系列をプロットしたいと思います。同じプロットで複数の時系列をプロットするにはどうすればよいですか?xtsオブジェクトからのggplot2の複数の時系列

+2

はStackOverflowのへようこそ。 [ツアー](http://stackoverflow.com/tour)にアクセスし、何をどのように[尋ねる]を観察してください(http://stackoverflow.com/help/asking)。あなたの質問がコードについての場合は、あなたがこれまで行ってきたことの[最小、完全かつ検証可能な例](http://stackoverflow.com/help/mcve)を提供してください。 – user3078414

答えて

2

あなたが任意のデータセットを提供していないので、私はAirPassengersデータセットを使用して例示します:

library(datasets) 
library(xts) 
library(ggplot2) 
library(broom) 
library(magrittr) 
ap.xts <- as.xts(AirPassengers) 
mseries <- cbind(ap.xts, rollmean(ap.xts,7)) # mseries is a xts object with multiple variables 
names(mseries) <- c("Passengers", "MA_Passengers") # names for the series, otherwise the names are '..1' and '..2' 
index(mseries) <- as.Date(index(mseries)) # to avoid warnings since ggplot scale don't handle yearmon natively 
tidy(mseries) %>% ggplot(aes(x=index,y=value, color=series)) + geom_line() 

multiple xts in ggplot2

関連する問題