library(tidyverse)
library(tidyr)
library(leaflet)
setwd("/Users/aashkanavale/Desktop/Montgomery College/MC Spring '24/DATA110/data sets")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")Healthy Cities GIS Assignment
Load the libraries and set the working directory
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("Latitude", "Longitude"), 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>, Latitude <dbl>, Longitude <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>, Latitude <dbl>, Longitude <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] "Latitude" "Longitude"
[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>, Latitude <dbl>, Longitude <dbl>, CategoryID <chr>,
# MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>
# I chose Washington DC because it's a relevant
dc <- prevention |>
filter(StateAbbr %in% c("DC"))
head(dc)# A tibble: 6 × 18
Year StateAbbr StateDesc CityName GeographicLevel Category UniqueID Measure
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
2 2017 DC District o… Washing… Census Tract Prevent… 1150000… Visits…
3 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
4 2017 DC District o… Washing… Census Tract Prevent… 1150000… Choles…
5 2017 DC District o… Washing… Census Tract Prevent… 1150000… Visits…
6 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
# ℹ 10 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
# PopulationCount <dbl>, Latitude <dbl>, Longitude <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.
I’m choosing to filter this dataset by Census Tract so it removes any “City” values.
dc2 <- dc |>
filter(GeographicLevel == "Census Tract")
head(dc)# A tibble: 6 × 18
Year StateAbbr StateDesc CityName GeographicLevel Category UniqueID Measure
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
2 2017 DC District o… Washing… Census Tract Prevent… 1150000… Visits…
3 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
4 2017 DC District o… Washing… Census Tract Prevent… 1150000… Choles…
5 2017 DC District o… Washing… Census Tract Prevent… 1150000… Visits…
6 2017 DC District o… Washing… Census Tract Prevent… 1150000… Taking…
# ℹ 10 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
# PopulationCount <dbl>, Latitude <dbl>, Longitude <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:
# Creating a color palette
desiredcolors <- c("grey80", "azure4", "white", "grey30")I decided to use the data_value variable as my x-axis because it represents the prevalence percentage. The y-axis is the population. I decided on coloring my points by the cities in Pennsylvania and shaped them by the measure taken.
I changed the transparency of the points and renamed my title, axes, and legends. Then I colored the points by the manual color palette I created above.
I got the next section from ChatGPT: I adjusted the value names for the Measure Taken legend to be a little bit more specific. Then I changed the theme of the graph to classic, filled the background to black, and changed the font of my chart.
plot <- dc2 |>
ggplot(aes(x = Data_Value, y = PopulationCount, color = Short_Question_Text)) +
geom_point() +
labs(title = "Measures Taken in Washington DC",
x = "Prevalence Percentage",
y = "Population",
color = "Measure Taken") +
scale_color_manual(values = desiredcolors,
labels = c("Annual Checkup ≥ 18 y/o",
"Cholesterol Screening ≥ 18 y/o",
"Lack of Health Insurance 18 - 64 y/o",
"Taking High BP Medication ≥ 18 y/o")) +
theme_classic() +
theme(panel.background = element_rect(fill = "black")) +
theme(text = element_text(family = "serif"))
plotWarning: Removed 4 rows containing missing values (`geom_point()`).
3. Now create a map of your subsetted dataset.
Here I’m using the package, leaflet. I set the longitude and latitude according to the coordinates of DC. I just pulled them from a quick Google search. Then I set the zoom to 11 since DC is so small and isn’t a state, the zoom needs to be much bigger. I set the theme of the chart to match my graph from above and made it dark. Then I plotted the points by Data_Value.
leaflet() |>
setView(lng = -77.009056, lat = 38.889805, zoom = 11) |>
addProviderTiles("CartoDB.DarkMatter") |>
addCircles(data = dc2,
radius = dc2$Data_Value)Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
4. Refine your map to include a mousover tooltip
Refined map chunk here
# Creating the popup chunk
dcpop <- paste0(
"<b>Measure Taken: </b>", dc2$Short_Question_Text, "<br>",
"<b>Prevalence Percentage: </b>", dc2$Data_Value, "<br>",
"<b>Population Count: </b>", dc2$PopulationCount, "<br>")# Plotting the same leaflet plot but with popups and refining the colors
leaflet() |>
setView(lng = -77.009056, lat = 38.889805, zoom = 11) |>
addProviderTiles("CartoDB.DarkMatter") |>
addCircles(data = dc2,
radius = dc2$Data_Value^10 / 100000000000000000, # Source : Emilio D.
color = "white",
fillColor = "black",
popup = dcpop)Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
5. Reflection
In a paragraph, describe the plots you created and what they show.
I decided to create plots on only Washington DC. My first plot shows the relationship between population and prevalence percentage, but also sorted by each measure taken. The lack of health insurance is the one measure taken that has the least prevalence. Then I created an interactive leaflet graph and set the radius to the prevalence percentage. Each point on the chart represents a different instance that has happened in 2017 in Washington DC. Each popup has the Measure Taken, the exact Prevalence Percentage, and the Population Count at that time in that year.