Graph 1. Number of Steps and Sleep Quality by Day of the Month


Graph 1 shows the relationship between number of steps and sleep quality. Overall, for most of the days, I walk between 2500 and 4000 steps per day. My sleep quality varies from 0.6 to 0.8. From the graph, I cannot find any correlation between my number of steps and sleep quality.

Graph 2. Time to Bed by Day of the Week


Graph 2 indicates the trend of time I go to bed every day during the week, across 4 weeks. During weekdays, I always go to bed between 12 am to 1 am. However, I go to sleep late during the weekends, especially on Fridays (after 1 am).

Graph 3. Time to Bed and Sleep Quality


Graph 3 shows the relationship between the time I go to sleep and my sleep quality. There is no clear pattern of the relationship. For most of the nights, my sleep quality is above China average. However, my average sleep quality (0.72) is very close to the average score in China (0.7).

Graph 4. Trend of Time to Bed and Time to Wake up by Week


Graph 4 shows the trends of my time to bed and time to wake up during the day of the week, across 4 weeks. Compared to my time to wake up, my time to sleep seems more stable. During the week, I always go to bed very late on Fridays and Saturdays, and get up late in the morning on Saturdays and Sundays.

Graph 5. Time in Bed and Sleep Quaity


Graph 5 reveals the relationship between my time in bed and sleep quality. Overall, there is no strong correlation between them. However, when my sleep time ranges from 8.5 to 9.2 hours, I have the highest level of sleep quality (around 0.8). Besides, I always have higher quality of sleep during weekends than during weekdays.

---
title: "Quantified Self Project.rmd"
author: "Xin Yuan"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source: embed
    storyboard: true
---


```{r, echo=FALSE}
setwd("C:/Users/Hannah/Desktop/Assignment/ANLY512/QSProject")
sleep<-read.csv("SleepData.csv", header=T)
sleep$Sleep_qua<-as.numeric(sleep$Sleep_qua)
sleep$Week<-as.factor(sleep$Week)
sleep$Bed_time <- as.POSIXct(sleep$Bed_time,format = "%H:%M")
sleep$Time_woke <- as.POSIXct(sleep$Time_woke,format = "%H:%M")
```

### Graph 1. Number of Steps and Sleep Quality by Day of the Month
```{r graph 1}
library(ggplot2)
library(ggthemes)
library(plotly)
library(flexdashboard)

g1 <- ggplot(sleep, aes(x = Day, y = Steps, size = Sleep_qua, fill=factor(Week))) +
      geom_point(shape = 21) +
      theme_bw() +
      theme() +
      ggtitle("Graph 1. Number of Steps and Sleep Quality by Day of the Month") +
      labs(x = "Day of the Month", y = "Number of Steps",
            size = "Sleep Quality", fill = "Week") +
      scale_x_continuous(breaks = seq(0, 28, 7)) +
      scale_size(range = c(1, 6))+
      theme(plot.title = element_text(hjust = 0.5))
g1
```

***

Graph 1 shows the relationship between number of steps and sleep quality. Overall, for most of the days, I walk between 2500 and 4000 steps per day. My sleep quality varies from 0.6 to 0.8. From the graph, I cannot find any correlation between my number of steps and sleep quality. 


### Graph 2. Time to Bed by Day of the Week
```{r graph 2}
#library(scales)
g2<-ggplot(sleep, aes(x = WeekDay, y = Bed_time, group=Week)) +
  geom_line(aes(color=Week))+
  geom_point(aes(color=Week))+
  ggtitle("Graph 2. Time to Bed by Day of the Week") +
  xlab("Day of the Week") + ylab("Time to Bed")+
  theme_light()+
  theme(legend.position="right")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_discrete(limits = c("Sun","Mon","Tue","wed","Thu","Fri","Sat"))
ggplotly(g2)
```

***

Graph 2 indicates the trend of time I go to bed every day during the week, across 4 weeks. During weekdays, I always go to bed between 12 am to 1 am. However, I go to sleep late during the weekends, especially on Fridays (after 1 am).


### Graph 3. Time to Bed and Sleep Quality
```{r graph 3}
g3 <- ggplot(sleep, aes(x = Bed_time, y = Sleep_qua, color=Week)) +
      geom_point(shape = 19,size = 2) +
      theme_bw() +
      theme() +
      ggtitle("Graph 3. Time to Bed and Sleep Quality") +
      labs(x = "Time to Bed", y = "Sleep Quality", fill = "Week") +
      theme(plot.title = element_text(hjust = 0.5))+
      geom_hline(aes(yintercept= 0.7, linetype = "China Average"), color = "blue", size=0.2)+
      scale_linetype_manual(name = "Average", values = 1, 
                      guide = guide_legend(override.aes = list(color = "blue")))
g3
```

***

Graph 3 shows the relationship between the time I go to sleep and my sleep quality. There is no clear pattern of the relationship. For most of the nights, my sleep quality is above China average. However, my average sleep quality (0.72) is very close to the average score in China (0.7). 


### Graph 4. Trend of Time to Bed and Time to Wake up by Week
```{r graph 4}
library("reshape2")
data4<-sleep[, c(2,3,5,8)]
data4_long <- melt(data4, id=c("Week", "WeekDay")) 
data4_long$value <- as.POSIXct(data4_long$value,format = "%H:%M")

data4_long$Week1[data4_long$Week == 1] <- "Week 1"
data4_long$Week1[data4_long$Week == 2] <- "Week 2"
data4_long$Week1[data4_long$Week == 3] <- "Week 3"
data4_long$Week1[data4_long$Week == 4] <- "Week 4"

g4<-ggplot(data4_long, aes(WeekDay, value, group=variable))+
  geom_line(aes(color=variable))+
  geom_point(aes(color=variable))+
  facet_wrap(~Week1, strip.position = "bottom")+  
  ggtitle("Graph 4. Trend of Time to Bed and Time to Wake up by Week") +
  xlab("Day of the Week") + ylab("Time")+
  theme_light()+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_color_manual(name = " ", labels=c("Time to Bed", "Time to Wake up"), values = c("#3182bd","#e6550d"))+
  scale_x_discrete(limits = c("Sun","Mon","Tue","wed","Thu","Fri","Sat"))

g4
```

***

Graph 4 shows the trends of my time to bed and time to wake up during the day of the week, across 4 weeks. Compared to my time to wake up, my time to sleep seems more stable. During the week, I always go to bed very late on Fridays and Saturdays, and get up late in the morning on Saturdays and Sundays.


### Graph 5. Time in Bed and Sleep Quaity
```{r graph 5}

g5<-ggplot(data=sleep, aes(x=Day, y=Time_inbed, color=Sleep_qua)) +
  geom_point(size=3)+
  theme_bw() +
  theme() +
  ggtitle("Graph 5. Time in Bed and Sleep Quality") +
  labs(x = "Day of the Month", y = "Time in Bed") +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_colour_gradient(name="Sleep Quality", low="#fa9fb5", high="#49006a")+
  scale_x_continuous(breaks = seq(0, 28, 1)) +
  scale_y_continuous(breaks = seq(5, 10, 1))+
  geom_vline(xintercept=c(1,7,14,21,28), color = "blue", size=0.2)
ggplotly(g5)
```

***

Graph 5 reveals the relationship between my time in bed and sleep quality. Overall, there is no strong correlation between them. However, when my sleep time ranges from 8.5 to 9.2 hours, I have the highest level of sleep quality (around 0.8). Besides, I always have higher quality of sleep during weekends than during weekdays.