Healthy Cities GIS Assignment

Author

Leika Joseph

Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
setwd("/Users/leikarayjoseph/Desktop/Data 110")
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

fl <- prevention |>
  filter(StateAbbr=="FL")|>
 # filter(Short_Question_Text %in% c("Cholesterol Screening", "Health Insurance")) |>
filter(CityName %in%c("Miami", "Tampa")) |>
 # Change long and lat to numeric
   mutate(lat= as.numeric(lat),
        long = as.numeric(long)) 
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   Tampa    City            Prevention 1271000  "Chole…
2  2017 FL        Florida   Miami    Census Tract    Prevention 1245000… "Chole…
3  2017 FL        Florida   Miami    Census Tract    Prevention 1245000… "Chole…
4  2017 FL        Florida   Miami    Census Tract    Prevention 1245000… "Takin…
5  2017 FL        Florida   Miami    Census Tract    Prevention 1245000… "Curre…
6  2017 FL        Florida   Miami    Census Tract    Prevention 1245000… "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>
#fl1 <- prevention |>
 #filter(StateAbbr=="FL")|>
 #filter(Short_Question_Text == "Cholesterol Screening")
#filter(CityName %in%c("Miami", "Tampa", "Jacksonville", "Orlando"))
#head(fl1)

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

First plot chunk here

options(scipen = 999) # Make population count into normal notation instead of scientific notation.
ggplot(fl, aes(x=PopulationCount, y= Data_Value, color = Short_Question_Text)) +
  geom_point(alpha = 0.05) +
  scale_color_viridis_d()+
  geom_jitter() +
  #facet_wrap(~Short_Question_Text) +
  labs(title = "Population vs Data_Value by Short_Question_Text")  +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels
Warning: Removed 28 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 28 rows containing missing values or values outside the scale range
(`geom_point()`).

3. Now create a map of your subsetted dataset.

First map chunk here

library(leaflet)

leaflet() |>
 setView( lng = mean(fl$long), lat = mean(fl$lat), zoom = 6) |>
  addProviderTiles("Esri.WorldStreetMap") |>
 addCircles(
    data = fl,
    radius = ~sqrt(Data_Value),
   color= "darkblue",
    fillColor = "red",
    fillOpacity = 0.25
)
Assuming "long" and "lat" are longitude and latitude, respectively
# Set color pallette for my map
#color_palette <- colorFactor(palette = "pastel2", domain = fl$Short_Question_Text)

# Now let's make the map
#leaflet(data= fl) |>
 #setView( lng = mean(fl$long), lat = mean(fl$lat), zoom = 6) |>
  #addProviderTiles("Esri.WorldStreetMap") |>
 #addCircles(
   # radius = ~sqrt(Data_Value),
   #color= ~color_palette(Short_Question_Text),
   # fillColor = ~color_palette(Short_Question_Text),
    #fillOpacity = 0.7)

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

Refined map chunk here

popup <- paste0(
      "<b>Prevalance: </b>", fl$Data_Value_Type, "<br>",
      "<b>Population: </b>", fl$PopulationCount, "<br>",
      "<b>Data_Value(%): </b>", fl$Data_Value, "<br>",
      "<strong>reason: </strong>", fl$Short_Question_Text, "<br>"
    )
# filter data for only Cholesterol screening
fl1 <- fl |>
  filter(Short_Question_Text == "Cholesterol Screening")
#create popup with fl1
popup <- paste0(
      "<b>Prevalance: </b>", fl1$Data_Value_Type, "<br>",
      "<b>Population: </b>", fl1$PopulationCount, "<br>",
      "<b>Data_Value(%): </b>", fl1$Data_Value, "<br>",
      "<strong>reason: </strong>", fl1$Short_Question_Text, "<br>"
    )

leaflet() |>
 setView( lng = mean(fl$long), lat = mean(fl$lat), zoom = 6) |>
  addProviderTiles("Esri.WorldStreetMap") |>
 addCircles(
    data = fl1,
    radius = fl$Data_Value*10, # multiply by 10 to increase the point size,
   color= "darkblue",
    fillColor = "red",
    fillOpacity = 0.5,
   popup = popup)
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.

For my first plot, I created a scatter plot of Population vs. data value by Short_Question_Text. In my plot, I observed that Health Insurance, in green, has the most spread across the Data Value. All the points are clustered in one line, which makes the plot not really interesting. 

For my map using leaflet, I filtered and chose Florida as the state I want to work with and I also filtered for the cityName and chose Tampa and Miami. I chose this state because I know and see among my research that its population is diverse and it’s a common place for tourism. I go further and add a popup on my map but this time I also filter the Short_Question_Text to only show the result for Cholesterol screening in the map we also observe that there are more points in than there are in Tampa than there are in Miami.