2016-07-26 78 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