Healthy Cities GIS Assignment

Author

Daniel B

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)

setwd("C:/Users/Administrator/Documents/Data110/Datasets")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")

The GeoLocation variable has (lat, long) format

Split GeoLocation (lat, long) into two columns: lat and long

latlong <- tidyr::extract(
  cities500,
  GeoLocation,
  c('lat', 'long'), 
  regex = '\\(([-+]?\\d+\\.\\d+), ([-+]?\\d+\\.\\d+)\\)'
)
head(latlong)
# A tibble: 6 × 25
   Year StateAbbr StateDesc  CityName  GeographicLevel DataSource Category      
  <dbl> <chr>     <chr>      <chr>     <chr>           <chr>      <chr>         
1  2017 CA        California Hawthorne Census Tract    BRFSS      Health Outcom…
2  2017 CA        California Hawthorne City            BRFSS      Unhealthy Beh…
3  2017 CA        California Hayward   City            BRFSS      Health Outcom…
4  2017 CA        California Hayward   City            BRFSS      Unhealthy Beh…
5  2017 CA        California Hemet     City            BRFSS      Prevention    
6  2017 CA        California Indio     Census Tract    BRFSS      Health Outcom…
# ℹ 18 more variables: UniqueID <chr>, Measure <chr>, Data_Value_Unit <chr>,
#   DataValueTypeID <chr>, Data_Value_Type <chr>, Data_Value <dbl>,
#   Low_Confidence_Limit <dbl>, High_Confidence_Limit <dbl>,
#   Data_Value_Footnote_Symbol <chr>, Data_Value_Footnote <chr>,
#   PopulationCount <dbl>, lat <chr>, long <chr>, CategoryID <chr>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

Filter the dataset

Remove the StateDesc that includes the United Sates, select Prevention as the category (of interest), filter for only measuring crude prevalence and select only 2017.

latlong_clean <- latlong |>
  filter(StateDesc != "United States") |>
  filter(Category == "Prevention") |>
  filter(Data_Value_Type == "Crude prevalence") |>
  filter(Year == 2017)

head(latlong_clean)
# A tibble: 6 × 25
   Year StateAbbr StateDesc  CityName   GeographicLevel DataSource Category  
  <dbl> <chr>     <chr>      <chr>      <chr>           <chr>      <chr>     
1  2017 AL        Alabama    Montgomery City            BRFSS      Prevention
2  2017 CA        California Concord    City            BRFSS      Prevention
3  2017 CA        California Concord    City            BRFSS      Prevention
4  2017 CA        California Fontana    City            BRFSS      Prevention
5  2017 CA        California Richmond   Census Tract    BRFSS      Prevention
6  2017 FL        Florida    Davie      Census Tract    BRFSS      Prevention
# ℹ 18 more variables: UniqueID <chr>, Measure <chr>, Data_Value_Unit <chr>,
#   DataValueTypeID <chr>, Data_Value_Type <chr>, Data_Value <dbl>,
#   Low_Confidence_Limit <dbl>, High_Confidence_Limit <dbl>,
#   Data_Value_Footnote_Symbol <chr>, Data_Value_Footnote <chr>,
#   PopulationCount <dbl>, lat <chr>, long <chr>, CategoryID <chr>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

What variables are included? (can any of them be removed?)

names(latlong_clean)
 [1] "Year"                       "StateAbbr"                 
 [3] "StateDesc"                  "CityName"                  
 [5] "GeographicLevel"            "DataSource"                
 [7] "Category"                   "UniqueID"                  
 [9] "Measure"                    "Data_Value_Unit"           
[11] "DataValueTypeID"            "Data_Value_Type"           
[13] "Data_Value"                 "Low_Confidence_Limit"      
[15] "High_Confidence_Limit"      "Data_Value_Footnote_Symbol"
[17] "Data_Value_Footnote"        "PopulationCount"           
[19] "lat"                        "long"                      
[21] "CategoryID"                 "MeasureId"                 
[23] "CityFIPS"                   "TractFIPS"                 
[25] "Short_Question_Text"       

Remove the variables that will not be used in the assignment

prevention <- latlong_clean |>
  select(-DataSource,
         -Data_Value_Unit,
         -DataValueTypeID,
         -Low_Confidence_Limit,
         -High_Confidence_Limit,
         -Data_Value_Footnote_Symbol,
         -Data_Value_Footnote)

head(prevention)
# A tibble: 6 × 18
   Year StateAbbr StateDesc  CityName  GeographicLevel Category UniqueID Measure
  <dbl> <chr>     <chr>      <chr>     <chr>           <chr>    <chr>    <chr>  
1  2017 AL        Alabama    Montgome… City            Prevent… 151000   Choles…
2  2017 CA        California Concord   City            Prevent… 616000   Visits…
3  2017 CA        California Concord   City            Prevent… 616000   Choles…
4  2017 CA        California Fontana   City            Prevent… 624680   Visits…
5  2017 CA        California Richmond  Census Tract    Prevent… 0660620… Choles…
6  2017 FL        Florida    Davie     Census Tract    Prevent… 1216475… Choles…
# ℹ 10 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
#   PopulationCount <dbl>, lat <chr>, long <chr>, CategoryID <chr>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

The new dataset “Prevention” is a manageable dataset now.

For your assignment, work with the cleaned “Prevention” dataset

1. Once you run the above code, filter this dataset one more time for any particular subset.

Filter chunk here

Explore categories

library(dplyr)

