在R中绘制若干时间序列图

在R中绘制若干时间序列图,r,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 62

有人能帮忙吗。我有一个大的数据集,需要绘制几个时间序列图

我使用了下面的内容,但它将所有内容放在一个图表上,很难阅读

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

  }

谢谢

编辑:已更新以使用修改后的样本数据和小时数

这里有一种方法。首先加载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"
  )

如果要在单独的幻灯片中创建图形,另一种方法是:

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

                  }

你好非常感谢以上的帮助。但是,当我尝试运行它时,我得到了以下消息:UseMethodmutate_u2;中的错误:没有适用于“mutate_2;”的方法应用于类函数的对象。我已经上传了所有合适的软件包。我还上传了一个.csv文件的文件。这可能是一个原因吗?我想问题是我在其他库调用的步骤中意外忽略了librarydplyr。或者,可以使用librarytidyverse,它碰巧加载了ggplot2、dplyr、tidyr和其他几个相关包中的所有三个。我会编辑上面的内容,请让我知道这是否解决了它。嗨,乔恩。我加载了Tidyverse,它仍然给出以下消息:UseMethodmutate_u2;中的错误:没有适用于“mutate_2;”的方法应用于类函数的对象。。出于某种原因,它似乎对mutate函数不满意?嗨,Jon,我还注意到代码中的时间序列图没有使用列中设置的时间。因此,我添加了一个额外的代码:as.posixctstrptimepastec$DATE,bc$TIME,%d/%m/%Y%H:%m:%S。错误仍然存在,但现在已更改为整数。谢谢。关于代码其余部分的任何建议,因为mutate函数仍然会出现错误。错误[.data.framedf,i,1对象“i”未找到。我还应该选择哪个路径?如果您使用的是我粘贴的相同代码,请尝试将df2替换为df。此外,路径可以是系统上您要保存power point文档的任何位置。请告诉我它是否工作..您好。路径已排序。但是脚本仍然不工作,不确定您在哪里使用请允许我更改df2而不是df?您可以使用Officer和ggplot2软件包为一个power point文档中的每个图形创建单独的幻灯片。您可以使用read_pptx from Officer,ggplot使用ggplot2创建图形,添加Officer的幻灯片,ph_from Officer的文本,ph_from Officer和print from officerI已将时间更改为让它们更容易阅读。最后三个08:00:00将在另一个日期完成。因此我需要R来保持列的顺序,这似乎要改变:
    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")
library(ggplot2);library(tidyr);library(dplyr)
df_tidy <- df1 %>%
  mutate(TIME = row.names %>% as.integer) %>%
  select(-row.names) %>%
  gather(series, value, -TIME)
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")

                  }