The Quantified Self assignment overview


A project for ANLY512: Data Visualization

The Quantified Self movement grew from the popularity and growth of the internet of things, the mass collection of personal information, and mobile technologies (primarily wearable computing). This final class project uses a collection of seven months of data on sleep habits and physical activity captured by an activity tracker. In particular, I used a Fitbit Charge 2 which tracks a wide variety of data points related to body, food intake, physical activity, and sleep.

The goal of the project is to collect, analyze and visualize the data using the tools and methods covered in class. Additionally, using the data-driven approach, I will create a summary which answers the following questions 1) how much sleep am I getting 2) how does an abundance or lack of sleep affect my activity level 3) when am I the most active 4) how do ‘Number of Awakenings’ effect my sleep 5) !!!!!!!!!!!!

Data preparation

First 13 data records
Date Calories.Burned Steps Distance Floors Minutes.Sedentary Minutes.Lightly.Active Minutes.Fairly.Active Minutes.Very.Active Activity.Calories Minutes.Asleep Minutes.Awake Number.of.Awakenings Time.in.Bed
1/1/17 3,429 12,219 5.36 16 527 350 48 27 2,104 6.8 30 2 438
1/2/17 2,237 4,407 1.93 10 697 138 7 4 625 9.2 31 4 592
1/3/17 2,256 5,434 2.38 13 864 174 0 0 706 7.1 20 1 448
1/4/17 2,707 8,908 3.91 13 764 248 14 9 1,192 6.3 31 2 411
1/5/17 2,314 5,259 2.31 18 860 194 0 0 792 6.2 16 1 386
1/6/17 2,799 10,043 4.41 11 715 288 12 22 1,415 6.5 9 2 403
1/7/17 2,663 10,315 4.53 6 640 312 6 1 1,294 7.3 42 3 481
1/8/17 2,500 8,404 3.69 11 725 231 6 2 1,002 7.6 20 0 476
1/9/17 2,863 9,626 4.22 17 731 275 16 23 1,371 6.1 31 3 395
1/10/17 2,654 8,454 3.71 17 756 292 0 0 1,230 6.1 26 2 392
1/11/17 2,484 7,575 3.32 7 700 279 0 0 1,072 7.4 15 1 461
1/12/17 2,449 6,531 2.87 14 868 220 4 4 895 5.4 20 2 344
1/13/17 2,489 8,251 3.62 11 771 241 0 0 1,006 6.7 25 1 424

Preparing the data for analysis

When worn, the tracking device displays the number of flights climbed, steps taken, heartrate, and calories burned but only for the current day. But the fitbit website provides tools for exporting tubular data groupd by categories, but only in 31 day increments.

So to begin the data preparation process, I exported the body and sleep documents in comman seperated value (csv) format for each month and combined the files into one spreadsheet in Excel. During this process I also identified which factors were missing but would be needed for analysis. These were 1) converting minute values to hours, 2) summing the activity values into a total column, and 3) extracting month and day of week from “mdy”" date values. All of whichwould be done once the data was imported into R Studio.

The chart here represents the first 13 records of the combined Excel spreadsheet. Once all values were validated for data types, thecontent was imported useing the read.csv command.

Question 1:
How many hours of sleep and activity am I getting?


To answer this question I located the following two facts.

  1. According to a February 2016 report on Morbidity and Mortality, Centers for Disease Control and Prevention’s (CDC) announced that “More than a third of American adults are not getting enough sleep on a regular basis”. The report recommends that adults 18-60 years old should get at least 7 hours of sleep. https://www.cdc.gov/media/releases/2016/p0215-enough-sleep.html

  2. Both the American College of Sports Medicine and the American Heart Association recommend two and half hours a week of moderate intensity exercise.

With these guidelines I set about to create a series of visualizations to illustrate my current sleep and activity levels.

Question 2:
Does an abundance or lack of sleep affect my activity level?


https://plot.ly/ggplot2/

If you use ggplot2, ggplotly() converts your plots to an interactive, web-based version! It also provides sensible tooltips, which assists decoding of values encoded as visual properties in the plot.

plotly supports some chart types that ggplot2 doesn’t (such as 3D surface, point, and line plots). You can create these (or any other plotly) charts using plot_ly().

Question 3


https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps.

While MetricsGraphics.js charts may not have the flexibility of ggplot2

Question 4


https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps.

While MetricsGraphics.js charts may not have the flexibility of ggplot2

Question 5


https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps.

While MetricsGraphics.js charts may not have the flexibility of ggplot2

Summary


https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps.

While MetricsGraphics.js charts may not have the flexibility of ggplot2

---
title: "Fitbit Dashboard"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    source: embed

---
                    

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flexdashboard)
library(knitr)
library(kableExtra)
library(ggplot2)
library(tidyverse)
library(readxl)
library(dplyr)
library(xts)
library(zoo)
library(lubridate) 

fit <- read.csv("~/Documents/CourseWork/Grad Program/Summer 2017/Final Assignment/fitbit_dataset.csv")
fit_rev <- subset(fit, fit$Time.in.Bed > 0)

