Isobelle Gadsby
Isobelle Gadsby

Reputation: 1

Plotting several time series graphs in R

Can anyone help. I have a large data set and need to plot several time series graphs.

I have used the following but it placed it all on one graph and was hard to read.

ts.plot(bc,gpars= list(col=rainbow(10)))

The data is below if anyone can help?

TIME      15         16           17           18          20          22          23
08:00:00 130.4905899    15.44164769 948.9185939 6211.837354 1071.730556 10.08920076 7.793301031
11:00:00 125.7301547    18.87143833 991.0009783 6304.471569 1082.126629 10.80475415 7.857773565
14:00:00 153.3779662    17.63335938 949.1741247 6209.524186 1079.102756 10.68438383 8.326058855
17:00:00 132.3256891    15.8961511  917.0452991 6123.402395 1081.166439 10.41007094 7.856372445
20:00:00 130.6405835    15.28122651 917.0229181 6135.679239 1084.589394 10.70688202 7.741277402
08:00:00 124.1484465    17.14357927 948.9060481 6126.791479 1085.907147 10.76713085 7.810187162
08:00:00 161.0455657    17.10409992 881.4517913 5839.73355  1073.585164 9.925269955 7.987206082
08:00:00 165.645823     16.45928764 860.4285647 5781.612679 1073.439013 10.01297791 7.983672272

I have been search and cant find what I need to do?

Thanks in advance.

The code I am using is as follows:

library(tidyverse)
df <- read.csv("Strep_time_series_NBS.csv", header = TRUE, row.names = NULL)
timestamps <- lubridate::dmy_h(c(3008201808, 3008201811,     3008201814,    3008201817, 3008201820,3108201808, 0309201808, 1009201808))

df_fix_times <- df %>%
  mutate(TIME=timestamps)

df_tidy <- df_fix_times %>%
  gather(series, value, -TIME)

ggplot(df_tidy, aes(TIME, value, group =series))+
  geom_line() +
  facet_wrap(~series, scales = "free_y")

library(sqldf)

df2=sqldf("What needs to go in here?")   


library(officer);library(ggplot2);library(magrittr);library(rvg)


doc <- read_pptx()
for (i in 1:nrow(df))
{
  data_graph_ind=df_tidy[(df_tidy$series==df[i,1]),]
  doc <- add_slide(doc, layout = "Two Content", master = "Office Theme")
  doc <- doc %>% 
  ph_with_text(type = "title",df2[i,1]) 

  doc <- doc %>%
  ph_with_vg(doc, ggobj=ggplot(data = data_graph_ind, aes(x = TIME, y    =      value)) + 
             geom_line()+geom_point(),type="body")
  print(doc,target="C:/path/example.pptx")

  }

Thanks

Upvotes: 0

Views: 139

Answers (2)

Jon Spring
Jon Spring

Reputation: 66480

(EDIT: updated to use the revised sample data with hour times.)

Here's one approach. First load the tidyverse and read in the data:

library(tidyverse)   

df <- read.table(
    header = TRUE, row.names = NULL, text =
      "TIME      15         16           17           18          20          22          23
08:00:00 130.4905899    15.44164769 948.9185939 6211.837354 1071.730556 10.08920076 7.793301031
             11:00:00 125.7301547    18.87143833 991.0009783 6304.471569 1082.126629 10.80475415 7.857773565
             14:00:00 153.3779662    17.63335938 949.1741247 6209.524186 1079.102756 10.68438383 8.326058855
             17:00:00 132.3256891    15.8961511  917.0452991 6123.402395 1081.166439 10.41007094 7.856372445
             20:00:00 130.6405835    15.28122651 917.0229181 6135.679239 1084.589394 10.70688202 7.741277402
             08:00:00 124.1484465    17.14357927 948.9060481 6126.791479 1085.907147 10.76713085 7.810187162
             08:00:00 161.0455657    17.10409992 881.4517913 5839.73355  1073.585164 9.925269955 7.987206082
             08:00:00 165.645823     16.45928764 860.4285647 5781.612679 1073.439013 10.01297791 7.983672272"
  )

I presume the data should match the original data, but in replacing hours passed with just the hour of day, we've lost the date component and need to add it back. (You can skip this if you have a working timestamp already.) I'll assume today's date, 9/13/18 as the starting point.

timestamps <- lubridate::ymd_h(c(
  2018091308, 2018091311, 2018091314, 2018091317, 
  2018091320, 2018091408, 2018091708, 2018092008))

df_fix_times <- df %>%
  mutate(TIME = timestamps)

Now, we can reshape into a "tidy" format where each observation is in a row. This will make it easier to plot.

df_tidy <- df_fix_times %>%
  gather(series, value, -TIME)

And now we can use the facet_wrap call here to put each series in its own graph.

ggplot(df_tidy, aes(TIME, value, group = series)) +
  geom_line() +
  facet_wrap(~series, scales = "free_y")

enter image description here

Upvotes: 1

girvarshangari
girvarshangari

Reputation: 16

Another approach if you want to create graphs in separate slides:

Step 1) Read the data

    df1 <-
  read.table(header = TRUE, row.names = NULL, text = 
               "TIME    15  16  17  18  20  22  23
0 hrs   130.4905899 15.44164769 948.9185939 6211.837354 1071.730556 10.08920076 7.793301031
3 Hrs   125.7301547 18.87143833 991.0009783 6304.471569 1082.126629 10.80475415 7.857773565
6 Hrs   153.3779662 17.63335938 949.1741247 6209.524186 1079.102756 10.68438383 8.326058855
8 Hrs   132.3256891 15.8961511  917.0452991 6123.402395 1081.166439 10.41007094 7.856372445
12 Hrs  130.6405835 15.28122651 917.0229181 6135.679239 1084.589394 10.70688202 7.741277402
24 Hrs  124.1484465 17.14357927 948.9060481 6126.791479 1085.907147 10.76713085 7.810187162
96 Hrs  161.0455657 17.10409992 881.4517913 5839.73355  1073.585164 9.925269955 7.987206082
168 hrs 165.645823  16.45928764 860.4285647 5781.612679 1073.439013 10.01297791 7.983672272")

Step 2)

library(ggplot2);library(tidyr);library(dplyr)
df_tidy <- df1 %>%
  mutate(TIME = row.names %>% as.integer) %>%
  select(-row.names) %>%
  gather(series, value, -TIME)

Step 3) Create graphs in slides one after other in ppt document

library(sqldf)

df2=sqldf("select distinct(series) from df_tidy")   


library(officer);library(ggplot2);library(magrittr);library(rvg)


doc <- read_pptx()
for (i in 1:nrow(df2))
                  {
                     data_graph_ind=df_tidy[(df_tidy$series==df2[i,1]),]
                     doc <- add_slide(doc, layout = "Two Content", master = "Office Theme")
                     doc <- doc %>% 
                     ph_with_text(type = "title",df2[i,1]) 

                     doc <- doc %>%
                     ph_with_vg(doc, ggobj=ggplot(data = data_graph_ind, aes(x = TIME, y = value)) + 
                                    geom_line()+geom_point(),type="body")
                     print(doc,target="C:/path/example.pptx")

                  }

Upvotes: 0

Related Questions