I was poking around my Fitbit stats earlier today and noticed I had crossed the 20 million mark. What better way to celebrate than learn a new visualization trick!
I’ve been working on a new visualization post that included all my running, activity data, and weight data. I’ve run into a bit of a road block for visualizating the data the way I want to, and while I was poking around I stumbled across the dygraphs for R package. This package brings the nice interactive JavaScript properties to the R envinvornment. Why not take this for a spin and see what we can do with my data.
First we have to get everything up and running.
#install.packages("dygraphs")
Now, I have the privelege for working for Fitabase, the leading research-focused data management platform for wearable device. We help researchers all over the world access data from Fitbit devices for their research projects. A nice side effect of this work is having my Fitbit data alway ready and available to me. Let’s get it ready.
# read in the data
daysteps <- read.csv("~/Downloads/ID 1003_dailySteps_20110227_20170117.csv")
# have to reformat the datetime variable
library(lubridate)
daysteps$ActivityDay <- mdy(daysteps$ActivityDay)
# create an xts time series object for use with dygraph package
library(xts)
daystepsxts <- xts(daysteps$StepTotal, daysteps$ActivityDay)
Now we have to make the plot. Again, the folks at RStudio did a great job putting together the tutorials over at the dygraphs for R page. In the following plot I’ve added some simple axis and data labeling as well as a range selector to the bottom of the plot to allow for panning and zooming.
library(dygraphs)
dygraph(daystepsxts, main = "My 20 Million Steps", ylab="Steps per Day") %>%
dySeries("V1", label = "Steps", color = "#00B0B9") %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyRangeSelector()
I prefer to view my per steps per day visualizations as bar charts. After a bit of searching I found a great answer on StackOverflow (of course) that was a great help. Let’s redo that plot as a bar chart.
library(dygraphs)
dygraph(daystepsxts, main = "My 20 Million Steps", ylab="Steps per Day") %>%
dySeries("V1", label = "Steps", color = "#00B0B9") %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyRangeSelector() %>%
dyOptions(useDataTimezone = TRUE, plotter =
"function barChartPlotter(e) {
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0); // see http://dygraphs.com/jsdoc/symbols/Dygraph.html#toDomYCoord
// This should really be based on the minimum gap
var bar_width = 2/3 * (points[1].canvasx - points[0].canvasx);
ctx.fillStyle = e.color;
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx; // center of the bar
ctx.fillRect(center_x - bar_width / 2, p.canvasy,
bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy,
bar_width, y_bottom - p.canvasy);
}
}")
You’ll nottice that I have a few gaps in the data. In mid-2013 I lost my Fitbit and didn’t replace it until early 2014.