0

I have 5 different multivariate time series (currently with the same lengths and time intervals). I would like to plot them all in the same graph, where the target graph should present the 5 multivariate times series above each other!

Suppose ts1, ts2 are the first and the second multivariate time series

head(ts1)
      Time      f1     f2     f3     f4     f5     f6     f7
      08:00:00 -0.018 -0.018 -0.024 -0.014 -0.009 -0.012 -0.017
      08:00:10 -0.016 -0.020 -0.024 -0.013 -0.007 -0.012 -0.017
      08:00:20 -0.016 -0.020 -0.022 -0.011 -0.007 -0.012 -0.019
      08:00:30 -0.014 -0.020 -0.024 -0.013 -0.009 -0.012 -0.017
      08:00:40 -0.016 -0.018 -0.024 -0.015 -0.011 -0.012 -0.017
      08:00:50 -0.016 -0.018 -0.022 -0.020 -0.009 -0.012 -0.017

and ts2

head(ts2)
    Time       f1     f2     f3    f4       f5     f6
    08:00:00  11489  11651  11587 13149.5  12093  12394
    08:00:10  11495  11709  11595 13206.0  12081  12295
    08:00:20  11409  11721  11493 13163.0  12014  12214
    08:00:30  11441  11729  11602 13303.0  11894  12340
    08:00:40  11413  11764  11590 13140.0  12047  12314
    08:00:50  11434  11785  11580 13300.0  12050  12331

I know that we use the function mvtsplot() to plot multivariate time series but I dont know how can I plot the 5 series above each other in the same plot

1 Answers1

3

I would suggest next approach using ggplot2 and tidyverse functions. If your data is allocated in different dataframes yu can use bind_rows() to combined all of them. After that you can reshape to long and then plot using facets scheme. Here the code with the data you shared:

library(ggplot2)
library(tidyverse)
#Data
ts <- bind_rows(ts1,ts2,.id = 'data')
#Process data and plot
ts %>% mutate(data=paste0('ts',data)) %>%
  pivot_longer(-c(data,Time)) %>%
  mutate(data=ifelse(data=='ts1','Rain','Snow')) %>%
  ggplot(aes(x=factor(Time),y=value,group=name,color=name))+
  geom_line()+
  facet_wrap(.~data,scales = 'free',ncol=1)+
  xlab('Time')

Output:

enter image description here

Some data used:

#Data 1
ts1 <- structure(list(Time = c("08:00:00", "08:00:10", "08:00:20", "08:00:30", 
"08:00:40", "08:00:50"), f1 = c(-0.018, -0.016, -0.016, -0.014, 
-0.016, -0.016), f2 = c(-0.018, -0.02, -0.02, -0.02, -0.018, 
-0.018), f3 = c(-0.024, -0.024, -0.022, -0.024, -0.024, -0.022
), f4 = c(-0.014, -0.013, -0.011, -0.013, -0.015, -0.02), f5 = c(-0.009, 
-0.007, -0.007, -0.009, -0.011, -0.009), f6 = c(-0.012, -0.012, 
-0.012, -0.012, -0.012, -0.012), f7 = c(-0.017, -0.017, -0.019, 
-0.017, -0.017, -0.017)), class = "data.frame", row.names = c(NA, 
-6L))

#Data 2
ts2 <- structure(list(Time = c("08:00:00", "08:00:10", "08:00:20", "08:00:30", 
"08:00:40", "08:00:50"), f1 = c(11489L, 11495L, 11409L, 11441L, 
11413L, 11434L), f2 = c(11651L, 11709L, 11721L, 11729L, 11764L, 
11785L), f3 = c(11587L, 11595L, 11493L, 11602L, 11590L, 11580L
), f4 = c(13149.5, 13206, 13163, 13303, 13140, 13300), f5 = c(12093L, 
12081L, 12014L, 11894L, 12047L, 12050L), f6 = c(12394L, 12295L, 
12214L, 12340L, 12314L, 12331L)), class = "data.frame", row.names = c(NA, 
-6L))
Duck
  • 39,058
  • 13
  • 42
  • 84
  • Thanks a lot for this great answer. Can you please how it is possible to change the name of each multivariate times series inside the plot. For example, to name them 'Rain' instead of 'ts1' and 'Snow' instead of 'ts2'. Thanks a gain – Sophie Allan Sep 24 '20 at 20:59
  • @SophieAllan Ho Sophie, I have added an update for what you want! – Duck Sep 24 '20 at 23:43