Summary

row

Summary

In this dashboard, I will use a couple of visualization tool to understand my slepping quality tracked by my Fitbit device.

Data Collection: I’ve exported my sleep tracking data from the Fitbit portal since I bought my device in September 2018. So the data contains the record from 9/22/2018 to 11/29/2019. To be more accurate, I also excluded the short napping hours in the records with duration less than 2 hours.

Methods This dashboard includes three visualizations.

  1. Total Duration in Sleep: Using bar chart to visualize the total duration in sleep everyday. Also, I used stacked bar chart to break down each sleeping phase and show how sleeping time distributed in each phases.

  2. Bedtime in 2019: Using scattered plot to visualize the time I go to bed. The earlier I sleep, the more likely I can guarrantee a better sleeping quality.

  3. YTD Sleeping Efficiency Score: Using line chart and trend line to show the efficiency score for my sleep every day and how sleeping quality progression by date.

Questions we can answer in this dashboard

  1. What is the overall performance of my sleeping quality?

  2. Which time during the year has the lower sleeping quality and what is the reason behind?

  3. How many awaken time during my time in bed?

  4. What time did I go to bed normally and how does it impact sleeping quality?

  5. How sleeping time correlates to the sleeping efficiency?

Conclusion In summary, within the 400 samples, my average sleeping hours are ~7.5 with a relatively high effiency score at 94. It is said that a healthy living for adult should have at least 6-7 hrs per day. My overall performance meets the criteria.

By analyzing the first chart on the left, I am trying to understand how many awaken time during my sleeping period. If awaken time takes over a great portion of the total sleeping hours, this may lead to a lower sleeping quality. It shows that during April 2019, the green area representing awaken time increased. The reason behind is that there were a lot of deliverables at my work as well as the final weeks of my school study, which brought a high pressure in my life and increased awaken time during my time in bed.

On the right hand side, the top chart shows the time that I went to bed everyday. My sleeping time is usually around midnight, but during the summer around July to August, it showed that my bedtime was getting later more frequently than before.

In correlation to the sleeping efficiency, this is also the time that the trend of my sleep efficiency score appearing lower as the blue line shows. Therefore, the bedtime does impact the sleeping quality if I stay up too late.

Number of Records

400

Avg.Sleeping Hours

7.57220833333333

Avg. Sleeping Efficiency

94.3975

Visulization

row

Total Time Duration in Sleep

Time Duration in Each Sleeping Phase

column

Bedtime in 2019

Sleeping Efficiency in 2019

---
title: "Final Projct - The Quantified Self"
author: "Yanqi Ma"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    social: menu
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(readxl)
library(reshape2)
library(lubridate)
```

```{r load data}
slp<-read_xlsx("Master Sleeping Data.xlsx")
# convert minute to hr
slp$timeInBed_hr<-slp$timeInBed/60
# excluding short napping time
slp<-subset(slp,slp$timeInBed_hr>2)
```

Summary
============================================


row
-----------------------------------------------------------------------
### Summary

In this dashboard, I will use a couple of visualization tool to understand my slepping quality tracked by my Fitbit device. 

Data Collection: I've exported my sleep tracking data from the Fitbit portal since I bought my device in September 2018. So the data contains the record from 9/22/2018 to 11/29/2019. To be more accurate, I also excluded the short napping hours in the records with duration less than 2 hours.

Methods
This dashboard includes three visualizations. 

1. Total Duration in Sleep: Using bar chart to visualize the total duration in sleep everyday. Also, I used stacked bar chart to break down each sleeping phase and show how sleeping time distributed in each phases. 

2. Bedtime in 2019: Using scattered plot to visualize the time I go to bed. The earlier I sleep, the more likely I can guarrantee a better sleeping quality.

3. YTD Sleeping Efficiency Score: Using line chart and trend line to show the efficiency score for my sleep every day and how sleeping quality progression by date. 


Questions we can answer in this dashboard

1. What is the overall performance of my sleeping quality?

2. Which time during the year has the lower sleeping quality and what is the reason behind? 

3. How many awaken time during my time in bed? 

4. What time did I go to bed normally and how does it impact sleeping quality? 

5. How sleeping time correlates to the sleeping efficiency? 

Conclusion
In summary, within the 400 samples, my average sleeping hours are ~7.5 with a relatively high effiency score at 94. It is said that a healthy living for adult should have at least 6-7 hrs per day. My overall performance meets the criteria. 

By analyzing the first chart on the left, I am trying to understand how many awaken time during my sleeping period. If awaken time takes over a great portion of the total sleeping hours, this may lead to a lower sleeping quality. It shows that during April 2019, the green area representing awaken time increased. The reason behind is that there were a lot of deliverables at my work as well as the final weeks of my school study, which brought a high pressure in my life and increased awaken time during my time in bed.

On the right hand side, the top chart shows the time that I went to bed everyday. My sleeping time is usually around midnight, but during the summer around July to August, it showed that my bedtime was getting later more frequently than before. 

In correlation to the sleeping efficiency, this is also the time that the trend of my sleep efficiency score appearing lower as the blue line shows. Therefore, the bedtime does impact the sleeping quality if I stay up too late. 


### Number of Records

```{r}
num_re<-nrow(slp)
valueBox(num_re)
```


### Avg.Sleeping Hours

```{r}
avg_slp<-mean(slp$timeInBed_hr)
valueBox(avg_slp)
```

### Avg. Sleeping Efficiency

```{r}
avg_ef<-mean(slp$efficiency)
valueBox(avg_ef)
```




Visulization
============================================


row {.tabset data-width=1000}
-----------------------------------------------------------------------
### Total Time Duration in Sleep

```{r}
ggplot(slp, aes(x = dateOfSleep, y = timeInBed_hr)) + 
  geom_bar(colour="blue",fill="light blue",stat = "identity")+
  labs(title = "Total Time Duration in Sleep",
       subtitle = "Source: Fitbit (9/22/2018 - 11/29/2019)",
       x = "Date",
       y = "Sleeping Hours") +
  theme_light()
