Edit this page

< back to recipes

The OpenAQ platform gathers and shares open air quality data from around the world. 162 automatic monitoring stations are listed on OpenAQ for the UK with hourly NO2 readings available over the last 90 days. Many of the stations also record levels of coarse and fine particulate matter (PM10 and PM2.5). This recipe will retrieve hourly mean NO2 concentrations from the Manchester Piccadilly monitoring station and plot the results.

Ingredients

Data sources

OpenAQ

R packages

Air quality chart

Instructions

  1. Load the necessary R packages.
library(ropenaq) ; library(ggplot2)
  1. Retrieve all the cities with monitoring stations in the UK.
cities <- aq_cities(country = "GB")
  1. Search for monitoring stations that record NO2 levels in Manchester. Other available pollutants include “pm10” and “pm25” and values for the cityURL variable in the cities dataframe are used for the city name.
locations <- aq_locations(country = "GB", city = "Manchester", parameter = "no2")
  1. Retrieve recent measurements of NO2 from the Manchester Piccadilly monitoring station. If you want readings from a different monitoring station simply change the city and the location using values for the locationURL field in the locations dataframe.
readings <- aq_measurements(country = "GB", 
                            city = "Manchester", 
                            parameter = "no2",
                            location = "Manchester+Piccadilly")
  1. Plot the hourly mean levels of NO2 over the last 90 days and add a horizontal line indicating the legal limit.
ggplot() + 
  geom_line(data = readings, aes(x = dateLocal, y = value), 
            colour = "#c51b8a", size = 0.5) +
  geom_hline(aes(yintercept = 200), color = "#000000", size = 1) +
  annotate(geom = "text", x = as.POSIXct(Sys.Date()-5), y = 200, 
           label = "Legal limit (18 exceedances allowed)", fontface = "bold", vjust = 2) +
  scale_y_continuous(limits = c(0, 210), expand = c(0.005, 0.005)) +
  labs(x = "time (hours)",
       y = expression(paste(mu, "g/",m^3)),
       title = expression(paste("Hourly mean ", NO[2], " concentrations")),
       subtitle = paste("Manchester Piccadilly, last 90 days"),
       caption = "Source: DEFRA | openaq.org") +
  theme_minimal() +
  theme(plot.margin = unit(c(1,1,1,1), "cm"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size = 16, face = "bold"),
        axis.title.x = element_text(hjust = 1),
        axis.title.y = element_text(hjust = 1),
        plot.caption = element_text(size = 9, color = "grey50", hjust = 1, margin = margin(t = 15)))
  1. Output the chart as a PNG file.
ggsave("air_quality.png", dpi = 300)