Heart Rate Trends by 5, 10 and 20-Minutes Intervals, Sub. R04
Oigawa Marathon 2023 Oct. 29
Code
# Data processing
# Convert timestamp to POSIXct
data$timestamp <- as.POSIXct(data$timestamp, format = "%Y-%m-%d %H:%M:%S")
# heart rate
heart_rate <- data$heart_rate
# pick up the start and end time of the marathon
start <- min(data$timestamp)
finish <- max(data$timestamp)
# plot heart rate from marathon start to finish
# Remove heart rates over 200
heart_rate <- heart_rate[heart_rate < 200]
# ajust length of `heart_rate` and `timestamp`
heart_rate <- heart_rate[seq_len(length(data$heart_rate))]
# then store `heart_rate` in to data
data$heart_rate <- heart_rateFigure 1 OIGAWA MARATHON 2023, Oct. 29
Code
# prepare plot canvas size
par(mar=c(2, 2, 1, 0)) # bottom, left, top, right margin
# deside plot start and end time on x-axis using `start` and `finish`
# full size plot from start to finish
plot(data$timestamp[data$timestamp >= start & data$timestamp <= finish],
data$heart_rate[data$timestamp >= start & data$timestamp <= finish],
type="l",
xlab="Time (sec)",
ylab="Heart Rate (bpm)")
# Set x-axis tick interval to xx minutes
axis(1, at=seq(9, 23, by=0.5), labels=seq(9, 23, by=0.5),
las=1, cex.axis=0.8, lwd=2)
abline(v=c(start, finish), col="red", lty=2)OIGAWA MARATHON 2023, Oct. 29 (every 5 minutes)
Code
# plot every 5 minutes from start
library(ggplot2)
par(mfrow= c(1, 2))
# for loop to plot every 5 minutes
for (i in seq(1, length(heart_rate), by = 5*60)) {
if (i < length(heart_rate))
# Remove heart rates over 200
heart_rate_filtered <- heart_rate[heart_rate <= 200]
end <- min(i + 5*60 - 1, length(heart_rate_filtered))
plot(data$timestamp[i:end], heart_rate_filtered[i:end],
mar=c(0, 0, 0, 0),
type = "l",
xlab = "Time (sec.)",
ylab = "Hert Rate (bpm)")
#main = paste("Plot Hear Rate per 5 minutes", ceiling(i/(5*60))))
coord_cartesian(ylim = c(140, 170))
abline(v = c(start, finish), col = "red", lty = 2)
}OIGAWA MARATHON 2023, Oct. 29 (every 5 minutes after 5 minutes the start)
Code
# plot every 5 minutes starting from 5 minutes after the start
library(ggplot2)
par(mfrow=c(1, 3))
# for loop to plot every 5 minutes after 5 minutes from the start
for (i in seq(1, length(heart_rate), by = 5*60)) {
if (i < length(heart_rate)) {
# Remove heart rates over 200
heart_rate_filtered <- heart_rate[heart_rate <= 200]
end <- min(i + 5*60 - 1, length(heart_rate_filtered))
# Set y-axis range within the loop
plot(data$timestamp[i:end], heart_rate_filtered[i:end],
mar=c(0, 0, 0, 0),
type = "l",
xlab = "Time (sec.)",
ylab = "Hert Rate (bpm)") #ylim時は))), scale_y時は))))
#main = paste("Plot Hear Rate per 5 minutes", ceiling(i/(5*60))))
#ylim = c(162, 176))
coord_cartesian(ylim = c(140, 170))
#scale_y_continuous(limits = c(140, 170), expand = c(0, 0.1))
abline(v = c(start, finish), col = "red", lty = 2)
}
}OIGAWA MARATHON 2023, Oct. 29 (every 10 minutes after 5 minutes the start)
Code
# plot every 5 minutes starting from 5 minutes after the start
library(ggplot2)
par(mfrow=c(1, 2))
interval <- 10
# for loop to plot every 5 minutes after 5 minutes from the start
for (i in seq(5*60, length(heart_rate), by = interval*60)) {
if (i < length(heart_rate)) {
# Remove heart rates over 200
heart_rate_filtered <- heart_rate[heart_rate <= 200]
end <- min(i + interval*60 - 1, length(heart_rate_filtered))
# Set y-axis range within the loop
plot(data$timestamp[i:end], heart_rate_filtered[i:end],
mar=c(0, 0, 0, 0),
type = "l",
xlab = "Time (sec.)",
ylab = "Hert Rate (bpm)",
#main = paste("Plot Hear Rate per 5 minutes", ceiling(i/(10*60))),
ylim = c(130, 179))
#scale_y_continuous(limits = c(140, 170), expand = c(0, 0.1))
abline(v = c(start, finish), col = "red", lty = 2)
}
}Code
# plot every 5 minutes starting from 5 minutes after the start
library(ggplot2)
par(mfrow=c(1, 1))
interval <- 20
# for loop to plot every 20 minutes after 5 minutes from the start
for (i in seq(5*60, length(heart_rate), by = interval*60)) {
if (i < length(heart_rate)) {
# Remove heart rates over 200
heart_rate_filtered <- heart_rate[heart_rate <= 200]
end <- min(i + interval*60 - 1, length(heart_rate_filtered))
# Set y-axis range within the loop
plot(data$timestamp[i:end], heart_rate_filtered[i:end],
mar=c(0, 0, 0, 0),
type = "l",
xlab = "Time (sec.)",
ylab = "Hert Rate (bpm)",
#main = paste("Plot Hear Rate per 20 minutes", ceiling(i/(interval*60))),
ylim = c(132, 178))
#scale_y_continuous(limits = c(140, 170), expand = c(0, 0.1))
abline(v = c(start, finish), col = "red", lty = 2)
}
}