```



### Time Duration in Each Sleeping Phase

```{r}
slp_1<-slp[c("logId","dateOfSleep","timeInBed_hr","minutesAsleep","minutesAwake","minutesAfterWakeup")]
slp_1 <- na.omit(melt(slp_1, id=c("dateOfSleep","logId","timeInBed_hr")))
slp_1$value<-slp_1$value/60
ggplot(slp_1, aes(x = dateOfSleep, y = value, fill = variable)) + 
  geom_bar(stat = "identity")+
  labs(title = "Time Duration in Each Sleeping Phase",
       subtitle = "Source: Fitbit (9/22/2018 - 11/29/2019)",
       x = "Date",
       y = "Sleeping Hours") +
  theme_light()
```




column {data-width=500}
-----------------------------------------------------------------------

### Bedtime in 2019

```{r}
slp_2<-slp[c("logId","dateOfSleep","timeInBed","startTime","endTime")]
slp_2$startTime<-strsplit(slp_2$startTime, "T")
slp_2$endTime<-strsplit(slp_2$endTime, "T")
slp_2<-cbind(slp_2,(data.frame(start = sapply(slp_2$startTime, "[", 2), end = sapply(slp_2$endTime, "[", 2))))
slp_2<-subset(slp_2,slp_2$dateOfSleep>"2019-01-01")
slp_2$start<-format(strptime(x = slp_2$start, format = "%H:%M:%S"),"%H:%M:%S")
slp_2$end<-format(strptime(x = slp_2$end, format = "%H:%M:%S"),"%H:%M:%S")
slp_2$start<-as.numeric(sapply(strsplit(slp_2$start,":"), function(a) paste(a[1:2], collapse = ".")))
slp_2$end<-as.numeric(sapply(strsplit(slp_2$end,":"), function(a) paste(a[1:2], collapse = ".")))
ggplot(data=slp_2,aes(x = dateOfSleep, y = start)) +
  geom_point(color="dark green") +
  scale_y_continuous(expand = c(0,0),
                     breaks = seq(0,24,4),
                     limits = c(0,24.1),
                    labels = c("12 AM","4 AM","8 AM", "12 PM", "4 PM","8 PM","11:59 PM")) +
  labs(title = "Bedtime",
       subtitle = "Source: Fitbit (1/1/2019 - 11/29/2019)",
       x = "Date",
       y = "Bedtime") +
  theme_minimal()
```


### Sleeping Efficiency in 2019

```{r}
slp_4<-subset(slp,slp$dateOfSleep>"2019-01-01")

ggplot(slp_4, aes(x=dateOfSleep, y=efficiency,)) + 
  geom_line(color="orange")+
   geom_smooth() +
  labs(title = "YTD Sleeping Efficiency Score",
       subtitle = "Source: Fitbit (1/1/2019 - 11/29/2019)",
       x = "Date",
       y = "Efficiency Score")+
  theme_minimal()
```