Milestone #4

1 1. Import final datasets for creation of visualization, join them, and prepare for visualization

#join all datasets by county
county_data <- calenviroscreen_measures_2021_new1 %>%
  left_join(
    calenviroscreen_scores_demog_2021_new1,
    by = "county"
  ) %>%
  left_join(
    asthma_county,
    by = "county"
  )

#check that there is only one row per county
stopifnot(
  nrow(county_data) == n_distinct(county_data$county)
)

#check for unmatched asthma counties
unmatched_asthma <- anti_join(
  asthma_county,
  calenviroscreen_measures_2021_new1,
  by = "county"
)

#show summary checks
cat("Counties in joined dataset:", nrow(county_data), "\n")
## Counties in joined dataset: 58
cat("Unmatched asthma counties:", nrow(unmatched_asthma), "\n")
## Unmatched asthma counties: 0
#prepare complete observations for scatterplot
plot_data <- county_data %>%
  filter(
    !is.na(meanpm2_5),
    !is.na(ed_rate_total)
  )

#number of counties included in plot
n_plot_counties <- nrow(plot_data)

#set the year of the asthma dataset
asthma_year <- 2020

#show number of counties in plot
cat("Counties included in scatterplot:", n_plot_counties, "\n")
## Counties included in scatterplot: 57

2 2. Interactive County Comparison

The following code creates an interactive table comparing environmental indicators and asthma emergency department (ED) visit rates across California counties. It displays average PM2.5 and ozone concentrations, median CalEnviroScreen 4.0 scores, child and adult asthma ED visit rates, and the ratio between these rates. Users can search, filter, and sort the data by county or indicator, with rate ratios greater than 1 highlighted in red to identify counties where children have higher asthma ED visit rates than adults.

#select elements of interest for the visualizations 
countytable <- county_data %>%
  select(county, meanpm2_5, mean_ozone, median_ces4_0, ed_rate_child, ed_rate_adult, rate_ratio) %>%
  mutate(
    meanpm2_5 = round(meanpm2_5,2),
    mean_ozone = round(mean_ozone,4),
    median_ces4_0 = round(median_ces4_0,2),
    rate_ratio = round(rate_ratio,2)
    )
#use datatable to make a print quality table
tagList(
  datatable(countytable,
          options = list(
            pageLength = 10,
            lengthMenu = c(5,10,25,60),
            order = list(1,'desc')
          ),
          class = "display",
          callback = JS("return table;"),
          rownames = FALSE,
          colnames = c("County", "Mean PM2.5 (&mu;g/m<sup>3</sup>)", "Mean Ozone (ppm)", "Median CES 4.0", "Child ED Rate", "Adult ED Rate", "Rate Ratio"), 
          caption = tags$caption(
                  style = 'caption-side: top; text-align: center; color: black; font-size: 18px; font-weight: bold;',
                  "Asthma ED Visit Rates (2020) and CalEnviroScreen 4.0 Indicators for California Counties"),
          filter = "top",
          escape = FALSE,
          style = "default",
          width = NULL
  ) %>%
  formatStyle(
    columns = "rate_ratio",
    color = styleInterval(cuts = 1.00, values = c("black", "red"))),
  
    tags$p(
    style = "font-size: 0.85em; color: #666; margin-top: 6px;",
    "County air pollution data originate from CalEnviroScreen4.0. ED asthma-related emergency department visit rates are from   California Health and Human Services. The rate ratio is the ratio of child to adult ED visit rates. Measures: PM2.5 (mcg/m^3), ozone (ppm), and CES 4.0 (Cal Enviro Screen Score). ED visit rates are rates (per 10,000 residents) of asthma emergency department (ED). To learn more, visit", tags$a(href = "https://oehha.ca.gov/calenviroscreen/model-scoring", target = "_blank", "OEHHA"), "and", tags$a(href = "https://data.chhs.ca.gov/dataset/asthma-emergency-department-visit-rates", target = "_blank", "CalHHS"),
    ".") 
  )

County air pollution data originate from CalEnviroScreen4.0. ED asthma-related emergency department visit rates are from California Health and Human Services. The rate ratio is the ratio of child to adult ED visit rates. Measures: PM2.5 (mcg/m^3), ozone (ppm), and CES 4.0 (Cal Enviro Screen Score). ED visit rates are rates (per 10,000 residents) of asthma emergency department (ED). To learn more, visit OEHHA and CalHHS .

### Interpretation

The interactive table allows users to compare asthma emergency department visit rates and environmental indicators across California counties. The child-to-adult rate ratio identifies counties where asthma ED visit rates are higher among children than adults, with values greater than 1 indicating higher rates among children.

4 4. PM2.5 and Asthma Emergency Department Visit Rates

This scatterplot examines the relationship between county-level PM2.5 concentrations and all-ages asthma emergency department visit rates.

#scatterplot 


ggplot(
  data = plot_data,
  mapping = aes(
    x = meanpm2_5,
    y = ed_rate_total
  )
) +
  geom_point(
    color = "black",
    size = 2.5,
    alpha = 0.8
  ) +

  # Add a linear trend line
  geom_smooth(
    method = "lm",
    se = FALSE,
    color = "red"
  ) +

  labs(
    title = "PM2.5 and Asthma Emergency Department Visits",

    subtitle = paste0(
      "California Counties — Asthma Data: ",
      asthma_year
    ),

    x = "Mean PM2.5 Concentration (µg/m³)",

    y = "Asthma ED Visit Rate (per 10,000 residents)",

    caption = str_wrap(
      paste0(
        "Sources: CalEnviroScreen 4.0 and CalHHS ",
        "asthma ED data. Each point represents ",
        "one county (n = ", n_plot_counties, "). ",
        "PM2.5 represents the unweighted mean ",
        "of census-tract values. The line shows ",
        "an unadjusted linear trend."
      ),
      width = 95
    )
  ) +

  theme_minimal(base_size = 12) +

  theme(
    plot.title = element_text(
      face = "bold",
      size = 14
    ),

    plot.subtitle = element_text(
      size = 11
    ),

    plot.caption = element_text(
      size = 9,
      hjust = 0
    ),

    panel.grid.minor = element_blank()
  )

4.0.1 Interpretation

The scatterplot compares average PM2.5 concentrations and asthma emergency department visit rates across 57 California counties. The points show considerable variation, with little evidence of a strong linear relationship between PM2.5 concentrations and asthma ED visit rates.

5 5. Limitations

This analysis uses county-level environmental and asthma emergency department data. Because the observations are aggregated by county, relationships observed in the data cannot establish individual-level exposure effects or causation.

The environmental measures represent unweighted averages across census tracts, which may not reflect population-weighted county exposure. The CalEnviroScreen 4.0 release and the asthma reporting year may also represent different underlying observation periods. Finally, missing or suppressed asthma ED rates reduce the number of counties available for some comparisons.