Plot only maximum values per hour

Viewed 74

I want to create a graph that plots only the maximum values (depth) per hour over the entire dataset (FYI, my datasets are hundreds of thousands of data points). I have been told that R likes it better if times are in integers, so I have already translated my times into minutes and hours.

Ex of dataset:

Date        Time     Minutes past midnight  Hours past midnight Depth
4-Nov-08  21:19:00    1279              21.3167        3

Eventually we are trying to create a cyclical GAM from this data, so if anyone has any code for that also would be amazing!

Thanks!

1 Answers

Here is one approach you can take

library(tidyverse)
library(lubridate)

dt <- data.frame(timestamp = c(
  '2022-03-04 01:00:49',
  '2022-03-04 01:03:00',
  '2022-03-04 02:00:01',
  '2022-03-04 02:00:00',
  '2022-03-04 02:00:49',
  '2022-03-04 02:00:00',
  '2022-03-04 03:02:00',
  '2022-03-04 03:33:00',
  '2022-03-04 03:45:00'),
  depth = c(1,2,3,4,5,6,7,8,9)
)

dt

dt %>% 
  mutate(hour = hour(timestamp)) %>% 
  group_by(hour) %>% 
  summarise(max_depth = max(depth)) %>%
  ggplot(aes(y = max_depth, x = hour)) + geom_point()

## timestamp             val
## 2022-03-04 01:00:49   1
## 2022-03-04 01:03:00   2
## 2022-03-04 02:00:01   3
## 2022-03-04 02:00:00   4
## 2022-03-04 02:00:49   5
## 2022-03-04 02:00:00   6
## 2022-03-04 03:02:00   7
## 2022-03-04 03:33:00   8
## 2022-03-04 03:45:00   9

enter image description here

Related