Instructions

Create a web page presentation using R Markdown that features a plot created with Plotly. Host your webpage on either GitHub Pages, RPubs, or NeoCities. Your webpage must contain the date that you created the document, and it must contain a plot created with Plotly. We would love to see you show off your creativity!

Review Criteria

The rubric contains the following two questions:

Does the web page feature a date and is this date less than two months before the date that you’re grading this assignment?

Is the web page a presentation and does it feature an interactive plot that appears to have been created with Plotly?

Nitrogen Dioxide Concentrations for Marylebone Road, London from 1997 to Present

Nitrogen dioxide is a known byproduct from the combustion of fossil fuels. Short term exposure can lead to inflammation of the airways and lead to respiratory infections. Nitrogen dioxide can also cause changes to the environment by changing soil chemistry and biodiversity.

Using DEFRA’s Automatic Urban and Rural Network (AURN) for ambient air quality monitoring, a time series plot of nitrogen dioxide values for the Marylebone Road monitoring station in London is extracted. A moving average is added to enable overall trends to be observed.

Loading appropriate libraries:

library(openair)
library(plotly)
library(lubridate)

First the sites which record nitrogen dioxide are extracted using the meta data:

#extract meta data for the AURN data set
meta_data <- importMeta(source = "aurn", all = TRUE)
#select only nitrogen dioxide measurements for the Marylebone Road monitoring station (MY1)
selected_data <- meta_data %>%
    filter(variable == "NO2") %>% filter(code == "MY1")
#extract the start and end year for measurements
start_year <- year(selected_data$start_date)
end_year <-  year(selected_data$ratified_to)

Averaged monthly data from the whole time series for the Marylebone Road monitoring station is extracted from the AURN database:

data <- importAURN(site = "MY1", year = start_year:end_year, data_type = "monthly", pollutant = "no2")
#tibble is converted to a dataframe to allow plotting and any NA values for nitrogen dioxide are omitted.
data.df <- data %>% as.data.frame() %>% na.omit()

As the data is noisy an n=13 moving average is extracted:

#define moving average filter
ma <- function(x, n){stats::filter(x, rep(1 / n, n), sides = 2)}
#use moving average filter on no2 data in dataframe extracted above
ma_data.df <-  ma(data.df$no2, 13)
#add the moving average data to the original dataframe
data.df <- cbind(data.df, ma_data.df)

Using Plotly the data is plotted:

fig <- plot_ly(data.df, x=data.df$date, y = data.df$no2, type = 'scatter', mode = 'lines', name = "Monthly Values")
m <- list(
    l = 50,
    r = 50,
    b = 100,
    t = 100,
    pad = 0
)
fig <- fig %>% layout(title = list(text ="Nitrogen Dioxide Concentrations for \n Marylebone Road, London (1997-Present)", 
                                   font = list(size =20), y = 0.95, x = 0.5),
                      xaxis =list(title ="Date", showline =T), yaxis =list(title="Concentration / µg/m3", showline = T),
                      legend = list(x=0.9, y =0.98),
                      margin = m) %>%
    add_trace(y=data.df$ma_data.df, line = list(color = "red"), name = "Moving Average n=13")
fig