绘制数据作为时间的函数

时间:2013-11-17 16:51:34

标签: r time

我最近刚接触编程,希望能够根据时间绘制一些数据。

这是我的数据:

Total Dive  Location    Seal    Dive    Onset   Time    Pause   Duration    Descent Bottom  Ascent  down    bottom  up  Max Min Mean    Median  Sex Weight  Length  DE
1   1   1   0   20/03/2001  08:10:00    0   28.03829    11.51914    7.831924    8.68722 0.20076935  0.035183302 -0.1687638  3.123837    2.5654047   2.8433998   2.8402238   m   91  178 0.279329588
2   1   1   1   20/03/2001  08:11:00    6.961711    37.38936    9.129786    0   28.25957    0.24363854      -0.060074619    2.471383                m   91  178 0
3   1   1   2   20/03/2001  08:15:00    223.4809    67.38936    37.81701    15.44257    14.12979    0.064050013 0.060173167 -0.2094474  3.7917579   2.6753003   3.3389067   3.4796772   m   91  178 0.053090921
4   1   1   3   20/03/2001  08:16:00    0.8702138   46.51914    26.74043    0   19.77872    0.23090517      -0.27213296 6.6596252               m   91  178 0
5   1   1   4   20/03/2001  08:17:00    15.22128    53.03829    18.25957    0   34.77872    0.2767181       -0.13182431 5.6302701               m   91  178 0
6   1   1   5   20/03/2001  08:18:00    8.702138    106.2979    36.51914    0   69.77872    0.21029149      -0.083307187    7.9690106               m   91  178 0
7   1   1   6   20/03/2001  08:20:00    16.96171    96.07658    23.25957    60  12.81701    0.34126665  -0.065462485    -0.19133167 8.1973492   2.1478016   5.0445987   4.4104276   m   91  178 0.530793592
8   1   1   7   20/03/2001  08:22:00    17.40428    64.55743    14.55743    15.22128    34.77872    0.36582394  0.17159207  -0.16005231 8.4578424   5.5830557   7.5521401   8.0730284   m   91  178 0.185712084
9   1   1   8   20/03/2001  08:23:00    16.96171    97.59572    18.25957    40  39.33615    0.38841734  0.001292291 -0.14804832 7.6132746   6.7015484   7.3927146   7.419443    m   91  178 0.349169844
10  1   1   9   20/03/2001  08:25:00    17.40428    194.7787    18.25957    155 21.51914    0.44144717  -0.006911473    -0.25799173 8.4558073   3.6488936   7.1492711   7.566339    m   91  178 0.73050157
11  1   1   10  20/03/2001  08:29:00    16.96171    93.03829    21.51914    42.40428    29.11487    0.27868403  -0.0229022  -0.12777556 6.299005    5.3278537   5.9544846   6.1116569   m   91  178 0.385493455
12  1   1   11  20/03/2001  08:31:00    31.96171    79.55743    16.51914    40.44257    22.59572    0.30018094  -0.025160411    -0.1332074  5.2090037   4.1914521   4.8630814   4.9686372   m   91  178 0.36265138
13  1   1   12  20/03/2001  08:32:00    10.44257    113.0383    11.51914    88.92342    12.59572    0.31829115  -0.022904328    -0.044787471    4.3041969   1.8767258   2.9966657   3.0617904   m   91  178 0.720139241
14  1   1   13  20/03/2001  09:29:00    3309.306    213.732 11.71059    177.4639    24.55743    0.10708516  0.016832289 -0.12737298 4.5907394   1.5100002   2.965759    3.147401    m   91  178 0.050372406
15  1   1   14  20/03/2001  09:34:00    30.44257    196.5191    15  123.4809    58.03829    0.29240362  -0.005099259    -0.052597255    5.0502657   4.0266088   4.5616455   4.4860453   m   91  178 0.54406059
16  1   1   15  20/03/2001  09:37:00    36.96171    236.0766    11.51914    205 19.55743    0.28451158  -0.003758488    -0.085951725    4.4914263   2.4290528   3.513724    3.4602396   m   91  178 0.75081039
17  1   1   16  20/03/2001  09:42:00    53.92342    174.5574    16.51914    145 13.03829    0.16377762  -0.004210558    -0.075608382    3.4107866   2.1620473   2.9550863   3.0076503   m   91  178 0.634626574
18  1   1   17  20/03/2001  09:49:00    210.4426    139.5574    11.51914    95  33.03829    0.22461999  -0.004691448    -0.052432491    3.3782249   2.6321561   2.998841    3.0756852   m   91  178 0.271428571
19  1   1   18  20/03/2001  10:03:00    740.4426    243.0383    16.51914    195 31.51914    0.21064162  -0.009094302    -0.064360592    4.6521995   2.3667787   3.6379855   4.0493523   m   91  178 0.19827533

我使用以下函数指示R“Onset”变量是日期向量,“Time”变量是以下代码行的时间向量:

x$Date <- as.POSIXct(paste(x$Onset), format="%d/%m/%Y")

x$Time <- as.POSIXct(paste(x$Time), format="%H:%M:%S")

我的数据是海豹的潜水数据,我想绘制旅行中不同时间(“时间”)密封达到的最大深度(“最大”)。我可以通过将“Max”绘制为线图并将x轴名称设置为“Time”来非常简单地在microsoft excel中执行此操作,但我很难在R中执行此操作!

我尝试过编码,如下所示:

attempt1 <- x$Max[(x$Location=="1")&(x$Seal=="1")&(x$Date=="20/03/2001 GMT")&(x$Time1 > 08:10:00 & x$Time1 <= 10:03:00)]
plot(attempt1)

请帮忙!

谢谢,

威尔

1 个答案:

答案 0 :(得分:1)

我无法导入您的数据。因此,如果您在R控制台中运行此代码,我无法保证不会出现任何错误。重要的是原则。我的第一个建议是将“Onset”和“Time”组合在一起。

x$pTime <- as.POSIXct(paste(x$Onset, X$Time), format="%d/%m/%Y %H:%M:%S")

然后绘制

transform(x, Max=-Max) # Deeper -> lower y
plot(Max ~ pTime, data=x)

如果您正在使用RStudio,您可以尝试使用操作函数来动态过滤数据......

library(manipulate)
manipulate(
    plot(Max ~ Time, data=x[x$Seal == Seal & x$Location == Loc & x$pTime > Tmin & x$pTime < x.Tmax,]),
    Seal=picker(unlist(x$Seal)),
    Loc=picker(unique(x$Location)),
    Tmin=slider(min(x$pTime), max(x$pTime)),
    Tmax=slider(min(x$pTime), max(x$pTime)))

希望它有所帮助。