## combine activity values into a summary column
fit_rev$Total.Activity <- round((fit_rev[,7]+fit_rev[,8]+fit_rev[,9])/60, digits=1)

## convert minutes to hours
fit_rev$Minutes.Asleep <- round(fit_rev[,11]/60, digits=1)


month_levels <- c(
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
)

weekday_levels <- c(
  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
)

## create day of week day column
fit_rev$Weekday <- wday(parse_date_time(fit_rev[,1], 'mdy'), label=TRUE)

## create day of week day column
fit_rev$Month <- month(parse_date_time(fit_rev[,1], 'mdy'), label=TRUE)

options(knitr.table.format = "html") 

```

### The Quantified Self assignment overview

```{r}
ggplot(fit_rev, aes(Date, Minutes.Asleep)) + 
  geom_point(alpha=.5, size = fit_rev$Distance) +
  labs(x = "Jan 1 - Jul 31", y = "Hours", 
       title = "7 months of sleep data",
       subtitle = "A comparison of Hours asleep and walking distance",
       caption = "Exported 8/2/2017 from https://www.fitbit.com") +
  theme(legend.position="none", 
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(), panel.grid.major = element_blank())
```

***
A project for ANLY512: Data Visualization

The Quantified Self movement grew from the popularity and growth of the internet of things, the mass collection of personal information, and mobile technologies (primarily wearable computing). This final class project uses a collection of seven months of data on sleep habits and physical activity captured by an activity tracker. In particular, I used a Fitbit Charge 2 which tracks a wide variety of data points related to body, food intake, physical activity, and sleep. 

The goal of the project is to collect, analyze and visualize the data using the tools and methods covered in class. Additionally, using the data-driven approach, I will create a summary which answers the following questions 1) how much sleep am I getting 2) how does an abundance or lack of sleep affect my activity level 3) when am I the most active 4) how do 'Number of Awakenings' effect my sleep 5) !!!!!!!!!!!!


### Data preparation

```{r}
kable(fit_rev[1:13,1:14], caption="First 13 data records") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))


