1

I'm a beginner and I'm learning R. I plot()ed my first graph in R using the following code:

plot(main="distance travelled collar 41361", data_travel$hour, 
     data_travel$avg_meters, type="p",pch=19,
     xlab="Hour of the day",ylab="Average distance travelled")

Which produced:the graph below

I would like to add some vertical sd bars and a line connecting all the points, but even when using previous codes available in the forum I'm not getting the desired result.

This is the format I'm after for the error bars:enter image description here

Help would be appreciated to add this two features to the graph. Please let me know if any other information is required.

The data plotted data_travel originates from the following code:

data_travel<-datanet %>% 
  mutate(
    date = dmy_hm(`Date & Time [Local]`),
    hour = hour(date)
  ) %>% 
  group_by(hour) %>%
  summarise(
    avg_meters = mean(meters)
  )

So I'm providing a sample of datanet:

> datanet<-datanet[c(1:600),]
> dput(datanet)
structure(list(`Date & Time [Local]` = c("18/06/2018 03:08", 
"18/06/2018 03:12", "18/06/2018 03:16", "18/06/2018 03:20", "18/06/2018 03:24", 
"18/06/2018 03:28", "18/06/2018 03:33", "18/06/2018 03:36", "18/06/2018 03:40", 
"18/06/2018 03:45", "18/06/2018 03:48", "18/06/2018 03:52", "18/06/2018 03:56", 
"18/06/2018 04:00", "18/06/2018 04:04", "18/06/2018 04:08", "18/06/2018 04:12", 
"18/06/2018 04:16", "18/06/2018 04:20", "18/06/2018 04:24", "18/06/2018 04:28", 
"18/06/2018 04:32", "18/06/2018 04:36", "18/06/2018 04:40", "18/06/2018 04:44", 
"18/06/2018 04:48", "18/06/2018 04:52", "18/06/2018 04:56", "18/06/2018 05:00", 
"18/06/2018 05:04", "18/06/2018 05:08", "18/06/2018 05:12", "18/06/2018 05:16", 
"18/06/2018 05:20", "18/06/2018 05:24", "18/06/2018 05:28", "18/06/2018 05:32", 
"18/06/2018 05:36", "18/06/2018 05:40", "18/06/2018 05:44", "18/06/2018 05:48", 
"18/06/2018 05:52", "18/06/2018 05:56", "18/06/2018 06:00", "18/06/2018 06:05", 
"18/06/2018 06:08", "18/06/2018 06:12", "18/06/2018 06:16", "18/06/2018 06:20", 
"18/06/2018 06:24", "18/06/2018 06:28", "18/06/2018 06:32", "18/06/2018 06:36", 
"18/06/2018 06:40", "18/06/2018 06:44", "18/06/2018 06:48", "18/06/2018 06:52", 
"18/06/2018 06:57", "18/06/2018 07:01", "18/06/2018 07:04", "18/06/2018 07:08", 
"18/06/2018 07:12", "18/06/2018 07:17", "18/06/2018 07:20", "18/06/2018 07:24", 
"18/06/2018 07:28", "18/06/2018 07:32", "18/06/2018 07:36", "18/06/2018 07:40", 
"18/06/2018 07:44", "18/06/2018 07:48", "18/06/2018 07:52", "18/06/2018 07:56", 
"18/06/2018 08:00", "18/06/2018 08:04", "18/06/2018 08:08", "18/06/2018 08:12", 
"18/06/2018 08:16", "18/06/2018 08:20", "18/06/2018 08:24", "18/06/2018 08:28", 
"18/06/2018 08:32", "18/06/2018 08:36", "18/06/2018 08:40", "18/06/2018 08:44", 
"18/06/2018 08:48", "18/06/2018 08:52", "18/06/2018 08:56", "18/06/2018 09:00", 
"18/06/2018 09:04", "18/06/2018 09:08", "18/06/2018 09:12", "18/06/2018 09:16", 
"18/06/2018 09:20", "18/06/2018 09:24", "18/06/2018 09:28", "18/06/2018 09:32", 
"18/06/2018 09:40", "18/06/2018 09:44", "18/06/2018 09:48", "18/06/2018 09:52", 
"18/06/2018 09:56", "18/06/2018 10:00", "18/06/2018 10:05", "18/06/2018 10:08", 
"18/06/2018 10:12", "18/06/2018 10:17", "18/06/2018 10:20", "18/06/2018 10:25", 
"18/06/2018 10:29", "18/06/2018 10:32", "18/06/2018 10:36", "18/06/2018 10:40", 
"18/06/2018 10:44", "18/06/2018 10:48", "18/06/2018 10:52", "18/06/2018 10:56", 
"18/06/2018 11:01", "18/06/2018 11:04", "18/06/2018 11:09", "18/06/2018 11:12", 
"18/06/2018 11:17", "18/06/2018 11:22", "18/06/2018 11:25", "18/06/2018 11:30", 
"18/06/2018 11:33", "18/06/2018 11:36", "18/06/2018 11:41", "18/06/2018 11:46", 
"18/06/2018 11:49", "18/06/2018 11:53", "18/06/2018 11:56", "18/06/2018 12:01", 
"18/06/2018 12:05", "18/06/2018 12:09", "18/06/2018 12:13", "18/06/2018 12:17", 
"18/06/2018 12:22", "18/06/2018 12:29", "18/06/2018 12:38", "18/06/2018 12:41", 
"18/06/2018 12:44", "18/06/2018 12:48", "18/06/2018 12:52", "18/06/2018 12:56", 
"18/06/2018 13:00", "18/06/2018 13:04", "18/06/2018 13:08", "18/06/2018 13:12", 
"18/06/2018 13:16", "18/06/2018 13:20", "18/06/2018 13:24", "18/06/2018 13:29", 
"18/06/2018 13:34", "18/06/2018 13:37", "18/06/2018 13:40", "18/06/2018 13:44", 
"18/06/2018 13:48", "18/06/2018 13:52", "18/06/2018 13:56", "18/06/2018 14:00", 
"18/06/2018 14:04", "18/06/2018 14:08", "18/06/2018 14:12", "18/06/2018 14:16", 
"18/06/2018 14:20", "18/06/2018 14:24", "18/06/2018 14:29", "18/06/2018 14:32", 
"18/06/2018 14:36", "18/06/2018 14:40", "18/06/2018 14:44", "18/06/2018 14:48", 
"18/06/2018 14:52", "18/06/2018 14:56", "18/06/2018 15:01", "18/06/2018 15:04", 
"18/06/2018 15:08", "18/06/2018 15:12", "18/06/2018 15:16", "18/06/2018 15:21", 
"18/06/2018 15:24", "18/06/2018 15:28", "18/06/2018 15:32", "18/06/2018 15:36", 
"18/06/2018 15:40", "18/06/2018 15:44", "18/06/2018 15:48", "18/06/2018 15:52", 
"18/06/2018 15:56", "18/06/2018 16:00", "18/06/2018 16:04", "18/06/2018 16:08", 
"18/06/2018 16:12", "18/06/2018 16:16", "18/06/2018 16:20", "18/06/2018 16:24", 
"18/06/2018 16:28", "18/06/2018 16:32", "18/06/2018 16:36", "18/06/2018 16:40", 
"18/06/2018 16:44", "18/06/2018 16:48", "18/06/2018 16:52", "18/06/2018 16:56", 
"18/06/2018 17:00", "18/06/2018 17:04", "18/06/2018 17:08", "18/06/2018 17:12", 
"18/06/2018 17:16", "18/06/2018 17:20", "18/06/2018 17:24", "18/06/2018 17:28", 
"18/06/2018 17:32", "18/06/2018 17:36", "18/06/2018 17:40", "18/06/2018 17:44", 
"18/06/2018 17:48", "18/06/2018 17:52", "18/06/2018 17:56", "18/06/2018 18:00", 
"18/06/2018 18:04", "18/06/2018 18:08", "18/06/2018 18:12", "18/06/2018 18:16", 
"18/06/2018 18:20", "18/06/2018 18:24", "18/06/2018 18:28", "18/06/2018 18:32", 
"18/06/2018 18:36", "18/06/2018 18:40", "18/06/2018 18:44", "18/06/2018 18:48", 
"18/06/2018 18:52", "18/06/2018 18:56", "18/06/2018 19:00", "18/06/2018 19:04", 
"18/06/2018 19:08", "18/06/2018 19:12", "18/06/2018 19:16", "18/06/2018 19:20", 
"18/06/2018 19:24", "18/06/2018 19:28", "18/06/2018 19:32", "18/06/2018 19:36", 
"18/06/2018 19:40", "18/06/2018 19:44", "18/06/2018 19:48", "18/06/2018 19:52", 
"18/06/2018 19:56", "18/06/2018 20:00", "18/06/2018 20:04", "18/06/2018 20:08", 
"18/06/2018 20:12", "18/06/2018 20:16", "18/06/2018 20:20", "18/06/2018 20:24", 
"18/06/2018 20:28", "18/06/2018 20:32", "18/06/2018 20:36", "18/06/2018 20:40", 
"18/06/2018 20:44", "18/06/2018 20:48", "18/06/2018 20:52", "18/06/2018 20:56", 
"18/06/2018 21:00", "18/06/2018 21:04", "18/06/2018 21:08", "18/06/2018 21:12", 
"18/06/2018 21:16", "18/06/2018 21:20", "18/06/2018 21:24", "18/06/2018 21:28", 
"18/06/2018 21:32", "18/06/2018 21:36", "18/06/2018 21:40", "18/06/2018 21:44", 
"18/06/2018 21:48", "18/06/2018 21:52", "18/06/2018 21:56", "18/06/2018 22:00", 
"18/06/2018 22:04", "18/06/2018 22:08", "18/06/2018 22:12", "18/06/2018 22:16", 
"18/06/2018 22:20", "18/06/2018 22:24", "18/06/2018 22:28", "18/06/2018 22:32", 
"18/06/2018 22:36", "18/06/2018 22:40", "18/06/2018 22:44", "18/06/2018 22:48", 
"18/06/2018 22:52", "18/06/2018 22:56", "18/06/2018 23:00", "18/06/2018 23:04", 
"18/06/2018 23:08", "18/06/2018 23:12", "18/06/2018 23:16", "18/06/2018 23:20", 
"18/06/2018 23:24", "18/06/2018 23:28", "18/06/2018 23:32", "18/06/2018 23:36", 
"18/06/2018 23:40", "18/06/2018 23:44", "18/06/2018 23:48", "18/06/2018 23:52", 
"18/06/2018 23:56", "19/06/2018 00:00", "19/06/2018 00:04", "19/06/2018 00:08", 
"19/06/2018 00:12", "19/06/2018 00:16", "19/06/2018 00:20", "19/06/2018 00:24", 
"19/06/2018 00:28", "19/06/2018 00:32", "19/06/2018 00:36", "19/06/2018 00:40", 
"19/06/2018 00:44", "19/06/2018 00:48", "19/06/2018 00:52", "19/06/2018 00:56", 
"19/06/2018 01:00", "19/06/2018 01:04", "19/06/2018 01:08", "19/06/2018 01:12", 
"19/06/2018 01:16", "19/06/2018 01:20", "19/06/2018 01:24", "19/06/2018 01:28", 
"19/06/2018 01:32", "19/06/2018 01:36", "19/06/2018 01:40", "19/06/2018 01:44", 
"19/06/2018 01:48", "19/06/2018 01:52", "19/06/2018 01:56", "19/06/2018 02:00", 
"19/06/2018 02:04", "19/06/2018 02:08", "19/06/2018 02:12", "19/06/2018 02:16", 
"19/06/2018 02:20", "19/06/2018 02:24", "19/06/2018 02:28", "19/06/2018 02:32", 
"19/06/2018 02:36", "19/06/2018 02:40", "19/06/2018 02:44", "19/06/2018 02:48", 
"19/06/2018 02:52", "19/06/2018 02:56", "19/06/2018 03:00", "19/06/2018 03:04", 
"19/06/2018 03:08", "19/06/2018 03:12", "19/06/2018 03:16", "19/06/2018 03:20", 
"19/06/2018 03:24", "19/06/2018 03:28", "19/06/2018 03:32", "19/06/2018 03:36", 
"19/06/2018 03:40", "19/06/2018 03:44", "19/06/2018 03:48", "19/06/2018 03:52", 
"19/06/2018 03:56", "19/06/2018 04:00", "19/06/2018 04:04", "19/06/2018 04:08", 
"19/06/2018 04:12", "19/06/2018 04:16", "19/06/2018 04:20", "19/06/2018 04:24", 
"19/06/2018 04:28", "19/06/2018 04:32", "19/06/2018 04:36", "19/06/2018 04:40", 
"19/06/2018 04:44", "19/06/2018 04:48", "19/06/2018 04:52", "19/06/2018 04:56", 
"19/06/2018 05:00", "19/06/2018 05:04", "19/06/2018 05:08", "19/06/2018 05:12", 
"19/06/2018 05:16", "19/06/2018 05:20", "19/06/2018 05:24", "19/06/2018 05:28", 
"19/06/2018 05:32", "19/06/2018 05:36", "19/06/2018 05:40", "19/06/2018 05:44", 
"19/06/2018 05:48", "19/06/2018 05:52", "19/06/2018 05:56", "19/06/2018 06:00", 
"19/06/2018 06:04", "19/06/2018 06:08", "19/06/2018 06:12", "19/06/2018 06:16", 
"19/06/2018 06:20", "19/06/2018 06:24", "19/06/2018 06:28", "19/06/2018 06:32", 
"19/06/2018 06:36", "19/06/2018 06:40", "19/06/2018 06:44", "19/06/2018 06:48", 
"19/06/2018 06:52", "19/06/2018 06:56", "19/06/2018 07:00", "19/06/2018 07:04", 
"19/06/2018 07:08", "19/06/2018 07:12", "19/06/2018 07:16", "19/06/2018 07:20", 
"19/06/2018 07:24", "19/06/2018 07:28", "19/06/2018 07:32", "19/06/2018 07:36", 
"19/06/2018 07:40", "19/06/2018 07:44", "19/06/2018 07:48", "19/06/2018 07:52", 
"19/06/2018 07:56", "19/06/2018 08:00", "19/06/2018 08:04", "19/06/2018 08:08", 
"19/06/2018 08:12", "19/06/2018 08:16", "19/06/2018 08:20", "19/06/2018 08:24", 
"19/06/2018 08:28", "19/06/2018 08:32", "19/06/2018 08:36", "19/06/2018 08:40", 
"19/06/2018 08:44", "19/06/2018 08:48", "19/06/2018 08:52", "19/06/2018 08:56", 
"19/06/2018 09:00", "19/06/2018 09:04", "19/06/2018 09:08", "19/06/2018 09:12", 
"19/06/2018 09:16", "19/06/2018 09:20", "19/06/2018 09:24", "19/06/2018 09:28", 
"19/06/2018 09:32", "19/06/2018 09:36", "19/06/2018 09:40", "19/06/2018 09:44", 
"19/06/2018 09:48", "19/06/2018 09:52", "19/06/2018 09:56", "19/06/2018 10:00", 
"19/06/2018 10:04", "19/06/2018 10:08", "19/06/2018 10:12", "19/06/2018 10:16", 
"19/06/2018 10:20", "19/06/2018 10:24", "19/06/2018 10:28", "19/06/2018 10:32", 
"19/06/2018 10:36", "19/06/2018 10:40", "19/06/2018 10:44", "19/06/2018 10:48", 
"19/06/2018 10:52", "19/06/2018 10:56", "19/06/2018 11:00", "19/06/2018 11:04", 
"19/06/2018 11:08", "19/06/2018 11:12", "19/06/2018 11:16", "19/06/2018 11:21", 
"19/06/2018 11:24", "19/06/2018 11:28", "19/06/2018 11:32", "19/06/2018 11:36", 
"19/06/2018 11:40", "19/06/2018 11:44", "19/06/2018 11:48", "19/06/2018 11:52", 
"19/06/2018 11:56", "19/06/2018 12:00", "19/06/2018 12:04", "19/06/2018 12:08", 
"19/06/2018 12:12", "19/06/2018 12:16", "19/06/2018 12:20", "19/06/2018 12:24", 
"19/06/2018 12:28", "19/06/2018 12:32", "19/06/2018 12:36", "19/06/2018 12:40", 
"19/06/2018 12:44", "19/06/2018 12:48", "19/06/2018 12:52", "19/06/2018 12:56", 
"19/06/2018 13:00", "19/06/2018 13:04", "19/06/2018 13:08", "19/06/2018 13:12", 
"19/06/2018 13:16", "19/06/2018 13:20", "19/06/2018 13:24", "19/06/2018 13:28", 
"19/06/2018 13:32", "19/06/2018 13:36", "19/06/2018 13:40", "19/06/2018 13:44", 
"19/06/2018 13:48", "19/06/2018 13:52", "19/06/2018 13:56", "19/06/2018 14:00", 
"19/06/2018 14:04", "19/06/2018 14:08", "19/06/2018 14:12", "19/06/2018 14:16", 
"19/06/2018 14:20", "19/06/2018 14:24", "19/06/2018 14:28", "19/06/2018 14:32", 
"19/06/2018 14:36", "19/06/2018 14:40", "19/06/2018 14:44", "19/06/2018 14:48", 
"19/06/2018 14:52", "19/06/2018 14:56", "19/06/2018 15:00", "19/06/2018 15:04", 
"19/06/2018 15:08", "19/06/2018 15:12", "19/06/2018 15:16", "19/06/2018 15:20", 
"19/06/2018 15:24", "19/06/2018 15:28", "19/06/2018 15:32", "19/06/2018 15:36", 
"19/06/2018 15:40", "19/06/2018 15:44", "19/06/2018 15:48", "19/06/2018 15:52", 
"19/06/2018 15:56", "19/06/2018 16:00", "19/06/2018 16:04", "19/06/2018 16:08", 
"19/06/2018 16:12", "19/06/2018 16:16", "19/06/2018 16:20", "19/06/2018 16:24", 
"19/06/2018 16:28", "19/06/2018 16:32", "19/06/2018 16:36", "19/06/2018 16:40", 
"19/06/2018 16:44", "19/06/2018 16:48", "19/06/2018 16:52", "19/06/2018 16:56", 
"19/06/2018 17:00", "19/06/2018 17:04", "19/06/2018 17:08", "19/06/2018 17:12", 
"19/06/2018 17:16", "19/06/2018 17:20", "19/06/2018 17:24", "19/06/2018 17:28", 
"19/06/2018 17:32", "19/06/2018 17:36", "19/06/2018 17:40", "19/06/2018 17:44", 
"19/06/2018 17:48", "19/06/2018 17:52", "19/06/2018 17:56", "19/06/2018 18:00", 
"19/06/2018 18:04", "19/06/2018 18:08", "19/06/2018 18:12", "19/06/2018 18:16", 
"19/06/2018 18:20", "19/06/2018 18:24", "19/06/2018 18:28", "19/06/2018 18:32", 
"19/06/2018 18:36", "19/06/2018 18:40", "19/06/2018 18:44", "19/06/2018 18:48", 
"19/06/2018 18:52", "19/06/2018 18:56", "19/06/2018 19:00", "19/06/2018 19:04", 
"19/06/2018 19:08", "19/06/2018 19:12", "19/06/2018 19:16"), 
    meters = c(15.9593660078151, 22.5355660523, 12.0368343188154, 
    18.7381339674668, 26.7818788216134, 8.34165858190966, 11.3327576966507, 
    15.7369069237407, 9.72882849925771, 10.297033774998, 4.05410707329196, 
    2.80921200361505, 5.99863428254126, 3.43702889630238, 1.0973506459739, 
    14.9210205443641, 11.8623534125001, 5.52037842119764, 3.47842432433199, 
    8.56352953345666, 10.3348047169779, 4.75068270841804, 10.2158524906783, 
    8.40843212136055, 19.176769622017, 8.27181435890216, 4.63398751788878, 
    4.64609200546047, 11.2731653163543, 11.4312910706539, 10.532832329052, 
    9.42036510967477, 3.72017081997164, 19.1405081193573, 7.62135953649555, 
    22.0583337290673, 5.17601163631861, 2.94307133012786, 8.16672175526036, 
    11.0388731772229, 8.4520380008427, 14.4121396478424, 9.9917860049159, 
    15.2689800274799, 23.5027696734591, 11.5713613868424, 3.76275614223104, 
    7.44089224335579, 4.75143353354129, 25.5863700372985, 4.12640308269035, 
    17.0185845366389, 9.19416438574403, 0.504992122982213, 7.58240103848382, 
    15.0141241467732, 47.820448738411, 48.9253417447483, 28.6982306077959, 
    30.0325794078543, 10.6419955753018, 16.3060328028124, 16.9950882683356, 
    17.5456158130927, 6.11772166759097, 10.3181549737979, 8.25795394979096, 
    12.5452581928342, 7.37344423304636, 14.0784069615297, 15.5217701103853, 
    5.85740700912933, 67.5814597516992, 86.1667068274933, 6.71080257210109, 
    12.8712310356668, 26.3464242058092, 14.862829937119, 17.1827244506893, 
    21.007162484455, 14.6159667602995, 15.0116767634002, 3.44415089036116, 
    2.90091354423872, 15.4060076069905, 11.0647299060198, 5.47803214985054, 
    21.4699825151886, 13.9952170296303, 5.47503135778305, 9.59949901256039, 
    2.67150469440983, 5.81615251769606, 18.7977607686961, 17.1405856185114, 
    0.770680478662077, 2.11599500699301, 14.4215651159807, 21.340644921803, 
    9.44803939591664, 9.35914024298419, 26.5419236409891, 12.2076659835784, 
    11.6727676178834, 11.5183958294441, 11.1464930265903, 4.48800980044162, 
    14.5010190190289, 17.9964315949038, 6.19790617124279, 11.4579719854227, 
    7.02888318334768, 2.43292600920741, 7.09331125112953, 7.41769642742311, 
    6.15912646167791, 6.17702835583737, 5.50679980253089, 27.2967339245239, 
    26.809594968574, 16.6454622873968, 6.74805054317244, 5.69175256676157, 
    5.28218041234659, 7.75355680259654, 9.13245480739586, 17.2138677622628, 
    30.8757344431557, 10.4532031567326, 6.64426961601855, 7.62833591252156, 
    67.2325093819334, 58.8815117188855, 14.67853688946, 10.4197239865493, 
    25.850404929181, 14.8215484542981, 17.8354868952429, 4.53929715323194, 
    2.49940267894446, 7.15430231148364, 9.79764663049093, 11.2388351166403, 
    4.22708779610617, 13.1226888741628, 6.86582195506769, 21.0333162925842, 
    24.2027940323749, 15.164801715416, 20.8875492434141, 10.8583121642675, 
    17.9399411230084, 23.4730382740048, 11.7208011931361, 6.08657685375735, 
    19.4111128168678, 15.6735709416045, 15.4027288613498, 14.7406668388859, 
    4.21308837483828, 6.9823143969642, 3.72864795928499, 17.4100339528372, 
    19.5473631918946, 11.7404262614498, 11.6668635506407, 4.51437593270175, 
    25.5020957862817, 2.42357811960285, 16.8353939403993, 7.85156097432205, 
    18.1401928118823, 1.14240586696655, 28.0586180742577, 59.6003755600272, 
    741.364692233718, 2451.03176274837, 1924.25225046211, 91.0077010587984, 
    129.914778093003, 23.7263560261478, 19.2052584330969, 31.2431026776704, 
    303.356757432854, 69.3573791615057, 79.2353341460047, 40.001574391757, 
    125.241489048742, 0.875997360851126, 101.761469702257, 14.3803188275614, 
    19.3916155295248, 16.5485908318872, 36.7050416049996, 2.28524732255712, 
    59.3064322763378, 97.7492244922703, 2.4560922159233, 18.9182418683955, 
    29.1775598809323, 72.8131889738511, 0.536696681632468, 1.66920881986582, 
    1.1474807643325, 1.00624417050841, 34.7753323815772, 13.1764541914541, 
    0.987640444852192, 46.8576255846636, 12.6260387130311, 0.981930298073766, 
    0, 95.5457420387006, 36.4423832793374, 11.3666824424413, 
    14.6529671509152, 11.6555883294269, 22.7960129030282, 2.49939892511722, 
    2.1865821808201, 9.09470367692037, 52.9342974939483, 6.47968205299305, 
    1.08340934139405, 0.560041122020628, 0.536696441270263, 1.32908923984974, 
    0.86507269221477, 1.22330477453381, 70.0552350787317, 12.3451237647639, 
    3.11666258579037, 1.93736182424659, 3.51794257381499, 1.32908899309918, 
    2.31154944415652, 0.834805281491471, 1.64638218465927, 0.504255396690464, 
    3.17394653919415, 1.75132408931791, 1.49111519239348, 2.73481993931031, 
    2.00143073945307, 1.4365841848048, 1.37960800380212, 2.8231207877357, 
    2.27843509169923, 22.1135221191054, 9.09386309784055, 1.63627180145216, 
    6.40111320930751, 7.05302865648799, 7.19422051080435, 4.25534285783686, 
    1.94331689005661, 0.981930279418075, 2.18872974709499, 1.30702979274852, 
    1.01730181071795, 0.25351813244195, 1.01094711070046, 2.63413039676338, 
    1.97998436214355, 1.95035919800779, 0.800780147342046, 1.71618874492291, 
    1.14748009836729, 2.09171118450703, 3.35928280239732, 2.69012333124418, 
    2.61191486817587, 7.44266937655, 1.02601599075127, 2.84418905758968, 
    1.96914434184303, 2.78048662570437, 1.93865533359675, 3.1881307017127, 
    12.9836375626473, 2.03466874517043, 3.2650256164747, 5.49720574599675, 
    3.16684326703025, 3.61040117444932, 2.4393235459518, 4.41349847491449, 
    1.39024319239671, 1.70331614558013, 1.1894815496101, 2.07256571088354, 
    0, 1.31556162525377, 0.3269295629247, 1.2315075039072, 1.12985048401375, 
    4.12639695258049, 3.53057858117975, 1.14689379910243, 1.89463915995567, 
    0.326929561525381, 2.12981387194087, 0.29095890697518, 1.69583706057361, 
    1.12824512647375, 2.63413021072766, 2.46969082725569, 1.69695134562863, 
    6.60037145525014, 4.08280291298734, 1.91314714385751, 4.10209917791834, 
    2.44808743796531, 58.5958105549192, 49.0573915168004, 1.71618923544311, 
    2.16623234264144, 33.7528092143734, 1.26876292628825, 4.09301793236524, 
    6.47380341213986, 2.02287444044928, 6.15764791418056, 4.88198337685391, 
    6.1518534564327, 19.9293981357374, 31.2807443706163, 11.9760177553429, 
    7.80034538189425, 2.09856685581426, 15.5559610933875, 5.54425190776513, 
    4.31727752887657, 1.69695203818687, 3.13701726469391, 4.17079079435503, 
    0, 0.253518261190509, 0.768515387271088, 5.75312128614323, 
    1.10161787301914, 0, 1.63627263077218, 1.94331762790558, 
    0.984128914771002, 0, 1.891376142142, 1.88169045315321, 1.14968339087591, 
    1.94662436122789, 2.53667294419711, 2.22463784687921, 1.14716037069784, 
    1.72269599615416, 0.253518262766866, 0.557086177521366, 0.834805798848609, 
    0.834805798848609, 0.834805798848609, 0.32373515360376, 0.250851604274291, 
    1.13037794205011, 1.01730179938836, 1.08245009766703, 1.29008139493895, 
    0.592050658495912, 0.984128914593752, 1.14716037069784, 0.246921421310107, 
    1.08086752437222, 0, 0.253518261190127, 0.834146432905571, 
    0.834146432905571, 1.32088856328962, 0, 1.01730179951988, 
    0.557086174016414, 0, 0, 0.981930302577223, 1.01730179952143, 
    0.801467385970428, 1.891376142142, 0.984128914771002, 0.984128914771002, 
    1.891376142142, 1.31556194378487, 1.70331684297699, 11.4746462151774, 
    5.19876037652309, 18.1991232643532, 5.45048803468078, 3.50474735760544, 
    0.834439563061093, 0, 1.29008140210492, 0.984760642562075, 
    1.01730179907391, 32.657190675824, 39.7593130805261, 34.2547425122791, 
    12.1116764053771, 18.526009117261, 3.86538989203226, 7.07005984938157, 
    1.72929805654892, 6.73320732100181, 23.0796824899771, 66.8539968083065, 
    101.031538229766, 142.563193250685, 117.93396678646, 30.4231296530894, 
    43.3196594211314, 14.3288406144688, 6.01740959634099, 7.35496556381327, 
    7.0780957149326, 1.06782836828317, 10.3553242863905, 59.205976673877, 
    34.5440414717095, 90.5195981896788, 32.8711449150829, 3.40710213072646, 
    6.60140695190154, 20.751091499863, 1.89137226358219, 2.83321448749677, 
    26.5437774761593, 4.78947844059108, 15.9299548924479, 3.49392462916022, 
    0.834803361386949, 53.9702480435499, 44.7123678229481, 4.58801253896994, 
    9.09964716760873, 52.4316347054904, 83.5342865610675, 4.66725660885041, 
    12.5978430947004, 1.94751698101872, 0.504253850871525, 3.45174941271515, 
    1.63626628055968, 5.40488600424627, 12.3995886978579, 16.9313577940681, 
    3.91617610821253, 4.70252672649754, 17.2466244205231, 5.87640930205112, 
    7.83322335835387, 68.1235163864855, 3.42589865816882, 13.1957354034354, 
    11.2205841718684, 1.01730188003348, 2.08209678645147, 2.82727432802774, 
    32.5321887823726, 12.067922456633, 1.8035643067272, 1.2090985158482, 
    2.5520360706561, 3.727677427748, 18.1658365688268, 2.53389262037358, 
    4.5038564350202, 3.99153396380181, 26.3513692155869, 52.630041380923, 
    53.0536675276293, 43.8202570822247, 17.9563768133082, 35.8843410313514, 
    2.52741128965252, 2.07122243755901, 61.1369106676384, 31.0602902769797, 
    1.93735691953792, 1.39063392798036, 4.74080462885894, 1.83677904498507, 
    1.5837058884799, 24.8779450701876, 1.02751235639547, 5.21277171962475, 
    5.88014964276792, 7.13082208780676, 1.84782941870358, 4.28412823329233, 
    4.8726754921709, 1.80492747976475, 0.834142917601792, 12.8071661996776, 
    45.7714211143098, 14.9358063376832, 117.774596740257, 1.94331089207628, 
    2.89950585430524, 1.55188895271854, 1.97146416004927, 3.62849282585251, 
    0.682586430353306, 1.14715895538192, 84.973523683883, 16.6260802285038, 
    5.17522902371143, 2.99113461676237, 7.1948737543452, 2.71109872104149, 
    4.22696799241781, 4.53907546305565, 5.71728738868701, 8.68379208483034, 
    3.75585732758793, 4.82085426327032, 5.81439758874005, 3.39028745288001, 
    3.55051027017038, 60.2330430998918, 38.3544603220122, 1.21375339001644, 
    1.91785016319426, 0.3237336743715, 1.69694647561642, 1.14877752688831, 
    2.37357312547656, 0, 0.909047979716786, 1.08244477955533, 
    3.35946823082954, 2.16622390570235, 1.9688553226609, 1.13578000777225, 
    1.63525542752795, 1.48883000783912, 2.83467921372661, 2.12754007740878, 
    17.3540112473834, 6.32166165556282, 1.94746224312546, 14.5084951818398, 
    3.71993035536828, 3.82335882963004, 2.84750706444208, 0, 
    1.71618366510756, 0.323733628303048, 6.97407775313294, 27.9890313303321, 
    2.18646977811563, 2.96545099539278, 0, 2.00143091430776, 
    14.0439085683219, 0.25351711418234, 1.69380290439435, 2.28323132482563, 
    3.85986892850026, 2.69197208488191, 2.22437966566857, 9.49208589978412, 
    49.4880243971914, 100.881212266528, 5.84222371086618, 85.6548691469987, 
    0.834143439442611, 71.1685884754485, 10.0799187801841, 37.4349094205841, 
    3.96457080819355, 8.02426971700344, 2.64427066449314, 11.2298595430791, 
    19.152210952781, 25.9621194522378, 2.43632829382703, 6.05555098063067, 
    154.726395809777, 18.7647119416837, 27.2616358671239, 11.5922993940012, 
    68.2775157158671, 49.1853059189567, 35.5259047498724, 28.1895151756555, 
    11.2830643554028, 70.5225858134716, 33.761787160485, 9.92505257193424, 
    12.2487708385319, 4.72780789239903, 2.36949301171681, 0.898029677765623, 
    1.41928645304406, 1.98000060537725)), row.names = c(NA, -600L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000002641ef0>)
G5W
  • 36,531
  • 10
  • 47
  • 80
juancda4
  • 323
  • 1
  • 8
  • 1
    What was wrong with [Scatter plot with error bars](https://stackoverflow.com/q/13032777/4752675) – G5W Mar 28 '19 at 23:23
  • @G5W Unfortunately they don't provide information on creating a line connecting the points. – juancda4 Mar 29 '19 at 09:53
  • The arrow _is_ the line. If you provide your data, I will illustrate. Please type `dput(data_travel)` and paste the result into your question. – G5W Mar 29 '19 at 12:34
  • @G5W There it is. Hope that helps and looking forward to hear back from you. – juancda4 Mar 29 '19 at 14:00
  • At my current location, I do not have lubridate and cannot install it. I will try this weekend if no one else answers first. – G5W Mar 29 '19 at 14:21
  • @G5W Great! Looking forward to hear back from you. – juancda4 Mar 30 '19 at 13:09

1 Answers1

1

First, using data_travel as you provided it, I do not get the same scatterplot as you. Perhaps this was only a sample of the full data and the full data gave your picture. Nevertheless, I think that you should be able to follow my code here and make it work with your data.

There are two parts to your question, connecting the points by a line and drawing the error bars. Let's take them one at a time. First, connecting with a line.

There are two good ways to do that. First, your plot statement included the argument type="p". That gives points, but type="b" is specifically to give both the points and lines between the points.

plot(main="distance travelled collar 41361", 
    data_travel$hour, data_travel$avg_meters, 
    type="b",pch=19,xlab="Hour of the day",
    ylab="Average distance travelled")

First plot with lines

But I find those connecting lines unattractive, so here is a different way. You can plot just the points as you did, then add the lines using the lines function.

plot(main="distance travelled collar 41361", 
    data_travel$hour, data_travel$avg_meters, 
    type="p",pch=19,xlab="Hour of the day",
    ylab="Average distance travelled")
lines(data_travel, col="darkgray")

Second Plot with lines

To me, that looks nicer.

Updated: Finally, we need to add the standard deviation bars to the plot. To get the standard deviations, I made a minor modification to your code above that created data_travel. But notice that like the mean for hour=15, the standard deviation for hour=15 is wildly different than the other hours, If you take a look at the values you provided, you will see that this matches the data.

Updated code for data_travel

data_travel<-datanet %>% 
  mutate(date = dmy_hm(`Date & Time [Local]`),
         hour = hour(date)) %>% 
  group_by(hour) %>%
  summarise(
    avg_meters = mean(meters),
    sd_meters = sd(meters))

I plot the bars using the method described in the previous answer mentioned in my comment Scatter plot with error bars which uses the arrows function to make the error bars.

plot(main="distance travelled collar 41361", 
    data_travel$hour, data_travel$avg_meters, 
    type="p",pch=19,xlab="Hour of the day",
    ylab="Average distance travelled", ylim=c(-50,250))
lines(data_travel, col="darkgray")

sdev = sd(data_travel$avg_meters)
arrows(data_travel$hour, data_travel$avg_meters-data_travel$sd_meters,
    data_travel$hour, data_travel$avg_meters+data_travel$sd_meters,
    length=0.05, angle=90, code=3)

Graph with error bars

Note that the standard deviation on hour=15 is huge. It would be possible to change the ylim so that the full bar would show, but then the other bars would be so small that they would show nothing, so I chose to let that bar just go off the graph.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Fantastic! I have to point out something however. It looks like all of the sd bars are the same for each data point (which is an average) and that shouldn't be the case. The original dataset `datanet` is the one `data_travel` is based on (the code is provided in the original post). If you inspect `datanet`, you will notice that there's a distance travelled measure in the column `meters` for each given time-stamp. `data_travel` returns the average distance travelled by the hour based on `data_net`, so there should also be a different sd value for each hour. Does that make sense? – juancda4 Mar 31 '19 at 12:51
  • Yes, I did use a constant sd and was wondering about what you wanted. If you compute a data_travel$sd in a similar way to your data_travel$avg_meters, you should be able to replace the sd that I used with that and it should work fine. – G5W Mar 31 '19 at 13:10
  • I've tried the same code as for `avg_meters` but I don't manage to find sd that aren't constant. Do you have any idea of a code I could use to find the different values of sd? – juancda4 Mar 31 '19 at 14:47
  • Thank you very much. That is perfect. I was just wondering something however, and it's the fact that the sd bars go below 0 on the y axis. Considering the y axis represents an average distance travelled that can only be positive, how can you explain the presence of negativ sd's? Do you know if there's a way to fix this problem? Please excuse my ignorance if this topic doesn't belong to this post. Anyways I appreciate your help very much. – juancda4 Apr 01 '19 at 10:26
  • @juancda4 While an actual negative value may not make sense, the data that you provided for hour=15 has huge variation (and a mean very different than all of the others. I can't help but wonder if the data contains errors. I think that the right questions to ask is "Why does the data for hour=15 have such large variation? and Why is the data for hour=15 so different from all of the other hours? – G5W Apr 01 '19 at 13:03
  • Considering the meaning of my data, the variation observed at h=15 is normal. If you take a closer look, Sds for h=0 or h=18 are belows 0, that is what I have trouble understanding. – juancda4 Apr 01 '19 at 13:09
  • 1
    I see that those too go negative. This is a weakness of using normal approximations for something that is definitely not normally distributed. A normal distribution would _always_ have a non-zero chance of a negative value. If your data comes from a process in which negative values are impossible, the model will look bad at times. – G5W Apr 01 '19 at 13:14
  • I totally see. Yes, my data comes from a process which negative values are impossible. Is there a way to avoid this by transforming the data somehow? – juancda4 Apr 01 '19 at 13:17
  • 1
    It would very much depend on your data. I know that when modeling peoples salaries, [log-normal distributions](https://en.wikipedia.org/wiki/Log-normal_distribution) are often used. You could test your data to see if that would be a better fit. – G5W Apr 01 '19 at 13:23