Healthy Cities GIS Assignment

Author

Jorge Pineda

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")
data(cities500)

The GeoLocation variable has (lat, long) format

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

latlong <- cities500|>
  mutate(GeoLocation = str_replace_all(GeoLocation, "[()]", ""))|>
  separate(GeoLocation, into = c("lat", "long"), sep = ",", convert = TRUE)
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 <dbl>, long <dbl>, 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(Data_Value_Type == "Crude prevalence") |>
  filter(Year == 2017) |>
  filter(StateAbbr == "CT") |>
  filter(Category == "Unhealthy Behaviors")
head(latlong_clean)
# A tibble: 6 × 25
   Year StateAbbr StateDesc   CityName   GeographicLevel DataSource Category    
  <dbl> <chr>     <chr>       <chr>      <chr>           <chr>      <chr>       
1  2017 CT        Connecticut Bridgeport Census Tract    BRFSS      Unhealthy B…
2  2017 CT        Connecticut Danbury    City            BRFSS      Unhealthy B…
3  2017 CT        Connecticut Norwalk    Census Tract    BRFSS      Unhealthy B…
4  2017 CT        Connecticut Bridgeport Census Tract    BRFSS      Unhealthy B…
5  2017 CT        Connecticut Hartford   Census Tract    BRFSS      Unhealthy B…
6  2017 CT        Connecticut Waterbury  Census Tract    BRFSS      Unhealthy B…
# ℹ 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 <dbl>, long <dbl>, 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

latlong_clean2 <- latlong_clean |>
  select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote)
head(latlong_clean2)
# A tibble: 6 × 18
   Year StateAbbr StateDesc   CityName GeographicLevel Category UniqueID Measure
  <dbl> <chr>     <chr>       <chr>    <chr>           <chr>    <chr>    <chr>  
1  2017 CT        Connecticut Bridgep… Census Tract    Unhealt… 0908000… Obesit…
2  2017 CT        Connecticut Danbury  City            Unhealt… 918430   Obesit…
3  2017 CT        Connecticut Norwalk  Census Tract    Unhealt… 0955990… Obesit…
4  2017 CT        Connecticut Bridgep… Census Tract    Unhealt… 0908000… Curren…
5  2017 CT        Connecticut Hartford Census Tract    Unhealt… 0937000… Obesit…
6  2017 CT        Connecticut Waterbu… Census Tract    Unhealt… 0980000… Obesit…
# ℹ 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>

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

For your assignment, work with a cleaned dataset.

1. Continued Filtering chunks and other libraries loading (<900)

To reduce the dataset to under 900 observations, I focused on: - A single health measure: Obesity among adults aged 18 or older - Excluded rows where GeographicLevel == "City" (only 32 rows), to focus on Census Tract data.

library(leaflet) #for interactive maps
# unique(latlong_clean2$GeographicLevel)
# table(latlong_clean2$GeographicLevel)

# I removed rows where GeographicLevel == "City" (only 32 observations),
# so we could focus on CensusTract-level granularity
latlong_filtered <- latlong_clean2 %>%
  filter(Measure == "Obesity among adults aged >=18 Years") %>%
  filter(GeographicLevel != "City") # removes 32 city-wide rows

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

First plot chunk here

# non map plot
ggplot(latlong_filtered, aes(x = Data_Value)) +
  geom_histogram(fill = "#69b3a2", color = "white", bins = 20) +
  labs(
    title = "Distribution of Obesity Rates",
    x = "Obesity Percentage",
    y = "Number of Census Tracts"
  ) +
  theme_minimal()

This histogram shows the distribution of obesity percentages across census tracts in the dataset. While most tracts fall between 20% and 40% obesity, there is considerable variation. The data appears slightly right-skewed, suggesting a number of tracts with unusually high rates. This baseline distribution helps contextualize the geographic disparities explored in the next steps.

Creating a map of subsetted dataset.

First map chunk here

# leaflet()
leaflet(latlong_filtered) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(lng = -73, lat = 41.4, zoom = 8) %>%
  addCircleMarkers(
    lng = ~long, lat = ~lat,
    radius = 3,
    fillColor = "steelblue",
    color = "steelblue",
    fillOpacity = 0.7,
    stroke = FALSE
  )