```

***

Preparing the data for analysis

When worn, the tracking device displays the number of flights climbed, steps taken, heartrate, and calories burned but only for the current day. But the fitbit website provides tools for exporting tubular data groupd by categories, but only in 31 day increments.

So to begin the data preparation process, I exported the body and sleep documents in comman seperated value (csv) format for each month and combined the files into one spreadsheet in Excel. During this process I also identified which factors were missing but would be needed for analysis. These were 1) converting minute values to hours, 2) summing the activity values into a total column, and 3) extracting month and day of week from "mdy"" date values. All of whichwould be done once the data was imported into R Studio.

The chart here represents the first 13 records of the combined Excel spreadsheet. Once all values were validated for data types, thecontent was imported useing the read.csv command.


### Question 1:
How many hours of sleep and activity am I getting? ```{r} fit_bg <- fit_rev[,-16] ggplot(fit_rev, aes(x=Minutes.Asleep, fill=Weekday)) + #geom_histogram(data = fit_bg, fill="grey", binwidth=.3, alpha=.5) + geom_histogram(colour="black", binwidth=.3) + labs(x = "Hours of Sleep", y="") + facet_wrap(~Weekday) + guides(fill=FALSE) + theme_bw() ggplot(fit_rev, aes(x=Total.Activity, fill=Weekday)) + #geom_histogram(data = fit_bg, fill="grey", alpha=.5, binwidth=.3) + geom_histogram(colour="black", binwidth=.3) + labs(x = "Hours of Activity", y="") + facet_wrap(~Weekday) + guides(fill=FALSE) + theme_bw() ``` *** To answer this question I located the following two facts. 1) According to a February 2016 report on Morbidity and Mortality, Centers for Disease Control and Prevention's (CDC) announced that "More than a third of American adults are not getting enough sleep on a regular basis". The report recommends that adults 18-60 years old should get at least 7 hours of sleep. https://www.cdc.gov/media/releases/2016/p0215-enough-sleep.html 2) Both the American College of Sports Medicine and the American Heart Association recommend two and half hours a week of moderate intensity exercise. With these guidelines I set about to create a series of visualizations to illustrate my current sleep and activity levels. ### Question 2:
Does an abundance or lack of sleep affect my activity level? ```{r} fill <- "gold1" line <- "goldenrod2" #fit_rev$Weekday <- factor(fit_rev$Weekday, levels=c("Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")) fit_bg <- fit_rev[,-15] ggplot(data=fit_rev, aes(x=fit_rev$Minutes.Asleep, fill=fit_rev$Weekday)) + geom_histogram(data=fit_bg, fill="grey", binwidth=.25) + geom_histogram(data=fit_rev, colour="black", binwidth=.25) + labs(x = "Hours asleep", y="") + facet_wrap(~Weekday) + guides(fill=FALSE) + theme_bw() ggplot(fit_rev, aes(x = Weekday, y=Minutes.Asleep)) + geom_boxplot(aes(group = Weekday), fill = fill, colour = line) + scale_x_discrete(limits=c("Sun","Mon","Tues","Wed","Thurs","Fri","Sat")) + labs(title = "1.1 Hours asleep per week day", x = "Weekdays", y = "Hours") + theme_minimal() ggplot(fit_rev, aes(x=factor(1), y=Minutes.Asleep, fill=Month)) + geom_bar(aes(group = Month), width = 1, stat = "identity") + coord_polar("y", start=0) + theme_minimal() ggplot(fit_rev, aes(x = Month, y=Minutes.Asleep)) + geom_boxplot(aes(group = Month), fill = fill, colour = line) + scale_x_discrete(limits=c("Jan","Feb","Mar","Apr","May","Jun","Jul")) + labs(title = "1.2 Hours asleep per month", x = "Months", y = "Hours") + theme_minimal() #fit <- fit[1:196,] #fit$Weekday <- factor(fit$Weekday, #levels=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")) #fit_bg <- fit #p <- ggplot(fit, aes(x=fit$Date, y=fit$hrs.asleep, group=fit$Weekday)) + # geom_line(mapping=aes(color=fit$Weekday)) + theme_light() + # theme(axis.ticks = element_blank(), axis.text.x = element_blank()) + # guides(colour = guide_legend("DOW")) + # labs(title = "#1 Hours asleep by Day of Week (DOW)",x = "Jan 1 - July 31", y="Hours asleep") #p library(plotly) p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) + geom_bar(position = "dodge") ggplotly(p) ``` *** https://plot.ly/ggplot2/ If you use ggplot2, `ggplotly()` converts your plots to an interactive, web-based version! It also provides sensible tooltips, which assists decoding of values encoded as visual properties in the plot. plotly supports some chart types that ggplot2 doesn't (such as 3D surface, point, and line plots). You can create these (or any other plotly) charts using `plot_ly()`. ### Question 3 ```{r} #library(metricsgraphics) #mjs_plot(mtcars, x=wt, y=mpg) %>% # mjs_point(color_accessor=carb, size_accessor=carb) %>% # mjs_labs(x="Weight of Car", y="Miles per Gallon") fit3 <- read.csv("~/Documents/CourseWork/Grad Program/Summer 2017/Final Assignment/fitbit_dataset.csv") fit3 <- fit3[c(1,7:9,11)] #fit3$Minutes.Asleep <- round(fit3$Minutes.Asleep/60,1) fit3$Total.Activity <- round((fit3[,2]+fit3[,3]+fit3[,4]), digits=1) fit3$Weekday <- wday(parse_date_time(fit3[,1], 'mdy'), label=TRUE) ggplot(fit3, aes(x = Weekday, y=week(parse_date_time(fit3[,1],'mdy')))) + geom_tile(aes(fill=Minutes.Asleep)) + scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) fit3$zz <- factor(fit3$Minutes.Very.Active) N <- nlevels(fit3$zz) colors <- colorRampPalette(c("#336611", "#99FF33"))(length(unique(fit3$zz))) ggplot(fit3) + geom_tile(aes(x=Weekday, y=week(parse_date_time(fit3[,1],'mdy')), fill=zz)) + scale_fill_manual(values=colors, breaks=levels(fit3$zz)[seq(1, N, by=5)]) + #ggplot(fit3, aes(x = Weekday, y=week(parse_date_time(fit3[,1],'mdy')), fill=Total.Activity, label=Minutes.Asleep-Total.Activity)) + #geom_tile(aes(fill=Minutes.Asleep-Total.Activity)) + #geom_text(size=2) + labs(x = "", y = "31 Weeks") + scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) ggplot(fit3, aes(x = Weekday, y=week(parse_date_time(fit3[,1],'mdy')))) + geom_tile(aes(fill=Minutes.Very.Active)) + labs(x = "", y = "31 Weeks") + scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) ``` *** https://hrbrmstr.github.io/metricsgraphics/ Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps. While MetricsGraphics.js charts may not have the flexibility of ggplot2 ### Question 4 ```{r} library(metricsgraphics) mjs_plot(mtcars, x=wt, y=mpg) %>% mjs_point(color_accessor=carb, size_accessor=carb) %>% mjs_labs(x="Weight of Car", y="Miles per Gallon") ``` *** https://hrbrmstr.github.io/metricsgraphics/ Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps. While MetricsGraphics.js charts may not have the flexibility of ggplot2 ### Question 5 ```{r} library(metricsgraphics) mjs_plot(mtcars, x=wt, y=mpg) %>% mjs_point(color_accessor=carb, size_accessor=carb) %>% mjs_labs(x="Weight of Car", y="Miles per Gallon") ``` *** https://hrbrmstr.github.io/metricsgraphics/ Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps. While MetricsGraphics.js charts may not have the flexibility of ggplot2 ### Summary ```{r} library(metricsgraphics) mjs_plot(mtcars, x=wt, y=mpg) %>% mjs_point(color_accessor=carb, size_accessor=carb) %>% mjs_labs(x="Weight of Car", y="Miles per Gallon") ``` *** https://hrbrmstr.github.io/metricsgraphics/ Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps. While MetricsGraphics.js charts may not have the flexibility of ggplot2