---
title: "DataThon 2020 Team Leuven"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
navbar:
- { title: "Leuvenair Project", href: "https://leuvenair.be/", align: left }
source_code: embed
social: ["menu"]
---
```{r setup, include=FALSE}
library(flexdashboard)
library(osmdata) # fetching OpenStreetmap-data
library(leaflet) # display interactive maps
library(stringr) # optional, tidyverse string-operations
library(dplyr) # data-manipulation
library(sf)
library(readxl)
# r opq, include=FALSE}
q.leuven <- opq(bbox = 'Leuven, Belgium') %>% # set bounding box via name
add_osm_feature(key = 'highway') %>% # get streets
add_osm_feature(key = 'name') %>% # include the name
osmdata_sf()
#r our meta data with the location, eval=FALSE, include=FALSE}
text.meta <- read.table("text_meta.txt", header = TRUE, sep = "", dec = ".", na.strings = "-")
meta_full<- text.meta %>% filter(complete.cases(.)) %>% rename(long =LON, lat = LAT) %>% unique()
# Hatem data
hatem <- read_excel("Donald.xlsx", sheet = "Sheet2")
hatem <- hatem %>% rename(SDS011ID = SID)
linked <- merge(meta_full, hatem, by = "SDS011ID")
# saving the street names we have, include=FALSE}
streets_names <- meta_full$STREET # note tried as a list first and it didnt works so do not save it as a list.
# filter streets we want, include=FALSE}
ds <- q.leuven$osm_lines %>% # use only the lines in the osmdata-object
select(name) %>% filter(name %in% streets_names)# keep only the name-variabele (this works imperfectly since the names are at times corrupted)
# keep only the streets I have info from
#map with markers for the traffic, eval=FALSE, include=FALSE}
# step to get the nice icons
getColor <- function(meta_full) {
sapply(meta_full$TRAFFIC, function(TRAFFIC) {
if(TRAFFIC <= 4) {
"green"
} else if(TRAFFIC <= 5) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(meta_full)
)
leaflet(meta_full) %>% addTiles() %>% addAwesomeMarkers(~long, ~lat, icon=icons, label=~as.character(TRAFFIC), group = "Traffic")%>% addMarkers(lng=4.678585, lat=50.864153, popup="Here We Are!")
# map with markers for the Woodstoves, eval=FALSE, include=FALSE}
# step to get the nice icons
getColor_wood <- function(meta_full) {
sapply(meta_full$WOODSTOVES, function(WOODSTOVES) {
if(WOODSTOVES <= 2) {
"green"
} else if(WOODSTOVES <= 5) {
"orange"
} else {
"red"
} })
}
icons_wood <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor_wood(meta_full)
)
# markers for pm
getColor_PM <- function(Mean) {
sapply(linked$Mean, function(Mean) {
if(Mean <= 6.589) {
"green"
} else if(Mean <= 7.509) {
"orange"
} else {
"red"
} })
}
icons_PM <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor_PM(linked)
)
# Other Graphs
library(ggplot2)
library(ggridges)
hatem <- read_excel("Donald.xlsx", sheet = "Sheet2")
hatem <- hatem %>% rename(SDS011ID = SID)
linked <- merge(meta_full, hatem, by = "SDS011ID")
```
Inputs {.sidebar}
-------------------------------------
This dashboard combines traffic data provided by TelRaam and pollution data collected by the sensors installed by the city of Leuven.
We marked the position of the sensors and highlighted the streets that have a traffic monitor in blue.
The Marker takes on different colors depending on what variable you use.
For example selecting "traffic" will give you an idea the amount of traffic in the street.
They turn red when the pollution values are in the top 25% Quantile.
When you roam over the marker you will see what the average pollution was over the last year.
Column {data-width=600}
-----------------------------------------------------------------------
###
```{r map}
leaflet(ds) %>%
addTiles() %>%
addPolylines(
# Shows the value on mouse-over:
label = ~name,
# Highlights the street in red on mouse-over:
highlight = highlightOptions(color = "red"))%>%
addAwesomeMarkers(data = meta_full,~long, ~lat, icon=icons, label=~as.character(TRAFFIC), group = "Traffic")%>%
addAwesomeMarkers(data = meta_full,~long, ~lat, icon=icons_wood, label=~as.character(WOODSTOVES), group = "WOODSTOVES") %>%
addMarkers(lng=4.678585, lat=50.864153, popup="Here We Are!") %>% #
addAwesomeMarkers(data = linked, ~long, ~lat,icon=icons_PM, label=~as.character(Mean), group = "Average PM") %>%
#Adding a control layer
addLayersControl(overlayGroups = c("Traffic", "WOODSTOVES", "Average PM"), options = layersControlOptions(collapsed = FALSE))
```
Column {data-width=400}
-------------------------------------
### Woodstoves
```{r woodstoves}
ggplot(linked, aes(x= Mean, y = as.factor(WOODSTOVES) ,group = WOODSTOVES, alpha = 0.5, point_color = SDS011ID)) + geom_density_ridges(aes(fill = as.factor(WOODSTOVES)), jittered_points = TRUE, position = position_points_jitter(height = 0) )+theme_minimal()+
labs(title = "Average PM per woodstoves", subtitle = 'Density of the levels of PM for all levels of woodstoves from select sensors \nwith complete data (2 years and 2 months).' , caption = 'Data from Leuven Air \n#TeamLeuven') + theme(plot.title = element_text(size=24)) + theme(legend.position = "none") + scale_y_discrete(expand = c(0,0), name = "Woodstoves")+ scale_x_continuous(expand = c(0,0), name = "Average PM")
```
### Traffic
```{r Traffic}
ggplot(linked, aes(x= Mean, y = as.factor(TRAFFIC) ,group = TRAFFIC, alpha = 0.5, point_color = SDS011ID)) + geom_density_ridges(aes(fill = as.factor(TRAFFIC)), jittered_points = TRUE, position = position_points_jitter(height = 0) )+theme_minimal()+
labs(title = "Average PM by Traffic level", subtitle = 'Density of the levels of PM for all levels of Traffic from select sensors \nwith complete data (2 years and 2 months).' , caption = 'Data from Leuven Air \n#TeamLeuven') + theme(plot.title = element_text(size=24)) + theme(legend.position = "none") + scale_y_discrete(expand = c(0,0), name = "Traffic Level")+ scale_x_continuous(expand = c(0,0), name = "Average PM")
```