Simple Leaflet Map of Obesity Rates in Connecticut

This map displays each census tract in the filtered dataset using leaflet(). The basemap style "CartoDB.Positron" was chosen for its clean, minimal look. The map is centered over Connecticut using approximate state-level coordinates: longitude -73 and latitude 41.4. These values place the map around the geographic center of Connecticut, where all the data points are located. The zoom level of 8 gives a full view of the state while keeping tract-level detail visible.

Each circle represents one census tract and is styled with a fixed size and a steel-blue color. In Step 4, this map will be made interactive with additional tooltips.

pal <- colorNumeric(palette = "YlOrRd", domain = latlong_filtered$Data_Value)

leaflet(latlong_filtered) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(lng = -73, lat = 41.4, zoom = 8) %>%
  addCircleMarkers(
    lng = ~long, lat = ~lat,
    color = ~pal(Data_Value),
    radius = 3,
    fillOpacity = 0.8,
    stroke = FALSE
  ) %>%
  addLegend(
    "bottomright",
    pal = pal,
    values = ~Data_Value,
    title = "Obesity %",
    opacity = 1
  )

A more informative map that visualizes obesity rates across census tracts in Connecticut. Each circle is color-coded based on obesity percentage using a yellow-to-red gradient (YlOrRd), with darker red indicating higher rates. The legend clarifies the meaning of the colors. This visualization allows patterns and clusters to be seen clearly, even without interaction. In the next step, interactivity will be added to reveal specifi

4. Refine your map to include a mouse-click tooltip

Refined map chunk with mouse-click tooltip

popup_text <- paste0(
  "<b>City: </b>", latlong_filtered$CityName, "<br>",
  "<b>State: </b>", latlong_filtered$StateAbbr, "<br>",
  "<b>Obesity Rate: </b>", round(latlong_filtered$Data_Value, 1), "%<br>",
  "<b>Population: </b>", latlong_filtered$PopulationCount
)
leaflet(latlong_filtered) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(lng = -73, lat = 41.4, zoom = 8) %>%
  addCircleMarkers(
    lng = ~long, lat = ~lat,
    color = ~pal(Data_Value),
    radius = 4,
    fillOpacity = 0.8,
    stroke = FALSE,
    popup = popup_text
  ) %>%
  addLegend(
    "bottomright",
    pal = pal,
    values = ~Data_Value,
    title = "Obesity %",
    opacity = 1
  )

Adding Mouse-Click Popups

This version of the map allows users to click on each tract to view detailed information. The popup displays the city, state, obesity percentage, and population count for that location. This refinement adds valuable context to the visual data and makes the map more informative and interactive.

Following is a the same visualization with the radius scale based on population count. This allows to highlight where the largest number of people are affected if manually zoomed in. The square root is used to moderate the range of population values, and dividing by 10 ensures that the marker sizes remain visually balanced.

leaflet(latlong_filtered) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(lng = -73, lat = 41.4, zoom = 8) %>%
  addCircleMarkers(
    lng = ~long, lat = ~lat,
    color = ~pal(Data_Value),
    radius = ~sqrt(PopulationCount) / 10,
    fillOpacity = 0.8,
    stroke = FALSE,
    popup = popup_text
  ) %>%
  addLegend(
    "bottomright",
    pal = pal,
    values = ~Data_Value,
    title = "Obesity %",
    opacity = 1
  )

5. Interpretation

This interactive map shows the distribution of obesity percentages across census tracts in Connecticut. Each circle represents a tract and is color-coded from yellow to red based on its obesity rate, with darker red indicating higher percentages. The size of each marker reflects the population size, highlighting where more people may be affected by higher obesity rates. Most tracts fall within the 25%–35% range, but some stand out with values exceeding 40%. The popups reveal specific details like city name, obesity rate and population count, helping to ground the data in recognizable locations. Overall, the map shows that while obesity is a widespread issue across the state, there are clear geographic concentrations where prevalence is noticeably higher.