在R

时间:2018-09-11 14:54:46

标签: r

任何人都可以帮忙。我的数据集很大,需要绘制几个时间序列图。

我使用了以下内容,但是将它们全部放在一张图上,很难阅读。

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

以下数据是否有人可以帮助?

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

我一直在搜索,找不到我需要做什么?

谢谢。

我正在使用的代码如下:

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")

  }

谢谢

2 个答案:

答案 0 :(得分:1)

(编辑:已更新,可在小时内使用修订后的示例数据。)

这是一种方法。首先加载tidyverse并读取数据:

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"
  )

我认为数据应该与原始数据匹配,但是用一天中的小时替换过去的小时数,我们丢失了日期部分,需要重新添加。 (如果已经有一个工作时间戳,则可以跳过此步骤。)我以今天的日期(18/9/13)为起点。

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

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

现在,我们可以将其整形为“整齐”的格式,其中每个观察值都在一行中。这将使绘制更加容易。

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

现在我们可以在这里使用facet_wrap调用来将每个系列放置在自己的图形中。

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

enter image description here

答案 1 :(得分:0)

如果要在单独的幻灯片中创建图形,则可以使用另一种方法:

第1步)读取数据

    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")

第2步)

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

第3步)在ppt文档中一个接一个地在幻灯片中创建图形

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")

                  }