Healthy Cities GIS Assignment

Author

M Sullivan

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
library(leaflet)
library(ggplot2)
library(ggthemes)
setwd("C:/Users/micha/OneDrive/Documents/DATA 110")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")

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>

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.

Lowercase the names of columns

names(prevention) <- tolower(names(prevention))
names(prevention) <- gsub(" ","_",names(prevention))
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>

Additional filter to highlight cholesterol screening among adults older than 18

measure <- prevention |>
  filter(measure == "Cholesterol screening among adults aged >=18 Years")
head(measure)
# 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   Choles…
3  2017 CA        California Richmond  Census Tract    Prevent… 0660620… Choles…
4  2017 FL        Florida    Davie     Census Tract    Prevent… 1216475… Choles…
5  2017 FL        Florida    Hialeah   Census Tract    Prevent… 1230000… Choles…
6  2017 FL        Florida    Miami Be… Census Tract    Prevent… 1245025… 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>
unique(latlong_clean$StateAbbr)
 [1] "AL" "CA" "FL" "CT" "IL" "MN" "NY" "PA" "NC" "OH" "OK" "OR" "TX" "RI" "SC"
[16] "SD" "TN" "UT" "VA" "WA" "AK" "WI" "AZ" "AR" "CO" "DE" "NV" "DC" "GA" "ID"
[31] "HI" "MA" "MI" "IN" "KS" "KY" "IA" "LA" "MD" "ME" "NH" "NJ" "NM" "MO" "MS"
[46] "NE" "MT" "ND" "WV" "VT" "WY"

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

First plot chunk here

ggplot(measure, aes(x = stateabbr, y = populationcount, color = stateabbr)) +
  geom_point(aes(size=data_value)) +
  labs(title = "Test", x = "State", y = "Population") +
  theme_light() +
    scale_fill_viridis_b(name = "Measure") +
  theme(axis.text.x = element_text(angle = 50, hjust = 1))
Warning: Removed 794 rows containing missing values or values outside the scale range
(`geom_point()`).

Remove outlier New York, NY

measure2 <- measure[measure$cityname != "New York",]
p2 <- ggplot(measure2, aes(x = stateabbr, y = populationcount, color = stateabbr, head(data(, 10)))) +
  geom_point(aes(size=data_value)) +
  labs(title = "Test", x = "State", y = "Population") +
  theme_light() +
    scale_fill_viridis_b(name = "Measure") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
options(scipen = 999)
p2 + geom_point() # add the points
Warning: Removed 771 rows containing missing values or values outside the scale range
(`geom_point()`).

fl <- prevention |>
  filter(stateabbr == "FL")
head(fl)
# A tibble: 6 × 18
   year stateabbr statedesc cityname   geographiclevel category uniqueid measure
  <dbl> <chr>     <chr>     <chr>      <chr>           <chr>    <chr>    <chr>  
1  2017 FL        Florida   Davie      Census Tract    Prevent… 1216475… "Chole…
2  2017 FL        Florida   Hialeah    Census Tract    Prevent… 1230000… "Visit…
3  2017 FL        Florida   Hialeah    Census Tract    Prevent… 1230000… "Chole…
4  2017 FL        Florida   Miami Bea… Census Tract    Prevent… 1245025… "Chole…
5  2017 FL        Florida   Palm Coast City            Prevent… 1254200  "Curre…
6  2017 FL        Florida   Tampa      City            Prevent… 1271000  "Chole…
# ℹ 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>
florida_cities <- fl |>
  filter(geographiclevel == "City") 
head(florida_cities)
# A tibble: 6 × 18
   year stateabbr statedesc cityname   geographiclevel category uniqueid measure
  <dbl> <chr>     <chr>     <chr>      <chr>           <chr>    <chr>    <chr>  
1  2017 FL        Florida   Palm Coast City            Prevent… 1254200  "Curre…
2  2017 FL        Florida   Tampa      City            Prevent… 1271000  "Chole…
3  2017 FL        Florida   Coral Spr… City            Prevent… 1214400  "Curre…
4  2017 FL        Florida   Deerfield… City            Prevent… 1216725  "Visit…
5  2017 FL        Florida   Deltona    City            Prevent… 1217200  "Curre…
6  2017 FL        Florida   Hollywood  City            Prevent… 1232000  "Takin…
# ℹ 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>
p4 <- ggplot(florida_cities, aes(x = cityname, y = populationcount, fill = short_question_text)) +
  geom_bar(stat = "identity", position = "Dodge") +
  labs(title = "Florida Prevention Broken Down by City", x = "City", y = "Population") +
  theme_stata() +
  scale_fill_brewer(name = "Measure") +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) +
  theme(axis.text.y = element_text(angle = 70, hjust = 1))
p4 + geom_point() # add the points

3. Now create a map of your subsetted dataset.

First map chunk here

leaflet(data = florida_cities) |>
  setView(lng = -83.0, lat = 28.0, zoom = 12) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles()
Assuming "long" and "lat" are longitude and latitude, respectively

4. Refine your map to include a mousover tooltip

Refined map chunk here

color_palette <- colorFactor(viridis::viridis_pal()(length(unique(florida_cities$short_question_text))), domain = florida_cities$short_question_text)
leaflet(data = florida_cities) |>
  setView(lng = -82.72448, lat = 27.78623, zoom = 12) |>
  addProviderTiles("Esri.WorldStreepMap") |>
  addCircles(
    data = florida_cities,
    radius = florida_cities$data_value* 3,
    color = color_palette(florida_cities$short_question_text),
    fillOpacity = 0.5,
    weight = 1.5,
    popup = ~paste("<b>City: ", cityname, "<br>",
                   "<b>Population Count: ", populationcount, "<br>",
                   "<b>Measure: ", short_question_text, "<br>",
                   "<b>Data Value: ", data_value))
Assuming "long" and "lat" are longitude and latitude, respectively

5. Write a paragraph

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

The plots created in this document came from the 500 Healthy Cities dataset. First I filtered the data to include only cholesterol screening among adults ages 18 and up. From implementing that filter, I initially wanted to create a visualization that plotted just the states in this dataset with their population size. Realizing that this was not turning out to be the most interesting plot, I altered my code to instead focus solely on cities within the state of Florida. I also added the fill function to include the short question text column within each of these cities. The four possible options, annual checkup, cholesterol screening, health insurance and taking BP medication, are color coded on each city bar. Unfortunately, there are several cities within Florida in this dataset, so it is difficult to see the distribution of each short question text within each city since the data appears so zoomed out. That said, I find it intriguing that Jacksonville, FL has more than double a population size than Miami, the next highest. Perhaps removing Jacksonville as an outlier would have been wise to help create some space between each bar. It was also challenging picking an appropriate latitude and longitude as well as zoom function since you must click on the “-” symbol to find my plotted points on the map.