Healthy Cities GIS Assignment

Author

Latifah Traore

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
library(plotly)
setwd("C:/Users/akais/OneDrive/Documents/500 Cities & local heath")
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(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 <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

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 <dbl>, long <dbl>, CategoryID <chr>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>
md <- prevention |>
  filter(StateAbbr=="MD")
head(md)
# 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>
unique(md$CityName)
[1] "Baltimore"

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

For your assignment, work with a cleaned dataset.

1. Once you run the above code, filter this dataset one more time for any particular subset with no more than 900 observations.

Filter chunk here

 subset_data <- prevention %>%
  filter(StateAbbr == "SC" & Short_Question_Text == "Health Insurance") 
 subset_data
# A tibble: 179 × 18
    Year StateAbbr StateDesc  CityName GeographicLevel Category UniqueID Measure
   <dbl> <chr>     <chr>      <chr>    <chr>           <chr>    <chr>    <chr>  
 1  2017 SC        South Car… Rock Hi… Census Tract    Prevent… 4561405… "Curre…
 2  2017 SC        South Car… Charles… Census Tract    Prevent… 4513330… "Curre…
 3  2017 SC        South Car… North C… Census Tract    Prevent… 4550875… "Curre…
 4  2017 SC        South Car… Rock Hi… Census Tract    Prevent… 4561405… "Curre…
 5  2017 SC        South Car… Charles… Census Tract    Prevent… 4513330… "Curre…
 6  2017 SC        South Car… North C… Census Tract    Prevent… 4550875… "Curre…
 7  2017 SC        South Car… Columbia Census Tract    Prevent… 4516000… "Curre…
 8  2017 SC        South Car… Columbia Census Tract    Prevent… 4516000… "Curre…
 9  2017 SC        South Car… Charles… Census Tract    Prevent… 4513330… "Curre…
10  2017 SC        South Car… Columbia City            Prevent… 4516000  "Curre…
# ℹ 169 more rows
# ℹ 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>
# Filtering for census tracts
tract_only <- subset_data %>%
  filter(GeographicLevel == "Census Tract")

# Filtering for cities
city_only <- subset_data %>%
  filter(GeographicLevel == "City")

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

First plot chunk here

health_insurance_plot <- ggplot(tract_only, aes(CityName, Data_Value, color = CityName)) +
  geom_point(shape = 17, size = 3) +
  scale_color_brewer(palette = "Dark2") +
  labs(
    x = "City Name",
    y = "Value (%)",
    title = "Health Insurance Prevalence by City in Maryland (2017)",
    subtitle = "Each point represents the prevalence of health insurance by city in Maryland.",
    color = "City Name"
  ) +
  theme(
    plot.background = element_rect(fill = "lightgrey"),
    panel.background = element_rect(fill = "grey"),
    axis.title = element_text(face = 2),
    legend.background = element_rect(fill = "lightgrey"),
    legend.title = element_text(color = "black", size = 12),
    legend.text = element_text(color = "black", size = 11),
    legend.key.size = unit(0.75, units = "cm"),
    panel.grid = element_line(color = "darkgrey"),
    axis.text.x = element_text(angle = 45, hjust = 1)  # Rotate x-axis text
  )

health_insurance_plot
Warning: Removed 7 rows containing missing values or values outside the scale range
(`geom_point()`).

3. Now create a map of your subsetted dataset.

Loading necessary libraries for the mapping

library(leaflet)
library(sf)
Warning: package 'sf' was built under R version 4.4.2
Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(knitr)

First map chunk here

map_plot <- leaflet() |>
  setView(lat = 33.86, lng = -80.64, zoom = 7) |>
  addProviderTiles("OpenStreetMap") |>
  addCircles(
    data = tract_only,
    radius = sqrt(10^(tract_only$Data_Value/22)) * 5,
    color = "blue"
  )
Assuming "long" and "lat" are longitude and latitude, respectively
map_plot

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

Refined map chunk here

# Define the popup content
map_popup <- paste0( 
  "<b>City: </b>", tract_only$CityName, "<br>",
  "<b>Census Tract: </b>", tract_only$UniqueID, "<br>",
  "<b>Data Value (%): </b>", tract_only$Data_Value, "<br>",
  "<strong>Population: </strong>", tract_only$PopulationCount, "<br>"
)

# Create the map with popups
map_w_popup <- leaflet() |>
  setView(lat = 33.86, lng = -80.64, zoom = 7) |>
  addProviderTiles("OpenStreetMap") |>
  addCircles(
    data = tract_only,
    radius = sqrt(10^(tract_only$Data_Value/23)) * 5,
    color = "#ff7f50",
    popup = map_popup
  )
Assuming "long" and "lat" are longitude and latitude, respectively
map_w_popup

5. Write a paragraph

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

The plots I created display health insurance prevalence data across cities and census tracts in South Carolina for 2017. The scatter plot visualizes the prevalence of health insurance by city, where each point represents a different city, colored by the city name. The map plot offers a geographic representation, with circle sizes proportional to health insurance prevalence, and tooltips provide additional city-specific data when clicked. These visualizations help to see the differences in health insurance rates across the state.