About

ANLY 512 Data Visualization Project

The Quantified Self

Ashwin Mendhi

I selected the folowing data sets to quantify and visualize my activity

1. Daily Steps Count
Data Collection - iPhone Health App. I used the Quantified Self app tp export the steps data from iPhone Health App. I then added travel and class schedule data from looking at my calender.

2. Macros Data
Data Collection - I used MyFitnessPal app on my phone to log my daily food intake. The app had an option to export this data and analysis purposes.

3. September 2016 Location Data
Data Collection - I downloaded my Google Location history and used this data to plot where I have been in September 2016 - my first month in the US. I wanted to track a more recent month/year but had stopped giving access to Google to track my loction from more than a year now. So selected September 2016 for analysis.

Steps

Row

Travel

HU Classes and Deliverable

Dieting

Row

Text

The charts on this page visualizes the number of steps I have taken in the last 3 to 4 months. I wanted to analyze some of the factors that affect the number of steps that I take each day. I looked at how travelling, HU classes and deliverables, and being on a diet affected my daily steps. From the charts,
1. I took the maximum number of steps on days when I was travelling, followed by
2. Days on which I was on a diet. Being on a diet makes me more active.
3. Lastly, I am least active on days when I either have Harrisburg University classes or have class deliverables due.

Macros

Rows

The charts below show how well I followed the Keto Diet during the first 28 days. As Keto is a high-fat, low-carb diet, my target was to consume a minimum of 55% of my daily calories from fats and a maximum of 10% of my daily calories from carbs. The remaining 35% calories woud come from protein. From these charts, I can see I have done a good job reaching the target for carbs and fats intake for most days. I can see that protien intake has been less than the target for many days. Also, I tend to eat more carbs when I am travelling or eating out.

Row

Carbs (Target = 10%)

Fats (Target = 55%)

Protein (Target = 35%)

Rows

Mood during first 28 days of Keto

Location

Rows

Places I visited during my first month in the United States

Rows

Pittsburgh - Sept 2016

Cleveland - Sept 2016

Row

Places visited in the US in September 2016

---
title: "Quantified Self"
author: "Ashwin Mendhi"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(ggplot2)
library(leaflet)
library(leaflet.extras)
library(ggmap)
```


About
=======================================================================
#### ANLY 512 Data Visualization Project  
#### The Quantified Self  
#### Ashwin Mendhi

I selected the folowing data sets to quantify and visualize my activity  

**1. Daily Steps Count**  
Data Collection - iPhone Health App. I used the Quantified Self app tp export the steps data from iPhone Health App. I then added travel and class schedule data from looking at my calender.  
  
**2. Macros Data**  
Data Collection - I used MyFitnessPal app on my phone to log my daily food intake. The app had an option to export this data and analysis purposes.  
  
**3. September 2016 Location Data**  
Data Collection - I downloaded my Google Location history and used this data to plot where I have been in September 2016 - my first month in the US. I wanted to track a more recent month/year but had stopped giving access to Google to track my loction from more than a year now. So selected September 2016 for analysis.


Steps
=======================================================================

Row {data-height=650}
-----------------------------------------------------------------------

### Travel

```{r}
stepsData <- read_xlsx("StepsData.xlsx", col_types = c("date", "date", "numeric", "numeric",
                                                       "numeric", "numeric", "numeric", "numeric",
                                                       "numeric", "numeric", "numeric", "numeric",
                                                       "guess", "text", "guess", "guess", "text"))
stepsData$Diet <- as.factor(stepsData$Diet)
steps <- stepsData[,c(1,10,12:16)]
steps <- subset(steps, Start >= "2020-07-31")
avgSteps <- mean(steps$`Steps (count)`)

stepsTrvl <- subset(steps, steps$`Travel outside Pittsburgh`== TRUE)
stepsNoTrvl <- subset(steps, steps$`Travel outside Pittsburgh`== FALSE)
avgStepsTrvl <- mean(stepsTrvl$`Steps (count)`)
avgStepsNoTrvl <- mean(stepsNoTrvl$`Steps (count)`)

