Healthy Cities GIS Assignment

Author

Jude E. Abban

Load the libraries and set the working directory

library(tidyverse)
library(highcharter)
library(tidyr)
library(leaflet)
setwd('/Users/eabban/College Stuff/R Studio')
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(Measure == ">=18") |>
  filter(StateAbbr == "CT") |>
  filter(Category == "Unhealthy Behaviors")
head(latlong_clean)
# A tibble: 0 × 25
# ℹ 25 variables: Year <dbl>, StateAbbr <chr>, StateDesc <chr>, CityName <chr>,
#   GeographicLevel <chr>, DataSource <chr>, Category <chr>, 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>, …

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: 0 × 18
# ℹ 18 variables: Year <dbl>, StateAbbr <chr>, StateDesc <chr>, CityName <chr>,
#   GeographicLevel <chr>, Category <chr>, UniqueID <chr>, Measure <chr>,
#   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 “latlong_clean2” is a manageable dataset now.

For your assignment, work with a cleaned dataset where you perform your own cleaning and filtering.

1. Once you run the above code and filter this complicated dataset, perform your own investigation by filtering this dataset however you choose so that you have a subset with no more than 900 observations through some inclusion/exclusion criteria.

Filter chunk here (you may need multiple chunks)

my_clean <- latlong |>
  filter(StateDesc != "United States") |>
  select(
    Year, StateAbbr, CityName, GeographicLevel,
    Category, Measure, Short_Question_Text,
    Data_Value, Low_Confidence_Limit, High_Confidence_Limit,
    PopulationCount, lat, long,
    MeasureId, CityFIPS, TractFIPS
  ) |>
  filter(str_detect(Measure, ">=18")) |>
  filter(!is.na(TractFIPS), !is.na(Data_Value), !is.na(PopulationCount), !is.na(lat), !is.na(long)) |>
  slice_sample(n = 900)

head(my_clean)
# A tibble: 6 × 16
   Year StateAbbr CityName  GeographicLevel Category Measure Short_Question_Text
  <dbl> <chr>     <chr>     <chr>           <chr>    <chr>   <chr>              
1  2017 MI        Detroit   Census Tract    Health … High c… High Cholesterol   
2  2017 NY        New York  Census Tract    Health … Stroke… Stroke             
3  2017 NY        New York  Census Tract    Health … Cancer… Cancer (except ski…
4  2017 NY        New York  Census Tract    Health … Mental… Mental Health      
5  2017 OH        Cincinna… Census Tract    Health … Diagno… Diabetes           
6  2017 CA        Moreno V… Census Tract    Health … Stroke… Stroke             
# ℹ 9 more variables: Data_Value <dbl>, Low_Confidence_Limit <dbl>,
#   High_Confidence_Limit <dbl>, PopulationCount <dbl>, lat <dbl>, long <dbl>,
#   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>
city_health <- my_clean |>
  group_by(CityName, StateAbbr, lat, long, PopulationCount, Data_Value)

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

First plot chunk here

hchart(city_health, "bubble",
       hcaes(x = long, y = lat,
             size = Data_Value,
             color = Data_Value)) |>
  hc_title(text = "900 Random U.S Cities and their Health Outcome (Adults 18+)") |>
  hc_xAxis(title = list(text = "Longitude")) |>
  hc_yAxis(title = list(text = "Latitude")) |>
  hc_colorAxis(
    stops = color_stops(colors = c("#1a9850", "#fee08b", "#d73027")),
    min = 0,
    max = 100
  ) |>
  hc_tooltip(
    pointFormat = "<b>{point.CityName}, {point.StateAbbr}</b><br>
    Health Value: {point.Data_Value}%<br>
    Population: {point.PopulationCount}"
  )

3. Now create a map of your subsetted dataset.

First map chunk here

leaflet(city_health) |>
  addTiles() |>
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    radius = ~sqrt(PopulationCount/100),
    color = "green",
    fillOpacity = 0.6,
    weight = 1,
    popup = ~paste(
      "<strong>", CityName, ", ", StateAbbr, "</strong><br>",
      "Population: ", format(PopulationCount, big.mark = ",")
    ))

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

Refined map chunk here

leaflet(my_clean) |>
  addProviderTiles("CartoDB.Positron") |>
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    radius = ~sqrt(PopulationCount/80),
    color = ~MeasureId,
    fillOpacity = 0.6,
    popup = ~paste0(
      "<b>", CityName, ", ", StateAbbr, "</b><br>",
      "Value: ", Data_Value, "%", "<br>",
      "Population: ", format(PopulationCount, big.mark = ","), "<br>",
      "Category: ", Category, "<br>",
      "Issues: ", MeasureId
    ),
    label = ~CityName,
    clusterOptions = markerClusterOptions()
  )

5. Write a paragraph

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

I began by cleaning the latlong dataset, randomly sampling 900 rows with slice_sample(), removing “United States,” and selecting the relevant columns. I then filtered the data to have only health measures for adults 18 and older using str_detect(), and removed NA’s from TractFIPS, Data_Value, PopulationCount, and the coordinate column. From that cleaned dataset, I created a summary table called city_health by grouping city, state, coordinates, data value, and population. Using that summary, I built a Highcharter bubble chart plotting cities by their geographic coordinates, where bubble size represents the health measure value, with a red-yellow-green color legend. I also created a basic Leaflet map with circle, with the size being the population, and popups showing the city name and population. Finally, I made a better version of the map by switching to a CartoDB Positron basemap, coloring by MeasureId, and adding more information to the popups.