Creating timeline charts in R — My fitness activity
Timeline charts are powerful visual tools that display succession of events in chronological order. Several layers of detail can be added to these charts using shapes, labels, colors and shading. Depending on the timeframes, there charts can be designed to look at long term horizons (years) or drill down into an event to provide minute by minute details.
In this article, we will look at how to create timeline charts using R with a sample dataset from Fitbit activity tracking. Fitbit’s web dashboards provide a summary of one’s activity or details of a specific workout session. However, I was more interested in the intraday activity details. Over the course of a week, what are the times when I am active? Are there specific slots where I can be more active? This was the motivation behind my work with the timeline charts.
The finished chart looks like this —
Now, let’s dive into the details
1. Getting the data
The activity history section on the Fitbit’s web page displays duration of activities with their start timestamps. This data can be copied from the webpage into an Excel file. The next step is to import this Excel file into R with the read_xlsx function. This will be stored as a data frame. readxl package is required to call this function.
d2 <- read_xlsx(“C:/TImelineCharts2/activityExport.xlsx”,col_names = TRUE,sheet=2)
2. Data Transformation
The end goal of this step is to get a day of week, start time, end time, calories and activity type from our data frame. lubridate and chron packages are a great in dealing with timestamp conversations. Name of the day can be derived from the ‘date’ field. Time and duration give us the start and end times of activities.
# Changing to appropriate data types
d2$Start_time <- chron(times. = format(strptime(d2$Time,”%I:%M %p”),format = “%H:%M:%S”))d2$cals <- as.numeric(d2$cals)
# Converting time to hours
d2$duration <- hours(d2$Duration)+(minutes(d2$Duration)/60)+(seconds(d2$Duration)/3600)d2$start_time <- hours(d2$Start_time)+(minutes(d2$Start_time)/60)+(seconds(d2$Start_time)/3600)
# End time calculation from start time and duration of activity
d2$end_time <- d2$start_time+d2$duration
3. Generating Timeline charts
We will use ggplot function from ggplot2 package to generate timeline charts. The following functions are used to add layers of details to the chart -
geom_segment : creates each activity block
scale_color_manual : adds colors by activity type; color types have to be provided manually
scale_color_gradient2 : adds color gradient based on calories burnt; more the calories burnt, the darker the shade
geom_label_repel : adds labels to the plot
ggtitle : adds a title to the chart
ggplot(d2) +
geom_segment(aes(x=start_time,xend=end_time, y=Day, yend=Day, color=Activity),size=10)+
scale_color_manual(values=colors)+labs(x = “Time (hrs)”)+
labs(y = “Day of week”) +geom_text_repel(aes(x=((d2$start_time)+(d2$duration/2)),y=d2$Day,label=round((d2$duration*60),digits=0)), box.padding=1, point.padding=1) +
ggtitle(“Workout Activity Timeline”) +
theme(plot.title=element_text(hjust=0.5),text=element_text(size=20)
Timeline charts can be used in a lot of applications like tracking equipment or a process status changes, resource availability & scheduling, project timelines, documenting start and end times of events. The beauty of ggplot2 package is that the code can be easily customized, and more details can be added to the plots.
This article was co-authored with Suraj Thatte
Kudos to @Rucha Jogaikar for contributing to the coding section