knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(AirVizR)

# For custom plots:
library(ggplot2) # For plotting
library(tidyr) # For pivoting
library(dplyr) # For piping
# Loading data if not already present. The 'july_api_*' files are imported with the package.
data_files <- c("data_meta", "data_full", "data_hourly", "data_daily", "data_diurnal")
file_list <- list()
for(i in data_files){file_list <- append(file_list, exists(i))}
if(FALSE %in% file_list){
  data_meta <- july_api_meta
  data_full <- july_api_full
  data_hourly <- july_api_hourly
  data_daily <- july_api_daily
  data_diurnal <- july_api_diurnal
}
remove(data_files, file_list, i)

Loading

Make sure you have your STADs of interest in your environment.
If you want to combine STADs of multiple sources first, read the instructions on the combine-data vignette.

Visualizing diurnal data requires that you have start and end dates defined in the environment; you can run the following to pull the value from your non-averaged data set (defined as data_full in this example):

if (exists("input_startdate") == FALSE) {
  input_startdate <- as.character(min((column_dt(data_full, "date"))$date))
}
if (exists("input_enddate") == FALSE) {
  input_enddate <- as.character(max((column_dt(data_full, "date"))$date))
}

Once you have loaded your STAD, you can run the visualization functions.


Default functions

map_stad() - Map Spatio-Temporal Atmospheric Data

# Most basic heat map
# Note that this will average by location and no other variables.
# Use with caution if data spans over large periods of time.
map_stad(data_hourly, pm25_epa_2021)

# Grouping variables
map_stad(data_hourly, pm25_epa_2021,
         grouping_vars = c("hour_tag", "date_tag"))

# Further customization
map_stad(data_daily, pm25_epa_2021,
         grouping_vars = "date_tag", point_size = 5,
         maptype = "terrain", tint_color = "white", tint_alpha = 0.7)

heatmap_single() - Heatmap of a single monitor of interest

Uses hourly data

# Most basic single heatmap
heatmap_single(pm25_epa_2021, "Rose City")

# Example using a different variable, and further customization
heatmap_single(humidity, "Rose City")

# For a more minimal look, you can hide text labels:
heatmap_single(humidity, "Rose City", data_labels = FALSE)

heatmap_cross() - Heatmap of many monitors

Uses data of any time concentration

# Most basic example
heatmap_cross(data_full, pm25_atm)

# Further customization. You can avoid washing out the color scale by adding a cap.
heatmap_cross(data_full, pm25_atm, cap_value = 75, cap_color = "green")

# Consider adding data labels for visualizations with fewer cell counts:
heatmap_cross(data_daily, temperature_ambient, data_labels = TRUE)

ts_line() - Time series line graph

# Most basic example
ts_line(data_hourly, pm25_epa_2021)

# For a more advanced look, add data points. You can also cap as above.
ts_line(data_hourly, pm25_epa_2021, add_points = TRUE)

# For a cleaner look, hide extrama labels and the rolling average:
ts_line(data_hourly, pm25_epa_2021, add_extrema = FALSE, add_average = FALSE)

# Further customization:
# Filter by site label to spotlight specific monitors without removing others in the set
# Consider plotting on a single column if few monitors are selected,
# for ease of visualizing temporal variation
ts_line(data_hourly, pm25_epa_2021, add_points = TRUE,
        label_filter = c("Lighthouse", "PSU"), single_column = TRUE)

# Using other functions to exclude monitors (including from the background graphics):
filter_df(data_hourly, label, exclude = "Sunset") %>% 
  ts_line(., pm25_epa_2021, add_points = TRUE)

ts_variation() - Time series variation, building upon openair's timeVariation().

# Most basic example
ts_variation(data_hourly, "pm25_epa_2021")

# Multiple pollutants. Remember that LRAPA does not calculate values above 65 µg/m^3
ts_variation(data_hourly, c("pm25_epa_atm", "pm25_lrapa"))

# Grouping, and selecting one plot of interest
ts_variation(data_hourly, "pm25_epa_2021", group = "date_tag", subset = "hour")

Custom plots

To cross-compare calculations, the data can be transformed into tidy format, where it can be visualized with non-specialized functions:

# scatterplot
data_hourly %>% 
  organize_stad(c(pm25_atm, pm25_epa_2021), "pm25") %>% 
  ggplot(aes(x = date_hour, y = pm25, color = measurement)) +
  geom_point(alpha = 0.1, size = 1)

# boxplot
data_daily %>% 
  organize_stad(c(pm25_atm, pm25_epa_2021), "pm25") %>% 
  ggplot(aes(x = as.character(date), y = pm25, color = measurement)) + 
  geom_boxplot()

Further customization

Visualization options can also be applied to combine data sets. See the combine-data vignette for more information!



gmcginnis/AirVizR documentation built on Dec. 20, 2021, 11:49 a.m.