Healthy Cities GIS Assignment

Author

Efren Martinez

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
library(leaflet)
library(viridis)
setwd("C:/Users/Marti/OneDrive/Desktop/MC-DV")
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>
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>
wi <- prevention |>
  filter(stateabbr == "WI")
head(wi)
# A tibble: 6 × 18
   year stateabbr statedesc cityname  geographiclevel category  uniqueid measure
  <dbl> <chr>     <chr>     <chr>     <chr>           <chr>     <chr>    <chr>  
1  2017 WI        Wisconsin Madison   Census Tract    Preventi… 5548000… "Takin…
2  2017 WI        Wisconsin Madison   Census Tract    Preventi… 5548000… "Chole…
3  2017 WI        Wisconsin Milwaukee Census Tract    Preventi… 5553000… "Visit…
4  2017 WI        Wisconsin Milwaukee Census Tract    Preventi… 5553000… "Chole…
5  2017 WI        Wisconsin Milwaukee Census Tract    Preventi… 5553000… "Takin…
6  2017 WI        Wisconsin Madison   Census Tract    Preventi… 5548000… "Curre…
# ℹ 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 data set “Prevention” is a manageable data set now.

For your assignment, work with the cleaned “Prevention” data set

1. Once you run the above code, filter this data set one more time for any particular subset.

Filter chunk here

Additional filter to only focus on the city of Kenosha, Wisconsin

kenosha_wi <- wi |>
  filter(cityname == "Kenosha")
head(kenosha_wi)
# A tibble: 6 × 18
   year stateabbr statedesc cityname geographiclevel category   uniqueid measure
  <dbl> <chr>     <chr>     <chr>    <chr>           <chr>      <chr>    <chr>  
1  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Chole…
2  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Visit…
3  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Curre…
4  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Chole…
5  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Chole…
6  2017 WI        Wisconsin Kenosha  Census Tract    Prevention 5539225… "Curre…
# ℹ 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>

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

First plot chunk here

Plotting to see the percentages of the data values in the Kenosha Wisconsin Data set by the short question text since it’s a shorter version of the measure.

ggplot(kenosha_wi, aes(x = short_question_text , y = data_value, color = short_question_text )) +
  geom_point(aes(size=data_value)) +
  labs(title = "Prevention for Kenosha Wisonsin", y = "Percentage", x = "Population") +
  theme_bw() +
    scale_fill_viridis_d(name = "Measure") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).

To see what else we can do with a differnt filter instead of just seeing Kenosha data lets filter for only “City” information for all of Wisconsin.

wisconsin_cities <- wi |>
  filter(geographiclevel == "City")
head(wisconsin_cities)
# A tibble: 6 × 18
   year stateabbr statedesc cityname  geographiclevel category  uniqueid measure
  <dbl> <chr>     <chr>     <chr>     <chr>           <chr>     <chr>    <chr>  
1  2017 WI        Wisconsin Milwaukee City            Preventi… 5553000  "Visit…
2  2017 WI        Wisconsin Green Bay City            Preventi… 5531000  "Chole…
3  2017 WI        Wisconsin Kenosha   City            Preventi… 5539225  "Visit…
4  2017 WI        Wisconsin Appleton  City            Preventi… 5502375  "Chole…
5  2017 WI        Wisconsin Racine    City            Preventi… 5566000  "Curre…
6  2017 WI        Wisconsin Green Bay City            Preventi… 5531000  "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>

Now using a bar graph to plot Wisconsin City data for each measure comparing each city.

ggplot(wisconsin_cities, aes(x = cityname, y = data_value, fill = short_question_text)) +
  geom_bar(stat = "identity", position = "Dodge") +
  labs(title = "Prevention for Wisconsin Cities", y = "Percentage of Population", x = "City") +
  theme_bw() +
  scale_fill_viridis_d(name = "Measure") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

3. Now create a map of your subsetted dataset.

First map chunk here

leaflet(data = kenosha_wi) |>
  setView(lng = -87.87357, lat = 42.58557, zoom = 12) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles(
    data = kenosha_wi,
    radius = kenosha_wi$data_value
)
Assuming "long" and "lat" are longitude and latitude, respectively
leaflet(data = wisconsin_cities) |>
  setView(lng = -88.2, lat = 43.5, zoom = 7) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles(
    data = wisconsin_cities,
    radius = wisconsin_cities$data_value
)
Assuming "long" and "lat" are longitude and latitude, respectively

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

Refined map chunk here

color_palette <- colorFactor(viridis::viridis_pal()(length(unique(kenosha_wi$short_question_text))), 
                             domain = kenosha_wi$short_question_text)

leaflet(data = kenosha_wi) |>
  setView(lng = -87.87357, lat = 42.58557, zoom = 12)  |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles(
    data = kenosha_wi,
     radius = kenosha_wi$data_value* 4,
        color = color_palette(kenosha_wi$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
color_palette <- colorFactor(viridis::viridis_pal()(length(unique(kenosha_wi$short_question_text))), 
                             domain = kenosha_wi$short_question_text)

leaflet(data = wisconsin_cities) |>
  setView(lng = -88.2, lat = 43.5, zoom = 7)  |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles(
    data = wisconsin_cities,
     radius = wisconsin_cities$data_value* 100,
    color = ~color_palette(short_question_text),
    fillColor = ~color_palette(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.

From my 500 Heathly Cities Data Set, I decided to take two approaches as my hometown and home state is Kenosha, Wisconsin. I filtered to the selection of just Kenosha, as well as just Wisconsin City Data. I plotted the different measures in prevention for Kenosha, but didn’t read much so I also did it for all Wisconsin cities. In the latter I found the large difference that the lack of health insurance was much lower than the rest of the catagories. I also mapped first for each data point then to try and show to different levels of percentage per catagory in each area, both in the Kenosha map, as well as the Wisconsin map. In these different sized bubbles it shows the percentage value of the measure in the percentage of that population with different colors representing the different inner circles. While the tool tip does seem to work, it doesn’t always select the individual inner circles as unique tool tips.