ggplot(data = steps) + 
  geom_col(data = steps, aes(x=Start, y=`Steps (count)`, fill = `Travel outside Pittsburgh`)) +
  labs(x="Day", y="Steps per day", fill=" " ) + lims(y=c(0,30000)) +
  scale_fill_manual(values = c("grey30", "orange"),
                    breaks=c("FALSE", "TRUE"),
                    labels=c("No Travel Days", "Travel outside Pittsburgh")) +  
  theme_classic() + theme(legend.position = "bottom", text=element_text(size=18)) +
  geom_hline(yintercept=avgStepsNoTrvl, color = "grey30", size = 1.5) + 
  geom_hline(yintercept=avgStepsTrvl, color = "orange", size=2) +
  geom_label(aes(x=Start[1],y=avgStepsNoTrvl,label = round(avgStepsNoTrvl, digits = 0), 
                vjust=-0.5), hjust=-0.01, size=5, color="grey30") +
  geom_label(aes(x=Start[1],y=avgStepsTrvl,label = round(avgStepsTrvl, digits = 0), 
                vjust=-0.5), hjust=-0.01, size=5, color="orange")
```

### HU Classes and Deliverable

```{r}
steps2 <- subset(steps, steps$Start >= "2020-07-31")

stepsClass <- subset(steps2, steps2$Huclass == TRUE | steps2$Hudeliverable == TRUE)
stepsNoClass <- subset(steps2, steps2$Huclass == FALSE & steps2$Hudeliverable == FALSE)
avgStepsClass <- mean(stepsClass$`Steps (count)`)
avgStepsNoClass <- mean(stepsNoClass$`Steps (count)`)

 

ggplot(data = steps2) + 
  geom_col(data = steps2, aes(x=Start, y=`Steps (count)`, fill = Huclass | Hudeliverable)) +
  labs(x="Day", y="Steps per day", fill=" " ) + lims(y=c(0,30000)) +
  scale_fill_manual(values = c("grey30", "#199787"),
                    breaks=c("FALSE", "TRUE"),
                    labels=c("No Class/Deliverable", "HU Class/Deliverable")) + 
  theme_classic() + theme(legend.position = "bottom", text=element_text(size=18)) +
  geom_hline(yintercept=avgStepsNoClass, color = "grey30", size=1.5) + 
  geom_hline(yintercept=avgStepsClass, color = "#199787", size=2) +
  geom_label(aes(x=Start[1],y=avgStepsNoClass,label = round(avgStepsNoClass, digits = 0), 
                vjust=-0.5), hjust=-0.01, size=5, color="grey30") +
  geom_label(aes(x=Start[1],y=avgStepsClass,label = round(avgStepsClass, digits = 0), 
                vjust=-0.5), hjust=-0.01, size=5, color="#199787")
#  geom_point(aes(x=Start,y=Hudeliverable), color = "red")


```


### Dieting

```{r}
stepsData$Diet <- as.factor(stepsData$Diet)
steps3 <- stepsData[,c(1,10,17)]
steps3 <- subset(steps3, steps3$Start >= "2020-05-31")

stepsDiet <- subset(steps3, steps3$Diet == "On a diet")
stepsNoDiet <- subset(steps3, steps3$Diet == "Not on a diet")
avgStepsDiet <- mean(stepsDiet$`Steps (count)`)
avgStepsNoDiet <- mean(stepsNoDiet$`Steps (count)`)


ggplot(data = steps3) + 
  geom_col(data = steps3, aes(x=Start, y=`Steps (count)`, fill = Diet)) +
  labs(x="Day", y="Steps per day", fill=" " ) + lims(y=c(0,30000)) +
  scale_fill_manual(values = c("grey30", "#AA5FFF")) +
  theme_classic() + theme(legend.position = "bottom", text=element_text(size=18)) +
  geom_hline(yintercept=avgStepsNoDiet, color = "grey30", size=1.5) + 
  geom_hline(yintercept=avgStepsDiet, color = "#AA5FFF", size=2) +
  geom_label(aes(x=Start[1],y=avgStepsNoDiet,label = round(avgStepsNoDiet, digits = 0), 
                vjust=1.5), hjust=-0.01, size=5, color="grey30") +
  geom_label(aes(x=Start[1],y=avgStepsDiet,label = round(avgStepsDiet, digits = 0), 
                vjust=-0.5), hjust=-0.01, size=5, color="#AA5FFF")

  
#  geom_point(aes(x=Start,y=Hudeliverable), color = "red")

