Skip to main content

SIDS-Related Deaths in Cook County, IL

Table of contents

  • 1 About
  • 2 Import the Data
  • Exploration
  • 3 Mapping SIDS-related Deaths

View book source

3 Mapping SIDS-related Deaths

Visit “http://danielriggins.com/widgets/cook_county_sids_deaths.html” for a full-screen view.

3.1 Code to produce the map

3.1.1 Load Dependencies

box::use(
    dplyr[
        case_when,
        full_join,
        mutate,
        select
    ],
    leaflet[
        addLayersControl,
        addLegend,
        addPolygons,
        addProviderTiles, 
        leaflet, 
        setMaxBounds, 
        setView
    ],
    leaflet.extras[addFullscreenControl],
    magrittr[`%>%`],
    sf[...],
    tibble[view]
)

3.1.2 Reshape data for use in the map

# Load SIDS death data
df <-
    # Load cached geospatial features
    readRDS("data/coords_and_pop_est.RDS") %>%
    # Join to cached dataframe
    full_join(readRDS("data/df.RDS")) %>%
    # Select ID and outcome variables
    select(fips, count_asphyxia) %>%
    # Turn outcome into an ordinal factor
    mutate(
        death_count = factor(
            case_when(
                count_asphyxia == 0 ~ "No Deaths",
                count_asphyxia == 1 ~ "One Death",
                count_asphyxia == 2 ~ "Two Deaths",
                count_asphyxia == 3 ~ "Three Deaths",
                count_asphyxia == 4 ~ "Four Deaths",
                count_asphyxia == 5 ~ "Five Deaths",
                count_asphyxia == 6 ~ "Six Deaths"
            ),
            ordered = TRUE,
            levels = c(
                "No Deaths", 
                "One Death", 
                "Two Deaths", 
                "Three Deaths", 
                "Four Deaths", 
                "Five Deaths", 
                "Six Deaths"
            )
        )
    )

# Configure color palette
sids_palette <- 
    leaflet::colorFactor(
        palette = "magma",
        reverse = TRUE,
        levels = c(
                "No Deaths", 
                "One Death", 
                "Two Deaths", 
                "Three Deaths", 
                "Four Deaths", 
                "Five Deaths", 
                "Six Deaths"
            )
    )

# Create map widget object
m <- leaflet(df) %>%
    # Use CartoDB's background tiles
    addProviderTiles("CartoDB.Positron") %>%
    # Center and zoom the map to Cook County
    setView(lat = 41.816544, lng = -87.749500, zoom = 9) %>%
    # Add button to enable fullscreen map
    addFullscreenControl() %>%
    # Add census tract polygons colored to reflect the number of deaths
    addPolygons(
        # No borders to the polygons, just fill
        stroke = FALSE,
        # Color according to palette above
        color = ~ sids_palette(death_count),
        # Group polygons by number of deaths for use in the layer control
        group = ~ death_count,
        # Make slightly transparent
        fillOpacity = 0.7,
        # Click on the polygon to get its ID
        popup = ~ paste0("<b>FIPS ID:</b> ", as.character(fips))
    ) %>%
    #Add legend
    addLegend(
        title = "Number of SIDS deaths <br> per census tract",
        values = ~ death_count,
        pal = sids_palette,
        position = "topright"
    ) %>%
    # Add ability to toggle each factor grouping on or off the map
    addLayersControl(overlayGroups = c(
                "No Deaths", 
                "One Death", 
                "Two Deaths", 
                "Three Deaths", 
                "Four Deaths", 
                "Five Deaths", 
                "Six Deaths"
            ),
            position = "topleft"
        )
2 Import the Data

On this page

  • 3 Mapping SIDS-related Deaths
  • 3.1 Code to produce the map
    • 3.1.1 Load Dependencies
    • 3.1.2 Reshape data for use in the map
  • View source
  • Edit this page

"SIDS-Related Deaths in Cook County, IL" was written by Daniel P. Hall Riggins. It was last built on 2022-02-20.

This book was built by the bookdown R package.