# Look at categories in data
categories <- prevention |>
  group_by(CategoryID, MeasureId, Data_Value_Type, Short_Question_Text) |>
  summarize(
    Min_Data_Value = min(Data_Value, na.rm = TRUE),
    Max_Data_Value = max(Data_Value, na.rm = TRUE)
  )

# Print the result
print(categories)
# A tibble: 4 × 6
# Groups:   CategoryID, MeasureId, Data_Value_Type [4]
  CategoryID MeasureId  Data_Value_Type  Short_Question_Text   Min_Data_Value
  <chr>      <chr>      <chr>            <chr>                          <dbl>
1 PREVENT    ACCESS2    Crude prevalence Health Insurance                 2.4
2 PREVENT    BPMED      Crude prevalence Taking BP Medication             9.6
3 PREVENT    CHECKUP    Crude prevalence Annual Checkup                  42.3
4 PREVENT    CHOLSCREEN Crude prevalence Cholesterol Screening           33.9
# ℹ 1 more variable: Max_Data_Value <dbl>

Filter subset

# Filter data
filtered_data <- prevention |>
  filter(StateAbbr == "MD") |>
  filter(GeographicLevel == "Census Tract") |>
  #filter(Short_Question_Text == "Annual Checkup") |>
 
# Change lat/long to numeric
mutate(
  lat = as.numeric(lat),
  long = as.numeric(long),
)

head(filtered_data)
# A tibble: 6 × 18
   Year StateAbbr StateDesc CityName  GeographicLevel Category  UniqueID Measure
  <dbl> <chr>     <chr>     <chr>     <chr>           <chr>     <chr>    <chr>  
1  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Chole…
2  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
3  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
4  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Curre…
5  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Curre…
6  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
# ℹ 10 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
#   PopulationCount <dbl>, lat <dbl>, long <dbl>, CategoryID <chr>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.

library(ggplot2)

# Scatter plot with facet wrap
ggplot(filtered_data, aes(x = PopulationCount,
                          y = Data_Value,
                          color = Short_Question_Text
)) +
  
  geom_point(alpha = 0.05) + 
  scale_color_viridis_d() + 
  geom_jitter() +
  
  facet_wrap(~Short_Question_Text, scales = "free") +
  
  labs(title = "Scatter Plot of Population Count vs. Annual Checkup Data",
       x = "Population Count",  
       y = "Cholesterol Data" 
  ) +
  
  # Modify legend title and add auto trend line
  labs(color = "Test Type") +
  geom_smooth(method = "auto", se = FALSE, color = "red", size = 1) + 
  
  #Theme
  theme_bw()

3. Now create a map of your subsetted dataset.

First map chunk here

library(leaflet)
library(sf)
library(tidyverse)
library(knitr)

# Filter data
filtered_data <- filtered_data |>
  filter(Short_Question_Text == "Annual Checkup")

# Color palette for the heatmap
color_palette <- colorQuantile("YlOrRd", domain = filtered_data$Data_Value)

# Leaflet map
leaflet(data = filtered_data) |>
  setView(lat = mean(filtered_data$lat),
          lng = mean(filtered_data$long),
          zoom = 11) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircleMarkers(data = filtered_data,
                   radius = ~sqrt(Data_Value),
                   color = "#14010d",
                   fill = TRUE,
                   fillColor = ~color_palette(Data_Value),
                   fillOpacity = 0.7)

4. Refine your map to include a mousover tooltip

Refined map chunk here

# Filter data
filtered_data <- filtered_data |>
  filter(Short_Question_Text == "Annual Checkup")

# Color palette for the heatmap
color_palette <- colorQuantile("YlOrRd", domain = filtered_data$Data_Value)

# Pop up
tooltip <- paste0(
  "<b>City FIPS: </b>", filtered_data$CityFIPS, "<br>",
  "<b>Tract FIPS: </b>", filtered_data$TractFIPS, "<br>",
  "<b>City Name: </b>", filtered_data$CityName, "<br>",
  "<b>State: </b>", filtered_data$StateAbbr, "<br>",
  "<br>",
  "<b>Annual Checkup Cholesterol Data: </b>", filtered_data$Data_Value, "<br>"
)

# Leaflet map
leaflet(data = filtered_data) |>
  setView(lat = mean(filtered_data$lat),
          lng = mean(filtered_data$long),
          zoom = 11) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircleMarkers(data = filtered_data,
                   radius = ~sqrt(Data_Value),
                   color = "#14010d",
                   fill = TRUE,
                   fillColor = ~color_palette(Data_Value),
                   fillOpacity = 0.7,
                   popup = tooltip)

5. Write a paragraph

In a paragraph, describe the plots you created and what they show.

  1. Plot: The chart is a scatter plot with a facet wrap to display a plot for each type of checkup or test on cholesterol levels. Charting it on a single graph was difficult due to the difference in the range and scale of cholesterol levels. The cholesterol data is on the y-axis, and population count is on the x-axis. Census tracts were used, but the population of census tracts might be normalized yielding not a very interesting trend. However, it was interesting to observe the range and the clustering of the different types.

  2. Leaflet map: Maryland was filtered along with the ‘annual checkup’ type of test with the colors in the form of a heat map. The darker the hue of red, the higher the cholesterol number. It seems to correlate well with socioeconomic conditions and poverty, which is not surprising, but I’m basing this on my familiarity with the area.

  3. Popup tooltip: A pop-up was added to the above map, which includes the city FIPS and tract FIPS as an ID for that data point, along with the city, state, and the annual checkup cholesterol data, for easy exploration by clicking the data point.

One thing I found challenging was figuring out how to add titles and legends to the leaflet map.