Heart Rate Trends by 5, 10 and 20-Minutes Intervals, Sub. L08

Kobe Marathon 2023 Nov. 19

Author

Kizen Sasaki

First version

2023 December 17

Modified

2023 December 19

Code
library(tidyverse)
setwd("~/Documents/brevet")
csv_xls <- "~/Documents/brevet/csv_xls/time_hr_leo.csv"
# csv_xls <- "~/Documents/brevet/csv_xls/time_hr_ryu.csv"
data <- read.csv(csv_xls, header = TRUE)
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_rate

Figure 1 KOBE MARATHON 2023, Nov. 19

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)

Figure 1: Heart Rate time plot (9:02:49 - 14:37:35)

KOBE MARATHON 2023, Nov. 19 (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)

  }

Figure 2: Heart rate trends for every 5 minutes

Figure 3: Heart rate trends for every 5 minutes

Figure 4: Heart rate trends for every 5 minutes

Figure 5: Heart rate trends for every 5 minutes

Figure 6: Heart rate trends for every 5 minutes

Figure 7: Heart rate trends for every 5 minutes

Figure 8: Heart rate trends for every 5 minutes

Figure 9: Heart rate trends for every 5 minutes

KOBE MARATHON 2023, Nov. 19 (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)
  }
}

Figure 10: Plot every 5 minutes after 5 minutes the start

Figure 11: Plot every 5 minutes after 5 minutes the start

Figure 12: Plot every 5 minutes after 5 minutes the start

Figure 13: Plot every 5 minutes after 5 minutes the start

Figure 14: Plot every 5 minutes after 5 minutes the start

Figure 15: Plot every 5 minutes after 5 minutes the start

KOBE MARATHON 2023, Nov. 19 (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)
  }
}

Figure 16: Plot every 5 minutes after 5 minutes the start

Figure 17: Plot every 5 minutes after 5 minutes the start

Figure 18: Plot every 5 minutes after 5 minutes the start

Figure 19: Plot 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, 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)
  }
}

Figure 20: Plot every 20 minutes after 5 minutes the start

Figure 21: Plot every 20 minutes after 5 minutes the start

Figure 22: Plot every 20 minutes after 5 minutes the start

Figure 23: Plot every 20 minutes after 5 minutes the start