```

Row {data-height=350}
-----------------------------------------------------------------------
### Text
The charts on this page visualizes the number of steps I have taken in the last 3 to 4 months. I wanted to analyze some of the factors that affect the number of steps that I take each day. I looked at how travelling, HU classes and deliverables, and being on a diet affected my daily steps. From the charts,  
1. I took the maximum number of steps on days when I was travelling, followed by  
2. Days on which I was on a diet. Being on a diet makes me more active.  
3. Lastly, I am least active on days when I either have Harrisburg University classes or have class deliverables due.


Macros
=======================================================================

Rows {data-height=150}
-----------------------------------------------------------------------

#### The charts below show how well I followed the Keto Diet during the first 28 days. As Keto is a high-fat, low-carb diet, my target was to consume a minimum of 55% of my daily calories from fats and a maximum of 10% of my daily calories from carbs. The remaining 35% calories woud come from protein. From these charts, I can see I have done a good job reaching the target for carbs and fats intake for most days. I can see that protien intake has been less than the target for many days. Also, I tend to eat more carbs when I am travelling or eating out.


Row {data-height=650}
-----------------------------------------------------------------------

### Carbs (Target = 10%)

```{r fig.height=7}
macros <- read_xlsx("MacrosData.xlsx")

ggplot(macros, aes(Week, reorder(Day, -Day), fill= Carbs)) + 
  geom_tile() + labs(y="Day") +
  scale_fill_gradient2(low="green", mid="white", high="red", midpoint = 0.1,
                       breaks = c(0.05, 0.1, 0.15),
                       labels = c("5%", "10%", "15%")) +
  theme_minimal() + theme(text = element_text(size = 18))

```

### Fats (Target = 55%)

```{r fig.height=7}

ggplot(macros, aes(Week, reorder(Day, -Day), fill= Fat)) + 
  geom_tile() + labs(y="Day") +
  scale_fill_gradient2(low="red", mid="white", high="green", midpoint = 0.55,
                       breaks = c(0.45, 0.55, 0.65, 0.75),
                       labels = c("45%", "55%", "65%", "75%")) +
  theme_minimal() + theme(text = element_text(size = 18))

```

### Protein (Target = 35%)

```{r fig.height=7}

ggplot(macros, aes(Week, reorder(Day, -Day), fill= Protein)) + 
  geom_tile() + labs(y="Day") +
  scale_fill_gradient2(low="red", mid="white", high="green", midpoint = 0.35,
                       breaks = c(0.15, 0.25, 0.35, 0.45),
                       labels = c("15%", "25%", "35%", "45%")) +
  theme_minimal() + theme(text = element_text(size = 18))

```


Rows {data-height=200}
-----------------------------------------------------------------------

### Mood during first 28 days of Keto

```{r fig.width=12, fig.height=1}

ggplot(macros, aes(Days, Tile, fill= Mood)) + 
  geom_tile(color = "gray") + theme_classic() + labs(x="Day") +
  scale_fill_manual(values = c("#FEA1A1", "#FAFAFA", "#A1FEA4")) +
  geom_text(aes(label=Reason), size=3, angle=45) +
  theme(text = element_text(size = 10),
        axis.title = element_blank(), axis.text.y = element_blank(), 
        axis.line = element_blank(), axis.ticks = element_blank())


```



Location
=======================================================================

Rows {data-height=50}
-----------------------------------------------------------------------

#### Places I visited during my first month in the United States


Rows {data-height=450}
-----------------------------------------------------------------------

```{r}
raw <- jsonlite::fromJSON("2016_SEPTEMBER.json")
locations <- raw$timelineObjects
locations1 <- cbind.data.frame(locations$activitySegment$startLocation$latitudeE7,
                               locations$activitySegment$startLocation$longitudeE7,
                               locations$activitySegment$duration$startTimestampMs)
colnames(locations1) <- c("lat","lon","timestamp") 
locations1$lat <- locations1$lat/10000000
locations1$lon <- locations1$lon/10000000
```


### Pittsburgh - Sept 2016
```{r}

leaflet(locations1) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  setView(-80, 40.44, zoom = 12.6) %>%
  addCircles(opacity = 0.5, weight = 20, color = "#FBB73B")
  #addHeatmap(data = locations1, lng = ~lon, lat = ~lat)
```


### Cleveland - Sept 2016
```{r}

leaflet(locations1) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  setView(-81.63, 41.5, zoom = 12.5) %>%
  addCircles(opacity = 0.5, weight = 20, color = "#453231")
  #addHeatmap(data = locations1, lng = ~lon, lat = ~lat)

```


Row {data-height=500}
-----------------------------------------------------------------------
### Places visited in the US in September 2016
```{r}

leaflet(locations1) %>% 
  addProviderTiles(providers$CartoDB.Positron) %>%
  addMarkers(data = locations1, ~lon, ~lat) #, clusterOptions = markerClusterOptions(), group = "Points") %>%
  #addHeatmap(lng = ~lon, lat = ~lat, group = "HeatMap", blur = 20, max = 0.01, radius = 15) 

```