This document introduces you to rpollution’s functions, and shows you how to use them with data fetched from OpenWeatherMap API. As these are wrapper functions, usage requires sign up to OpenWeatherMap for free to gain access to an API key.

For more information about API call limits and API care recommendations please visit the OpenWeather how to start page.

Set up

Each function will require you to pass in your generated API key. This can either be read in from a config.json file or you can pass it in directly as a string. To set it up using a config file create a config.json in the root directory and add your API key like the following:

{
  "api_key": "API_KEY_0001"
}

You can then set your API key as a variable using the following code:

my_secrets <- function() {
  path <- "path/to/config/config.json"
  if (!file.exists(path)) {
    stop("Can't find secret file: '", path, "'")
  }
  
  jsonlite::read_json(path)
}

api_key <- my_secrets()$api_key

Load the library

Retrieve historic pollution data with specified date range and location:

This function fetches the historic air pollution levels based on a location. It requires a start and end date and re-formats the data into a more usable data frame with the values of the polluting gases over the specified date range.

Note: Historical data is accessible from 27th November 2020

df <- get_pollution_history(1606488670, 1606747870, 49.28, 123.12, api_key)
head(df)
#>   X         dt main.aqi components.co components.no components.no2
#> 1 1 1606489200        1        208.62             0           2.53
#> 2 2 1606492800        1        208.62             0           2.72
#> 3 3 1606496400        1        208.62             0           2.74
#> 4 4 1606500000        1        208.62             0           2.55
#> 5 5 1606503600        1        208.62             0           2.40
#> 6 6 1606507200        1        210.29             0           2.38
#>   components.o3 components.so2 components.pm2_5 components.pm10 components.nh3
#> 1         60.08           1.13             0.86            1.05           0.60
#> 2         58.65           1.19             0.88            1.05           0.60
#> 3         57.94           1.24             0.87            1.02           0.59
#> 4         57.94           1.22             0.82            0.95           0.55
#> 5         57.94           1.24             0.79            0.92           0.55
#> 6         57.94           1.28             0.78            0.91           0.54

Generate an interactive map containing current pollution data by location:

To visualize air pollution levels at a given location, you can use this function with location coordinates and view the Air Quality Index levels on a world map. This coloured map is interactive and displays the varying regions of air quality.

Additionally, you can add a title to the plot by passing the string as the final argument.

pollution_map_plot <- get_air_pollution(49.28, 123.12, api_key, "Current Air Pollution")
pollution_map_plot

Generate a time-series line chart of forecasted air pollution data:

This function will show you air pollution data for the next 5 days for a specific location. It will return a time series plot for each of the 8 air pollutants and show the varying levels of predicted pollution over time.

forecast_plot <- get_pollution_forecast(49.28, 123.12, api_key)
forecast_plot