Healthy Cities GIS Assignment

Author

Aline Mayrink

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
setwd("~/Desktop/DATA/Data Visualization 110/DataSets")
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)
head(latlong_clean)
# 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      Unhealthy Beh…
4  2017 CA        California Indio     Census Tract    BRFSS      Health Outcom…
5  2017 CA        California Inglewood Census Tract    BRFSS      Health Outcom…
6  2017 CA        California Lakewood  City            BRFSS      Unhealthy Beh…
# ℹ 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 CA        California Hawthorne Census Tract    Health … 0632548… Arthri…
2  2017 CA        California Hawthorne City            Unhealt… 632548   Curren…
3  2017 CA        California Hayward   City            Unhealt… 633000   Obesit…
4  2017 CA        California Indio     Census Tract    Health … 0636448… Arthri…
5  2017 CA        California Inglewood Census Tract    Health … 0636546… Diagno…
6  2017 CA        California Lakewood  City            Unhealt… 639892   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. Once you run the above code and learn how to filter in this format, filter this dataset however you choose so that you have a subset with no more than 900 observations.

Filter chunk here

subset_prevention <- latlong_clean2 |>
  filter(Category == "Unhealthy Behaviors") |>
  filter(Measure %in% c("Obesity among adults aged >=18 Years", 
                        "Current smoking among adults aged >=18 Years", 
                        "No leisure-time physical activity among adults aged >=18 Years", 
                        "Binge drinking among adults aged >=18 Years")) |>
  slice_head(n = 900) # Only show 900 obs

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

First plot chunk here

unhealthy_by_state <- subset_prevention |>
  group_by(StateDesc, Measure) |>
  summarise(AvgPrevalence = mean(Data_Value, na.rm = TRUE), .groups = "drop")
# Add region info for better visualization
state_regions <- data.frame(
  StateDesc = c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", 
                "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", 
                "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", 
                "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", 
                "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", 
                "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", 
                "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", 
                "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", 
                "Utah", "Vermont", "Virginia", "Washington", "West Virginia", 
                "Wisconsin", "Wyoming"),
  Region = c("South", "West", "West", "South", "West", "West", 
             "Northeast", "South", "South", "South", "West", "West", 
             "Midwest", "Midwest", "Midwest", "Midwest", "South", "South", 
             "Northeast", "South", "Northeast", "Midwest", "Midwest", 
             "South", "Midwest", "West", "Midwest", "West", 
             "Northeast", "Northeast", "West", "Northeast", "South", 
             "Midwest", "Midwest", "South", "West", "Northeast", 
             "Northeast", "South", "Midwest", "South", "South", 
             "West", "Northeast", "South", "West", "South", 
             "Midwest", "West")
)

# Join with unhealthy_by_state 
unhealthy_by_state <- left_join(unhealthy_by_state, state_regions, by = "StateDesc")
ggplot(unhealthy_by_state, aes(x = Region, y = AvgPrevalence, fill = Region)) +
  geom_col(position = "dodge") +
  facet_wrap(~Measure) +
  labs(
    title = "Average Crude Prevalence by Region for Each Unhealthy Behavior",
    x = "Region",
    y = "Average Crude Prevalence (%)",
    fill = "Region",
    caption = "Source: CDC - 500 Cities (2017)"
  ) +
  scale_fill_manual(values = c(
    "South" = "#7538a1", 
    "Northeast" = "#3846a1", 
    "Midwest" = "#29b0d9", 
    "West" = "#2f9c7b" )) +
  theme_bw(base_size = 10) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "none"
  )

3. Now create a map of your subsetted dataset.

First map chunk here

# Load leaflet
library(leaflet)
# Color palette for unhealthy behaviors
colors_behavior <- colorFactor(
  palette = c("#7538a1", "#3846a1", "#29b0d9", "#2f9c7b"), 
  levels = c(
    "Current smoking among adults aged >=18 Years", 
    "Obesity among adults aged >=18 Years", 
    "No leisure-time physical activity among adults aged >=18 Years", 
    "Binge drinking among adults aged >=18 Years"), subset_prevention$Measure
)
leaflet(subset_prevention) |>
  setView(lng = -97, lat = 38, zoom = 4) |> #Had to Google the lat and long
  addProviderTiles("Esri.WorldPhysical") |>
  addCircles(
    lng = ~long,
    lat = ~lat,
    radius = ~Data_Value * 500, #Size of the circles
    weight = 1,
    color = ~colors_behavior(subset_prevention$Measure),
    fillOpacity = 0.6,
)

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

Refined map chunk here

unique(subset_prevention$Measure)
[1] "Current smoking among adults aged >=18 Years"                  
[2] "Obesity among adults aged >=18 Years"                          
[3] "No leisure-time physical activity among adults aged >=18 Years"
[4] "Binge drinking among adults aged >=18 Years"                   
    popupunhealthy = ~paste0(
      "<b>City:</b> ", CityName, "<br>",
      "<b>State:</b> ", StateDesc, "<br>",
      "<b>Measure:</b> ", Measure, "<br>",
      "<b>Value:</b> ", Data_Value, "%" #Add "%" to the value
  )
leaflet(subset_prevention) |>
  setView(lng = -97, lat = 38, zoom = 4) |>
  addProviderTiles("Esri.WorldPhysical") |>
  addCircles(
    lng = ~long,
    lat = ~lat,
    radius = ~Data_Value * 500,
    weight = 1,
    color = ~colors_behavior(subset_prevention$Measure),
    fillOpacity = 0.6,
    popup = popupunhealthy) |>
  
  # Add legend https://rstudio.github.io/leaflet/articles/legends.html#:~:text=Use%20the%20addLegend()%20function,colors%20and%20labels%20for%20you.
  addLegend("bottomright", 
    pal = colors_behavior, 
    values = ~Measure, 
    title = "Unhealthy Behavior",
    opacity = 1
)

5. Write a paragraph

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

In this project, I analyzed data from the CDC’s 500 Cities dataset, focusing on four unhealthy behaviors: smoking, obesity, physical inactivity, and binge drinking. I filtered and cleaned the dataset to include only relevant observations, limiting the data to 900 entries for clarity. Using ggplot2, I created a faceted bar plot that displays the average crude prevalence of each behavior across U.S. regions—South, Northeast, Midwest, and West—revealing that the South consistently shows higher prevalence rates. I also created an interactive map using leaflet, where each city is represented by a circle whose size corresponds to the prevalence and color to the specific behavior. A legend was added to improve readability. These visualizations make it easier to analyze geographic patterns of unhealthy behaviors and raise questions for deeper investigation, such as why certain behaviors are more common in specific regions and what strategies high-prevalence states might be implementing to mitigate these health risks.

Sources:

https://www.cdc.gov/places/about/500-cities-2016-2019/index.html

https://rstudio.github.io/leaflet/articles/legends.html#:~:text=Use%20the%20addLegend()%20function,colors%20and%20labels%20for%20you..