Logo

The Data Daily

Working With Your Fitbit Data in R | R-bloggers

Working With Your Fitbit Data in R | R-bloggers

Matt KayeR-bloggershereclick herehere [This article was first published on , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

is now available on CRAN! You can install it with or you can get the latest dev version with makes it easy to pull your Fitbit data into R and use it for whatever interests you: personal projects, visualization, medical purposes, etc. This post shows how you might use to pull and visualize some of your data.

First, you should either generate a new token with or load a cached token with . library(fitbitr)
library(lubridate)
library(tidyverse)

## Dates to use throughout post
start 23, x - 24, x),
format = scales::comma_format(suffix = ":00", accuracy = 1)
)
) +
labs(x = "Date", y = 'Time') +
theme_fivethirtyeight() +
scale_x_date(date_breaks = '1 month', date_labels = '%b', expand = c(0, 0)) +
facet_grid(. ~ year, space = 'free', scales = 'free_x', switch = 'x') +
theme(panel.spacing.x = unit(0,"line"), strip.placement = "outside") This bit of code makes a nicely formatted plot of the times you went to sleep and woke up over the past three months. You can also use to expand the time window with a little help from (the Fitbit API rate limits you, so you can’t request data for infinitely long windows in a single request). After pulling the data, we can use the same code again to visualize it. sleep %
mutate(
date = as_date(date),
start_time = as_datetime(start_time),
end_time = as_datetime(end_time),
sh = ifelse(hour(start_time) < 8, hour(start_time) + 24, hour(start_time)), #create numeric times
sm = minute(start_time),
st = sh + sm/60,
eh = hour(end_time),
em = minute(end_time),
et = eh + em/60,
mst = rollmean(st, 7, fill = NA), #create moving averages
met = rollmean(et, 7, fill = NA),
year = year(start_time)
) %>%
distinct()

sleep %>%
ggplot(aes(x = date)) +
geom_line(aes(y = et), color = 'coral', alpha = .3, na.rm = T) +
geom_line(aes(y = st), color = 'dodgerblue', alpha = .3, na.rm = T) +
geom_line(aes(y = met), color = 'coral', na.rm = T) +
geom_line(aes(y = mst), color = 'dodgerblue', na.rm = T) +
scale_y_continuous(
breaks = seq(0, 30, 2),
labels = trans_format(
function(x) ifelse(x > 23, x - 24, x),
format = scales::comma_format(suffix = ":00", accuracy = 1)
)
) +
labs(x = "Date", y = 'Time') +
theme_fivethirtyeight() +
scale_x_date(date_breaks = '1 month', date_labels = '%b', expand = c(0, 0)) +
facet_grid(. ~ year, space = 'free', scales = 'free_x', switch = 'x') +
theme(panel.spacing.x = unit(0,"line"), strip.placement = "outside")

You can also pull your heart rate data with . Maybe we’re curious about seeing how the number of minutes spent in the “fat burn,” “cardio,” and “peak” zones correlates with the number of steps taken that day. Let’s find out! First, we can examine the heart rate data: Now, let’s plot them against each other. df %
filter(zone != "Out of Range") %>%
group_by(date) %>%
summarize(total_minutes = sum(minutes_in_zone), .groups = "drop") %>%
inner_join(steps, by = "date")

df %>%
mutate(steps = as.numeric(steps)) %>%
filter(log(total_minutes) > 1) %>%
ggplot(
aes(
steps,
total_minutes
)
) +
geom_point() +
geom_smooth(method = "lm", se = F) +
scale_x_log10() +
scale_y_log10() Or maybe it’d be interesting to predict your zone minutes from your steps:

And that’s it! Hopefully this helped show how makes pulling your data easy, and gets you curious about the insights you can glean from your own data. The Fitbit API gives you access to so much interesting information about yourself, your habits, your fitness, and so much more, and is just meant to be a door into that gold mine.

Images Powered by Shutterstock