Introduction

Hello and welcome back to PHW251B: Data Visualization for Public Health! This RMarkdown document will serve as your third coding assignment this semester. In this assignment, we’re building directly off of the Module #10: Interactive Plots and creating our own dynamic charts and tables. It will be helpful to have worked through that Module before beginning this assignment!

Submission Instructions

Knit the file to HTML, and then submit it to bCourses.

KNITTING ERROR WORKAROUNDS

If you experience any knitting errors with this assignment please do the following -

Step 1 -

  • Make sure that YAML settings are configured properly with following parameters output: html_document: default self_contained: true df_print: paged
    pandoc_args: [“–to=html5”]
  • Make sure that you installed all packages needed and that the package version for ggplot2 is ‘3.5.1’ and package version for ggplot2 is ‘3.5.1’ package version for plotly is ‘4.10.4’

Step 2 -

  • In the code chunks that prompt the errors -> set echo=TRUE, eval=FALSE. This is going to display your code without rendering your visualization (this is the important piece for grading!) as we learned in Week 1 of the course. Upload this knitted HTML version.
  • Take a screen shot of the plot(s) that you created that did not render and upload those along with your HTML to Assignment 3 (more than 1 upload are permitted)

Rubric

See the rubric for in bCourses Assignments Sidebar Tab -> Assignment 3.

The Data

Today, we’ll be revisiting CalEnviroScreen (CES) 4.0, which is a composite dataset with a host of environmental exposure and health outcome markers produced and maintained by the Office of Environmental Health Hazards Assessment (OEHHA), a branch of California’s EPA. It’s a relevant dataset that has a continuing influence on how health equity programs are funded and distributed!

You can read more about CES here: CES 4.0 website

CES 4.0 is available as both a tabular spreadsheet (.xlsx) and a geospatial shapefile (.shp). Today, we will be using the former, but do feel free to go to the linked website above to download the shapefile for your own practice/exploration.

There are too many variables to list out descriptions in this .rmd, but we have converted the data dictionary for your viewing.

Run Package Updates and Versions

Install the following packages (IMPORTANT for “ggplot2” & “plotly”)

# Update ggplot2 & update plotly
install.packages("ggplot2")
install.packages("plotly")
install.packages("reactable")
install.packages("openxlsx")
## Warning in install.packages("openxlsx"): installation of package 'openxlsx' had
## non-zero exit status
install.packages("ggiraph")

Then check their versions “ggplot2” & “plotly”

# Check package versions (Check package versions)
packageVersion("ggplot2") # should be ‘3.5.1’
## [1] '4.0.3'
packageVersion("plotly") # should be ‘4.10.4’
## [1] '4.12.1'

Run Knit Options

Load Packages

The Assignment

Let’s dive in! Today we’ll be asking you to construct an interactive plot/graph and an interactive table using the CES dataset.

Question #1

Please construct a boxplot that depicts the distribution of the Low.Birth.Weight variable across Bay Area counties using ggplotly, plotly, or ggiraph. You’ll want to filter the dataset for the nine Bay Area counties first, and then construct your plot. Feel free to add any visual modification that you deem necessary to make it a publication-ready graphic.

# Some libraries that you may need for convenience (you may add more)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
bay_area_counties <- c("Alameda", "Contra Costa", "Marin", "Napa", "San Francisco", "San Mateo", "Santa Clara", "Solano", "Sonoma")


bay_area_counties <- c("Alameda", "Contra Costa", "Marin", "Napa",
                       "San Francisco", "San Mateo", "Santa Clara",
                       "Solano", "Sonoma")


bay_area_data <- ces_data %>%
  filter(california_county %in% bay_area_counties)


q1_plot <- ggplot(bay_area_data,
                  aes(x = california_county,
                      y = low_birth_weight,
                      fill = california_county)) +
  geom_boxplot() +
  labs(
    title = "Distribution of Low Birth Weight Across Bay Area Counties",
    x = "County",
    y = "Low Birth Weight"
  ) +
  theme_minimal() +
  theme(legend.position = "none",
    axis.text.x = element_text(angle = 45, hjust = 1),
    plot.title = element_text(face = "bold"))

ggplotly(q1_plot)

Question #2

Next, let’s make an interactive table that lets a user view the CES dataset on their own. Construct the table using DT or reactable.

The entire dataset is likely too large to explore as a table, but let’s first summarize the following variables at the County level to simplify the data:

Population-weighted averages for: - CES 4.0 Score - PM2.5 - Lead - Traffic - Pollution Burden - Asthma - Cardiovascular Disease - Housing Burden

Note: see the weighted.mean function for weighted averages

We’d also like you to round all columns, except for the County name, to 2 decimal points, bold the County column, and turn off rownames.

# Some libraries that you may need for convenience (you may add more)
library(DT)
library(reactable)


# Some libraries that you may need for convenience
library(DT)
library(reactable)


county_summary <- ces_data %>%
  group_by(california_county) %>%
  summarise(ces_4_0_score = weighted.mean(ces_4_0_score, total_population, 
                                          na.rm = TRUE),
    pm2_5 = weighted.mean(pm2_5, total_population, na.rm = TRUE),
    lead = weighted.mean(lead, total_population, na.rm = TRUE),
    traffic = weighted.mean(traffic, total_population, na.rm = TRUE),
    pollution_burden = weighted.mean(pollution_burden, total_population, 
                                     na.rm = TRUE),
    asthma = weighted.mean(asthma, total_population, na.rm = TRUE),
    cardiovascular_disease = weighted.mean(cardiovascular_disease, 
                                           total_population, na.rm = TRUE),
    housing_burden = weighted.mean(housing_burden, total_population, 
                                   na.rm = TRUE),
    .groups = "drop") %>%
  mutate(across(where(is.numeric), ~ round(.x, 2)))


reactable(
  county_summary,
  columns = list(
    california_county = colDef(
      name = "County",
      style = list(fontWeight = "bold")
    ),
    ces_4_0_score = colDef(name = "CES 4.0 Score"),
    pm2_5 = colDef(name = "PM2.5"),
    lead = colDef(name = "Lead"),
    traffic = colDef(name = "Traffic"),
    pollution_burden = colDef(name = "Pollution Burden"),
    asthma = colDef(name = "Asthma"),
    cardiovascular_disease = colDef(name = "Cardiovascular Disease"),
    housing_burden = colDef(name = "Housing Burden")),
  rownames = FALSE,
  searchable = TRUE,
  filterable = TRUE,
  striped = TRUE,
  highlight = TRUE,
  bordered = TRUE)

And that brings us to the end of Assignment 3 – congrats! Please knit to HTML and submit it to bCourses whenever you are done.