1

I have

> head(My Data)
     Time   V1 V2    V3    V4   V5   V6   V7   V8 V9
1 10:00:00  0 11489 11017 6846 6832 3.95 5.75  0 464
2 10:00:10  0 11495 11012 6796 6807 3.95 5.75  0 467
3 10:00:20  0 11409 10983 6835 6797 3.95 5.75  0 458
4 10:00:30  0 11441 11064 6761 6661    0    0  0 463
5 10:00:40  0 11413 10999 6870 6853    0    0  0 461
6 10:00:50  0 11434 11053 6798 6814    0    0  0 459

I would like to plot "in the same plot":

  1. 9 separated curves in the same plot, where each curve with a different color represents a time series : V1, ..., V9.

  2. Add, to the plot, the name of each time series "column name".

  3. For this plot, I would like to add Time to x-axis but as the timestamp is too small "10 seconds" there will be a dense of values arranged on the x-axis. Instead, I would like to represent Time in 30 minutes time lag.

  4. Add a plot name, so to add My data as a title of the plot.

What I usually do is to use ts.plot() or autoplot(ts()) to plot similar multiple time series in the same graph but I don't know how to calibrate my plot so I think ggplot would be better to do this.

Could you please help me to learn how to do this easily in ggplot or even using ts.plot() if possible

Jean
  • 57
  • 7

1 Answers1

1

You can get the data in long format to plot, convert Time to POSIXct. Since the values in each column are not in the same range I have used facets to plot them separately.

library(tidyverse)

my_data %>%
  pivot_longer(cols = -Time) %>%
  mutate(Time = as.POSIXct(Time, format = '%H:%M')) %>%
  ggplot() + aes(Time, value, color = name) + 
  geom_line() + 
  facet_wrap(~name, scales = 'free_y') + 
  scale_x_datetime(date_labels =  '%H:%M', breaks = '30 mins') + 
  theme(legend.position="none") + 
  ggtitle('My data')

To keep everything in the same plot you can do :

my_data %>%
  pivot_longer(cols = -Time) %>%
  mutate(Time = as.POSIXct(Time, format = '%H:%M')) %>%
  ggplot() + aes(Time, value, color = name) + 
  geom_line() + 
  scale_x_datetime(date_labels =  '%H:%M', breaks = '30 mins') + 
  ggtitle('My data')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Many thanks for your answer. Could you please clarify how could I add a title "My data" to the plot? Additionally, how can I arrange curves above each other, so, using your method, to have 9 curves above each other? – Jean Dec 29 '20 at 07:39
  • You can add title information using `ggtitle`, that is already included in the answer. To have all the lines in one plot check the updated answer. – Ronak Shah Dec 29 '20 at 07:47
  • Great, thanks a lot for your efforts. I already accepted your answer. A last question if possible, how can I center the title in your first answer. Because, according to the first answer, the title is printed at the left of the plot but how to center it? – Jean Dec 29 '20 at 08:05
  • You need to add `+ theme(plot.title = element_text(hjust = 0.5))` to the above. See https://stackoverflow.com/questions/40675778/center-plot-title-in-ggplot2 – Ronak Shah Dec 29 '20 at 08:07