I have a data set with time and heart rate data. The time intervals are currently about 5 seconds apart and each have data for hear rate (i.e 7:21AM = Time, 97 = Value... 7:21:05 = Time, 102 = Value.)I want to group the time into hour so that I can create a line graph of how the heart rate changes from hour to hour. When I attempt to load in the data with this many data points the graph does not load properly. How would I go about doing this to create a clean graph that shows how heart rate changes from 0000 to 0100 to 0200 to 0300.. etc etc?
update -- Okay thank you for suggestions, I have added the visualization I am working with. Essentially I just want to add values across the x axis which represent 7 Am to 8pm.
structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1"),
Time = c("4/12/2016 7:21:00 AM", "4/12/2016 7:21:05 AM",
"4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM", "4/12/2016 7:21:25 AM",
"4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM", "4/12/2016 7:22:15 AM",
"4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM"), Value = c(97L,
102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L)), row.names = c(NA,
10L), class = "data.frame")
CodePudding user response:
Updated
We can convert into a POSIXct
format, then plot the data. We can use scale_x_datetime
to specify plotting at 1 hour intervals showing just hour, minute, and AM/PM.
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = as.POSIXct(strptime(Time, "%m/%d/%Y %I:%M:%S %p"), format = "%m/%d/%Y %H:%M:%OS %p")) %>%
ggplot(aes(x = Time, y = Value))
geom_line()
theme_bw()
scale_x_datetime(breaks = "1 hour", date_labels = "%I:%M %p")
Output
Data
df <- structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1", "user_1",
"user_1", "user_1"), Time = c("4/12/2016 6:50:00 AM", "4/12/2016 7:21:00 AM",
"4/12/2016 7:21:05 AM", "4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM",
"4/12/2016 7:21:25 AM", "4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM",
"4/12/2016 7:22:15 AM", "4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM",
"4/12/2016 7:55:20 AM", "4/12/2016 8:05:25 AM"), Value = c(105L,
97L, 102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L, 96L, 98L
)), row.names = c(NA, 13L), class = "data.frame")
Original Answer
If you want to summarise for each hour, then we could just convert the time to the hour, then get the mean of values for that hour, then convert back to a time format for plotting.
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = hour(hms(format(strptime(Time, "%I:%M:%S %p"), "%H:%M:%S")))) %>%
group_by(Time) %>%
summarise(Value = mean(Value)) %>%
mutate(Time = paste0(Time, ":00"),
Time = as_datetime(hm(Time))) %>%
ggplot(aes(x = Time, y = Value))
geom_line()
theme_bw()
scale_x_datetime(breaks = "1 hour", date_labels = "%H:%M %p")
Output
Data
set.seed(200)
time.seq = format(seq(from=as.POSIXct("04:00:00", format="%H:%M:%OS",tz="UTC"),
to=as.POSIXct("08:59:59", format="%H:%M:%OS", tz="UTC"), by = 5), "%I:%M:%S%p")
df <- data.frame(Time = time.seq, Value = round(runif(3600, 50, 150), digits = 0))