Healthy Cities GIS Assignment

Author

Julian Beckert

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
library(leaflet)
library(sf)
setwd("/Users/Lucinda/Downloads/data110")
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>
#unique(md$CityName) 

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

unique(latlong_clean2$StateDesc)
 [1] "California"    "Alabama"       "Alaska"        "Arizona"      
 [5] "Arkansas"      "Connecticut"   "Delaware"      "District of C"
 [9] "Florida"       "Colorado"      "Illinois"      "Indiana"      
[13] "Iowa"          "Kansas"        "Georgia"       "Idaho"        
[17] "Kentucky"      "Louisiana"     "Maine"         "Massachusetts"
[21] "Michigan"      "Minnesota"     "Mississippi"   "Missouri"     
[25] "Montana"       "Nebraska"      "New York"      "Nevada"       
[29] "New Hampshire" "New Jersey"    "Pennsylvania"  "North Carolin"
[33] "North Dakota"  "Ohio"          "Oklahoma"      "Oregon"       
[37] "Texas"         "Rhode Island"  "South Carolin" "South Dakota" 
[41] "Tennessee"     "Utah"          "Vermont"       "Virginia"     
[45] "Washington"    "West Virginia" "Wisconsin"     "Wyoming"      
[49] "Hawaii"        "Maryland"      "New Mexico"   
tx_lat <- latlong_clean2 |>
  filter(StateDesc == "Texas")
# I think the professor meant to select "prevention" in chunk 3, or maybe we're supposed to have noticed and added it, but I'm going to add it here
tx_lat2 <- tx_lat |>
  filter(Category == "Prevention") |>
  filter(GeographicLevel == "Census Tract") |> # there were too few in the only other option, "City", though I might as well just remove "City" altogether
  filter(PopulationCount > 7200)

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

First plot chunk here

tx_lat3 <- tx_lat2 |>
  filter(Measure == "Current lack of health insurance among adults aged 18\x9664 Years") |>
  group_by(CityName, PopulationCount) |>
  summarize(count = n())
`summarise()` has grouped output by 'CityName'. You can override using the
`.groups` argument.
ggplot(tx_lat3, aes(x=PopulationCount, y=CityName))+
  geom_histogram(stat = "identity", fill = "#de3a83") +
  labs(title = "Total Count for Lack of Health Insurance in Adults by City (Texas)",
       x = "Count",
       y = "City",
       caption = "Source: Centers for Disease Control and Prevention") +
  theme_minimal(base_size = 11, base_family = "serif")
Warning in geom_histogram(stat = "identity", fill = "#de3a83"): Ignoring
unknown parameters: `binwidth`, `bins`, and `pad`

3. Now create a map of your subsetted dataset.

First map chunk here

leaflet() |>
  setView(lng = -99.9, lat = 31.96, zoom =5.2) |>
  addProviderTiles("OpenTopoMap") |>
  addCircles(
    data = tx_lat2,
    radius = tx_lat2$PopulationCount,
    color = "#bf1871")
Assuming "long" and "lat" are longitude and latitude, respectively

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

Refined map chunk here

popuplat <- paste0(
      "<b>Population: </b>", tx_lat3$PopulationCount, "<br>",
      "<b>City Name: </b>", tx_lat3$CityName, "<br>")

leaflet() |>
  setView(lng = -99.9, lat = 31.96, zoom =5.2) |>
  addProviderTiles("OpenTopoMap") |>
  addCircles(
    data = tx_lat2,
    radius = tx_lat2$PopulationCount,
    color = "#bf1871",
    popup = popuplat)
Assuming "long" and "lat" are longitude and latitude, respectively
popup = popuplat

5. Write a paragraph

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

My first plot shows the occurance of a lack of health insurance per county. Sometimes, subsetting data, especially for a bar chart, is unusually difficult. This was one of those times. I was trying to figure out how to show which prevention measure was most common in each city, but for whatever reason just couldn’t figure it out. What’s shown is really just the total number of instances created instead of the fraction out of the population for each city, which is what I wanted to show. I’m going to spend more time after submitting this trying to figure out what the issue was. I used filter, group_by and summarize to subset the data, and used a histogram to plot it. The second and third charts show population per city in Texas, using leaflet.