library(tidyverse)
library(scales)
library(janitor)
library(colorspace)
<- tribble(
aqi_levels ~aqi_min, ~aqi_max, ~color, ~level,
0, 50, "#D8EEDA", "Good",
51, 100, "#F1E7D4", "Moderate",
101, 150, "#F8E4D8", "Unhealthy for sensitive groups",
151, 200, "#FEE2E1", "Unhealthy",
201, 300, "#F4E3F7", "Very unhealthy",
301, 400, "#F9D0D4", "Hazardous"
)
AE: Air quality index in Syracuse, NY
Packages
Import and clean Syracuse 2023 data
<- read_csv(file = "data/aqi-syracuse/ad_aqi_tracker_data-2023.csv")
syr_2023
<- syr_2023 |>
syr_2023 clean_names() |>
mutate(date = mdy(date))
syr_2023
Recreate the plot
Basic line chart
Demo: Draw a simple line chart of the AQI in Syracuse for 2023.
# draw the graph
|>
syr_2023 ggplot(aes(x = date, y = aqi_value, group = 1)) +
# plot the AQI in Syracuse
geom_line(linewidth = 1, alpha = 0.5)
Add color shading
Your turn: Add color shading to the plot based on the AQI guide. The color palette does not need to match the specific colors in the table yet.
You can use geom_rect()
to draw rectangles on a plot. Remember the layered grammar of graphics and arrange the geom_*()
in an appropriate order.
# draw the graph
|>
syr_2023 ggplot(aes(x = date, y = aqi_value, group = 1)) +
# shade in background with colors based on AQI guide
geom_rect(
data = TODO,
aes(
ymin = TODO, ymax = TODO,
xmin = TODO, xmax = TODO,
x = TODO, y = TODO, fill = color
)+
) # plot the AQI in Syracuse
geom_line(linewidth = 1, alpha = 0.5)
Use scale_fill_identity()
Your turn: Use the hexidecimal colors from the dataset for the color palette.
# add code here
+
... scale_TODO_TODO()
Label each AQI category on the chart
Your turn: Incorporate text labels for each AQI value directly into the graph. To accomplish this, you need to”
- Calculate the midpoint AQI value for each category
- Add a
geom_text()
layer to the plot with the AQI values positioned at the midpoint on the \(y\)-axis
Extend the range of the \(x\)-axis to provide more horizontal space for the AQI category labels without interfering with the trend line.
<- aqi_levels |>
aqi_levels mutate(aqi_mid = ((aqi_min + aqi_max) / 2))
# add code here
+
... # add text labels for each AQI category
geom_text(
data = TODO,
aes(x = TODO, y = aqi_mid, label = level),
hjust = 1, size = 6, fontface = "bold", color = "white",
family = "Atkinson Hyperlegible"
)
Add labels
Demo: Add a meaningful title, axis labels, caption, etc.
# add code here
+
... # human-readable labels
labs(
x = NULL, y = "AQI",
title = "Ozone and PM2.5 Daily AQI Values",
subtitle = "Syracuse, NY",
caption = "\nSource: EPA Daily Air Quality Tracker"
)
Adjust plot theme components and use theme_minimal()
Your turn: Use theme_minimal()
and get rid of the minor grid lines.
# add code here
Use fit-asp
to ensure rendering is correct
Your turn: Render the plot with an aspect ratio of \(0.618\).
```{r}
#| label: plot-final
#| fig-asp: 0.618
# add code here
```