How I Built Animated Plots in R to Analyze my Fitness Data (and you can too!)

You will receive a zip file containing XML objects.

Download and read it into your R console (discussed in the next section).

Android users can also extract their health data but the steps will be a little different.

Follow the steps mentioned in this link export your health data from the Google Fit app.

  Importing the XML Data File into R Once you have downloaded your health data, you need to import it using an R-friendly format.

Use the below code block to read the XML file: Note: Install the ‘XML’ package first from the CRAN repository if you haven’t done so already.

View the code on Gist.

  Pre-processing our Health Data We’ll have to transform a few variables and add new features before we can prepare visualizations and build our dashboard.

  This will help us in easily subsetting and preparing intuitive plots.

We’ll be using the date column to add new columns like years, month, week, hour, etc.

For this, we require the ‘lubridate’ package in R.

I personally love this package – it is very useful when we’re dealing with date and time data.

View the code on Gist.

Now, let’s look at the structure of the cleaned and sorted data: View the code on Gist.

  Output: data.

frame: 422343 obs.

of 14 variables: $ type : Factor w/ 12 levels “HKQuantityTypeIdentifierActiveEnergyBurned”,.

: 9 4 7 7 7 7 7.

$ sourceName : Factor w/ 3 levels “Health”,”Mukeshâ200231s Apple Watch”,.

: 1 1 2 2 2 2 2 2 2.

$ sourceVersion: Factor w/ 22 levels “10.

3.

3″,”11.

0.

1″,.

: 1 1 17 17 17 17 17 17 17 17 .

$ unit : Factor w/ 8 levels “cm”,”count”,”count/min”,.

: 1 5 3 3 3 3 3 3 3 3 .

$ creationDate : Factor w/ 167201 levels “2017-08-18 19:33:16 +0530”,.

: 8 8 2450 2452 2464 2472 .

$ startDate : Factor w/ 361820 levels “2017-08-18 18:30:27 +0530”,.

: 27 27 9642 9647 9655 9664.

$ endDate : POSIXct, format: “2017-08-19 10:34:01” “2017-08-19 10:34:01” “2018-05-01 08:24:12”.

$ value : num 163 79 83 82.

1 83 .

$ device : Factor w/ 102050 levels “<<HKDevice: 0x283c00dc0>, name:Apple Watch, manufacturer:Apple,.

$ month : chr “08” “08” “05” “05” .

$ year : chr “2017” “2017” “2018” “2018” .

$ date : chr “2017-08-19” “2017-08-19” “2018-05-01” “2018-05-01” .

$ dayofweek : Ord.

factor w/ 7 levels “Sunday”<“Monday”<.

: 7 7 3 3 3 3 3 3 3 3 .

$ hour : chr “10” “10” “08” “08” .

Quite a lot of observations for us to begin our analysis.

But what’s the first thing a data scientist should do before anything else?.Yes, it is necessary to set your hypothesis first.

  Drawing up our Hypothesis Our aim here is to use the readily available health-app data to study and answer the below pointers: Average steps, heart-rate, distance covered, flights climbed Which months in each year have been really strenuous in terms of step counts and flights climbed?.Comparative study of the step-counts, calories burned, distance covered from the previous months A weekly breakdown of the step-counts to check for dominant days in a month Are there any outliers?.How does the activity compare on weekdays and weekends?.Does the subject transition to a healthy lifestyle after a certain period?.Any other you can think of?.Let me know and we can add that into the final analysis!.  Exploring the Health Data Let’s look at what kind of observations were stored by the Apple Watch, the fitness band, and the health app: View the code on Gist.

Output: HKQuantityTypeIdentifierActiveEnergyBurned HKQuantityTypeIdentifierAppleExerciseTime 220673 6905 HKQuantityTypeIdentifierBasalEnergyBurned HKQuantityTypeIdentifierBodyMass 47469 1 HKQuantityTypeIdentifierDistanceWalkingRunning HKQuantityTypeIdentifierFlightsClimbed 48447 7822 HKQuantityTypeIdentifierHeartRate HKQuantityTypeIdentifierHeartRateVariabilitySDNN 45371 567 HKQuantityTypeIdentifierHeight HKQuantityTypeIdentifierRestingHeartRate 1 313 HKQuantityTypeIdentifierStepCount HKQuantityTypeIdentifierWalkingHeartRateAverage 44575 199 We have sufficient observations under each type.

We will narrow down our focus to a few important variables in the next section.

But first, begin by installing and importing a few important libraries that will help us in subsetting the data and generating plots: View the code on Gist.

  Preparing our Personalized Fitness Tracker Dashboard The wait is over!.Let’s begin preparing our dashboard to create intuitive and advanced visualizations.

We are primarily interested in the following metrics: Heart rate Steps count Active energy burned Stairs climbed Distance covered Let’s take them up one by one.

  Heart Rate Easily the most critical metric in our dataset.

Let’s look at the heart rate since I started using these fitness tracking devices.

We will group the mean values of the heart rate by date, month and year.

You can do this using the following code block: View the code on Gist.

Output: head(heart_rate) # A tibble: 6 x 4 # Groups: date, year [6] date year month heart_rate <date> <chr> <chr> <dbl> 1 2018-05-01 2018 05 92.

8 2 2018-05-02 2018 05 85.

3 3 2018-05-03 2018 05 86.

0 4 2018-05-04 2018 05 84.

2 5 2018-05-05 2018 05 85.

2 6 2018-05-06 2018 05 93.

5 View the code on Gist.

Having too many dates on the X-axis will look haphazard.

So, scale the X-axis by just representing the month and year: View the code on Gist.

Output: Nice!.This is a good start.

Let’s take things up a notch, though.

Yes, I’m talking about animated plots in R!.You must have come across animated plots on social media.

I certainly can’t scroll by without seeing one or two of those.

How about we create an animated plot ourselves? We’ll be needing the ‘gganimate’ package for this: View the code on Gist.

Output: Looks cool, right?.We got a really “good looking” plot using just one line of code.

Now, we know that the normal resting heart-rate should be somewhere between 60-100 bpm and the normal active heart-rate should be between 100-120 bpm.

There have been times when the heart rate has crossed these boundaries.

It could potentially be that there was some extra activity done on those days.

Could be data points be outliers?. More details

Leave a Reply