---
title: "AQI-LosAngeles"
author: "Priya"
date: "5/18/2019"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: [ "menu" ]
source: embed
verticle_layout: scroll
---
# Conclusion {.sidebar}
### The Air quality has always been a big issue in Los Angeles due to the high amount of traffic observed here. The Los Angeles county is an area spanning 4084 Square miles making it one of the nations largest counties. The landscape of Los Angeles county is very picturesque and has hills and valleys which surrounds the towering buildings in the downtown. Although, after living here for almost a year I've observed that the hills in the Los Angeles National park which are 9 miles from where I live aren't always visible. Changes in air quality could be the reason why the visibility changes making the hills only visible at certain days. I wanted to find if there were any seasonal patterns observed year after year that could give more insight into the case of the disappearing hills!
### Data Collection: The data was collected for five years from 2014 to 2018 from the United States Environment Protection Agency(https://www.epa.gov/outdoor-air-quality-data). The data has latitude and longitude data to mark various air quality inspection stations arounds the LA county along with the AQI levels for all days of the respective year from 2014-2018. The data also has air quality levels for AQI in the range of 0-50(Good), 51-100(Moderate), 101-150(Unhealthy for ceratin groups), 151-200(unhealthy for all groups) and above 200(unhealthy for all groups).
### Data Cleaning: Necessary packages were loaded and the data was cleaned and transformed into formats necessary to then create various visualizations using the package "dygraphs".
# 2014 AQIs
```{r setup, echo=FALSE}
library(flexdashboard)
library(readxl)
library(dygraphs)
library(xts)
library(tidyquant)
LAcountyAQI2014 <- read_excel("C:/Users/Priya/Desktop/LAcountyAQI.xlsx",
sheet = "2014")
LAcountyAQI2015 <- read_excel("C:/Users/Priya/Desktop/LAcountyAQI.xlsx",
sheet = "2015")
LAcountyAQI2016 <- read_excel("C:/Users/Priya/Desktop/LAcountyAQI.xlsx",
sheet = "2016")
LAcountyAQI2017 <- read_excel("C:/Users/Priya/Desktop/LAcountyAQI.xlsx",
sheet = "2017")
LAcountyAQI2018 <- read_excel("C:/Users/Priya/Desktop/LAcountyAQI.xlsx",
sheet = "2018")
aqs_sites <- read_excel("C:/Users/Priya/Desktop/aqs_sites.xlsx")
##Data merging and cleaning
library(tidyr)
LAcountyAQI20140 <- separate(LAcountyAQI2014, col = "Defining Site", into = c("SC", "CC", "SiteNo", sep = "-"))
LAcountyAQI20150 <- separate(LAcountyAQI2015, col = "Defining Site", into = c("SC", "CC", "SiteNo", sep = "-"))
LAcountyAQI20160 <- separate(LAcountyAQI2016, col = "Defining Site", into = c("SC", "CC", "SiteNo", sep = "-"))
LAcountyAQI20170 <- separate(LAcountyAQI2017, col = "Defining Site", into = c("SC", "CC", "SiteNo", sep = "-"))
LAcountyAQI20180 <- separate(LAcountyAQI2018, col = "Defining Site", into = c("SC", "CC", "SiteNo", sep = "-"))
m2014=merge(LAcountyAQI20140, aqs_sites, by.x = "SiteNo", by.y = "Site Number", all.x = TRUE)
m2015=merge(LAcountyAQI20150, aqs_sites, by.x = "SiteNo", by.y = "Site Number", all.x = TRUE)
m2016=merge(LAcountyAQI20160, aqs_sites, by.x = "SiteNo", by.y = "Site Number", all.x = TRUE)
m2017=merge(LAcountyAQI20170, aqs_sites, by.x = "SiteNo", by.y = "Site Number", all.x = TRUE)
m2018=merge(LAcountyAQI20180, aqs_sites, by.x = "SiteNo", by.y = "Site Number", all.x = TRUE)
# data wrangling and transformation
data2014 <- LAcountyAQI2014[,6]
dates2014 <- seq(as.Date("2014-01-01"), length = 365, by = "days")
data2015 <- LAcountyAQI2015[,6]
dates2015 <- seq(as.Date("2015-01-01"), length = 365, by = "days")
data2016 <- LAcountyAQI2016[,6]
dates2016 <- seq(as.Date("2016-01-01"), length = 366, by = "days")
data2017 <- LAcountyAQI2017[,6]
dates2017 <- seq(as.Date("2017-01-01"), length = 365, by = "days")
data2018 <- LAcountyAQI2018[,6]
dates2018 <- seq(as.Date("2018-01-01"), length = 365, by = "days")
xts2014 <- xts(x = data2014, order.by = dates2014)
xts2015 <- xts(x = data2015, order.by = dates2015)
xts2016 <- xts(x = data2016, order.by = dates2016)
xts2017 <- xts(x = data2017, order.by = dates2017)
xts2018 <- xts(x = data2018, order.by = dates2018)
Comparision <- cbind(xts2014, xts2015, xts2016, xts2017, xts2018)
```
### As shown in the 2014 AQI chart below, the highest AQI of 205 was in the Month of July on July 31 while the lowest AQI of 29 was in the month of December on December 3.
```{r, echo=TRUE}
dygraph(xts2014) %>% dyRangeSelector()
```
# 2015 AQIs
### As shown in the 2015 AQI chart below, the highest AQI of 203 was in the Month of June on June 20 while the lowest AQI of 39 was in the month of December on December 12.
```{r, echo=TRUE}
dygraph(xts2015) %>% dyRangeSelector()
```
# 2016 AQIs
### The highest AQI of 210 was in the Month of June on June 29 while the lowest AQI of 39 was in the month of December on December 16. There has been a huge spike in AQI on January 1st of all years, usually hovering around 150, except 2016 which saw AQI of 101 only.
```{r, echo=TRUE}
dygraph(xts2016) %>% dyRangeSelector()
```
# 2017 AQIs
### As shown in the 2017 AQI chart below, the highest AQI of 224 was in the Month of July on July 7 while the lowest AQI of 34 was in the month of April on April 18. This year had a higher average AQI in the month of December
```{r, echo=TRUE}
dygraph(xts2017) %>% dyRangeSelector()
```
# 2018 AQIs
### The highest AQI of 201 was in the Month of August on August 7 while the lowest AQI of 34 was in the month of October on October 4. This year had a higher average AQI in the month of December
```{r, echo=TRUE}
dygraph(xts2018) %>% dyRangeSelector()
```
# 5 year comparision
### As we do a comparison of all the 5 years we observe the following-
### • As seen from all the five years we can see the trend to be similar with Summer months showing ### highest AQI (June, July, and August) and Winter months of October and December showing the lowest ### (with an outlier of April in 2017).
### • 2017 was the year which saw the widest range of 190 AQI in terms of Highest and lowest AQI.
### • The trend of Summer contributing to highest AQI can be correlated to Summer season attracting more tourists to LA.
```{r, echo=TRUE}
dygraph(Comparision) %>% dyRangeSelector()
```
# further analysis
### This Spatial Map shows the 13 centers from which the data was captured. Clearly the data from Long Beach area and the Santa Clarita areas show the least AQI due to being near the Ocean and Mountains and the highest coming from the downtown LA area.
```{r}
library(sp)
library(leaflet)
a <- as.vector(m2014$Latitude)
b <- as.vector(m2014$Longitude)
a <- a[!is.na(a)]
b <- b[!is.na(b)]
df <- data.frame(longitude = b, latitude = a)
coordinates(df) <- ~longitude+latitude
leaflet(df) %>% addMarkers() %>% addTiles()
```