使用ggplot迭代绘制时间序列

时间:2018-02-13 09:14:29

标签: r ggplot2 time-series

我有以下时间序列数据(此处仅给出samlpe):

df
        Month repo revrepo bankrate  CRR Callrate  WPI GDP  FED width    nse   usd
1  2001-04-01 9.00    6.75      7.0 8.00     7.49 5.41 4.6 4.50   225 1125.2 46.79
2  2001-05-01 8.75    6.50      7.0 7.50     8.03 5.60 4.6 4.00   225 1167.9 46.92
3  2001-06-01 8.50    6.50      7.0 7.50     7.24 5.30 4.6 3.75   200 1107.9 47.00
4  2001-07-01 8.50    6.50      7.0 7.50     7.19 5.23 5.3 3.75   200 1072.8 47.14
5  2001-08-01 8.50    6.50      7.0 7.50     6.94 5.41 5.3 3.50   200 1053.8 47.13
6  2001-09-01 8.50    6.50      7.0 7.50     7.30 4.52 5.3 3.00   200  913.9 47.65
7  2001-10-01 8.50    6.50      6.5 7.50     7.40 2.91 6.8 2.50   200  971.9 48.02
8  2001-11-01 8.50    6.50      6.5 5.75     6.97 2.59 6.8 2.00   200 1067.2 48.00
9  2001-12-01 8.50    6.50      6.5 5.50     7.08 2.08 6.8 1.75   200 1059.0 47.92
10 2002-01-01 8.50    6.50      6.5 5.50     6.63 1.51 6.4 1.75   200 1075.4 48.32
11 2002-02-01 8.50    6.50      6.5 5.50     6.73 1.39 6.4 1.75   200 1142.0 48.69
12 2002-03-01 8.00    6.00      6.5 5.50     6.97 1.76 6.4 1.75   200 1129.5 48.74
13 2002-04-01 8.00    6.00      6.5 5.50     6.58 1.50 5.1 1.75   200 1084.5 48.92
14 2002-05-01 8.00    6.00      6.5 5.50     6.90 1.56 5.1 1.75   200 1028.8 49.00
15 2002-06-01 8.00    5.75      6.5 5.00     6.04 2.43 5.1 1.75   225 1057.8 48.97

我试图使用ggplot绘制每个变量,如下所示(仅显示第一个变量图):

ts_data<-df
ts_data$Month<-as.Date(ts_data$Month,"%Y-%m-%d")

library(ggplot2)
library(ggthemes)

p <- ggplot() + 
  geom_line(data = ts_data, aes(x=Month, y=repo, color = "repo"),size=1.45,colour="#0072B2")  +ylim(0,12) +labs(color="") + xlab('\nYears') + ylab('Repo Rates (%)\n') + scale_x_date(date_breaks = "2 year",date_labels = "%Y")+ggtitle("Repo Rate movement from 2001 April to 2016 April") + theme(axis.text.x=element_text(size=12, color = "black")) + theme_stata() 
p

enter image description here

我可以类似地绘制所有其他剩余变量,但为了有效,可以采用迭代方法。如何使用绘图标题迭代绘制所有变量,以及yaxis标题随每次迭代而变化。提前谢谢。

3 个答案:

答案 0 :(得分:1)

我们可以循环遍历列并以PDF格式输出,每列绘制为新页面:

# if Month column is not date class then convert to date
# df1$Month <- as.Date(df1$Month, "%Y-%m-%d")

pdf("myPlots.pdf")
for(i in tail(colnames(df1), -1)){
  gg <- ggplot(df1, aes_string(x = "Month", y = i)) +
    geom_line() + ggtitle(paste("My title for", i))
  print(gg)}
dev.off()

或者我们可以从宽到长的格式重塑,然后使用 facet _

library(tidyr) 

plotDat <- gather(df1, key = "Group", value = "Y", -Month)

ggplot(plotDat, aes(x = Month, y = Y)) +
  geom_line() +
  facet_grid(Group ~ ., scales = "free_y")

答案 1 :(得分:0)

假设您从csv文件中读取:

library(lubridate)
library(reshape2)
library(dply)

    # If your 'Month' variable is already of Date class ignore the following line
df$Month<-ymd(df$Month)


dfmelt=melt(df,id.vars="Month")
ggplot(dfmelt,aes(x=Month,y=value,col=variable))+geom_line()

答案 2 :(得分:0)

根据@ zx8754给出的答案,这是具有所需主题的最终代码:

       for(i in tail(colnames(ts_data), -1)){
  gg <- ggplot(ts_data, aes_string(x = "Month", y = i)) +
    geom_line(size=1.45,colour="#0072B2") + scale_x_date(date_breaks = "2 year",date_labels = "%Y")+ xlab('\nYear') + ylab(paste(i,ifelse(i %in% c("repo", "revrepo", "bankrate","CRR","Callrate","WPI", "GDP","FED"), "(%)", ifelse(i %in% c("width"),"(bps)",ifelse(i %in% c("nse"),"(index points)","(Rs.)"))),'\n')) + ggtitle(paste(i,"Trend from 2001 April to 2016 April\n")) + theme_stata()
  print(gg)
  }

Thanx很多。由于有许多系列,Facet网格压缩图形,否则这也是一个很好的解决方案。也许在这里可以使用某种刻面包装。大量学习!

相关问题