library(tidyverse)
library(tidyr)
library(leaflet)
setwd("C:/Users/ubjho/Downloads")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")Healthy Cities GIS Assignment
Load the libraries and set the working directory
For your assignment, work with a cleaned dataset.
1. Once you run the above code and learn how to filter this complicated dataset, perform your own investigation by filtering this dataset however you choose so that you have a subset with no more than 900 observations.
Filter chunk here (you may need multiple chunks)
latlong <- cities500 |>
mutate(GeoLocation = str_replace_all(GeoLocation, "[()]", "")) |>
separate(GeoLocation, into = c("lat", "long"), sep = ",", convert = TRUE)Filtering data
health_subset <- latlong |>
filter(StateDesc != "United States") |>
filter(Data_Value_Type == "Age-adjusted prevalence") |>
filter(Year == 2017) |>
filter(StateAbbr == "CA") |>
filter(Category == "Health Outcomes") |>
slice_head(n = 900)Creating Bar graph
First plot chunk here
latlong_plot <- health_subset |>
group_by(Short_Question_Text) |>
mutate(total_value = sum(Data_Value)) |>
ungroup() |>
distinct(Short_Question_Text, total_value) |>
arrange(desc(total_value)) |>
filter(row_number() <= 5)
ggplot(latlong_plot, aes(x = reorder(Short_Question_Text, total_value), y = total_value)) +
geom_col(fill = "forestgreen") +
coord_flip() +
labs(
title = "Top 5 Health Outcomes by Total Prevalence\n(California, 2017)",
x = "Health Outcome",
y = "Total Prevalence Score"
) +
theme_minimal()Mapping the graph
latlong_top5 <- health_subset |>
filter(Short_Question_Text %in% latlong_plot$Short_Question_Text)
popup_health <- paste0(
"<b>City:</b> ", latlong_top5$CityName, "<br>",
"<b>Outcome:</b> ", latlong_top5$Short_Question_Text, "<br>",
"<b>Value:</b> ", latlong_top5$Data_Value, "%"
)map1 <- leaflet(latlong_top5) |>
addProviderTiles("Esri.WorldStreetMap") |>
addCircles(
data = health_subset,
lng = ~long,
lat = ~lat,
radius = ~Data_Value * 100,
color = "violet",
fillColor = "maroon",
fillOpacity = 0.4
)
map1Adding different colors
pal <- colorFactor(
palette = c("blueviolet", "chartreuse", "darkred", "deeppink", "darkslateblue"),
levels = unique(latlong_top5$Short_Question_Text)
)Displaying the map with legend
map1 <- leaflet(latlong_top5) |>
setView(lng = -119.4179, lat = 36.7783, zoom = 6) |>
addProviderTiles("Esri.WorldStreetMap") |>
addCircles(
lng = ~long,
lat = ~lat,
fillColor = ~pal(Short_Question_Text),
color = ~pal(Short_Question_Text),
radius = ~Data_Value * 50,
fillOpacity = 0.8,
popup = popup_health
) |>
addLegend(
"topleft",
pal = pal,
values = latlong_top5$Short_Question_Text,
title = "Top 5 Health Outcomes",
opacity = 0.7
)
map15. Write a paragraph
The dataset focuses on Health Outcomes in California in 2017, measured by crude prevalence rates. The bar plot reveals which health issues were most common, with “High blood pressure” and “Arthritis” showing the highest mean prevalence across cities. The interactive map displays the geographical spread of these conditions, with tooltips providing detailed information for each city. This visualization allows for quick identification of areas with higher burdens of chronic illness and supports targeted public health interventions.