0

I am recently new to r programming and would like some help with plotting some data as a function of time.

Here is my data:

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

I have used the following function to instruct R that the "Onset" variable is a date vector and the "Time" variable is a time vector by the following lines of code:

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

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

My data is the diving data of a seal, I would like to plot the maximum depth ("Max") reached by the seal at different times ("Time") in the trip. I can do this very simply in microsoft excel by plotting "Max" as a line graph and setting the x-axis names to "Time", but I am struggling to do it in R!

I have tried coding it like the following:

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)

Please help!

Thank you,

Will

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • 3
    posting the results of `dput(data)` would be much better here than the pasted data – user1317221_G Nov 17 '13 at 17:04
  • Sorry, I'm not sure exactly how to do that! – Dr Devilish Nov 17 '13 at 17:09
  • 1
    if your data is called say `mydata` type `dput(mydata)` in `R` and paste the results instead of the pasted data you have. As it stands we can not easily paste your data into `R` and use it. – user1317221_G Nov 17 '13 at 17:11
  • 1
    There is an error in your header... `Dive` appears twice and there are more labels then columns. – nico Nov 17 '13 at 17:37
  • 1
    Also, lines 2,4,5 and 6 do not seem to be complete... – nico Nov 17 '13 at 17:40
  • 1
    [Read this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) It'll help you to make a good question by providing a good reproducible example. – Jilber Urbina Nov 17 '13 at 17:46

1 Answers1

1

I can't import your data. Hence I can't guaranty that there will not have any error if you run this code in your R console . The important thing is the principle. My first advice would be to group "Onset" and "Time" together.

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

Then plot

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

If you are using RStudio you can try the manipulate function to filter the data dynamicaly...

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

Hope it helps.

SESman
  • 238
  • 2
  • 9
  • Hi SESman, Thanks for the help. I appreciate that my data had been pasted badly so I will improve that for any future questions, apologies. I've attempted your code with my own objects in R but I am receiving the following error: Error in slider(min(x$datetime), max(x$datetime)) : min, max, amd initial must all be numeric values – Dr Devilish Nov 18 '13 at 09:22
  • Try to replace each occurrence of `x$pTime` by `as.numeric(x$pTime)` in the manipulate environment. – SESman Nov 18 '13 at 09:33
  • You can also add `main=paste("From", min(x$pTime[as.numeric(x$pTime) >= Tmin]) "to", max(x$pTime[as.numeric(x$pTime) <= Tmax]))` to the `plot()` arguments in order to get the time lapse of the dispayed data. – SESman Nov 18 '13 at 10:06
  • I'll have a try of these and get back to you. Thanks again! – Dr Devilish Nov 18 '13 at 10:09
  • Hi SESman, I transformed the data into an as.numeric and it now seems to have plotted out correctly, however when I moved the sliders in the Manipulate window I am given the following error: Error in `[.data.frame`(x, x$Seal == Seal & x$Location == Loc & x$date_time > : undefined columns selected – Dr Devilish Nov 18 '13 at 10:30
  • `undefined columns selected` generally means that your have made a syntax error: index or variable name referring to a variable that doesn't exist in the table. – SESman Nov 18 '13 at 10:37