library(tidyverse)
library(tidyr)
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("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>
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.
Filter chunk here
unique(latlong_clean$StateAbbr) [1] "AL" "CA" "FL" "CT" "IL" "MN" "NY" "PA" "NC" "OH" "OK" "OR" "TX" "RI" "SC"
[16] "SD" "TN" "UT" "VA" "WA" "AK" "WI" "AZ" "AR" "CO" "DE" "NV" "DC" "GA" "ID"
[31] "HI" "MA" "MI" "IN" "KS" "KY" "IA" "LA" "MD" "ME" "NH" "NJ" "NM" "MO" "MS"
[46] "NE" "MT" "ND" "WV" "VT" "WY"
# Filter Prevention dataset for only the state of Maryland
subset_data <- prevention |>
filter(StateAbbr == "MD")
head(subset_data)# 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>
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
library(ggplot2)
# New column for plotting the measures: Cholesterol and Checkup
subset_data$Measure_Type <- ifelse(subset_data$Measure == "Cholesterol screening among adults aged >=18 Years", "Cholesterol", "Checkup")
# Scatterplot for both Annual checkup and Cholesterol screening
plot_combined <- ggplot(subset_data, aes(x = PopulationCount, y = Data_Value, color = Measure_Type)) +
geom_point(na.rm=TRUE) +
scale_color_manual(values = c("Cholesterol" = "blue", "Checkup" = "green")) +
labs(title = "Cholesterol Screening vs Annual Checkup",
x = "Population Count",
y = "Data Value",
color = "Measure") +
theme_minimal() +
scale_x_continuous(labels = scales::comma, limits = c(0, 10000))
# Display combined scatterplot
plot_combined3. Now create a map of your subsetted dataset.
First map chunk here
library(leaflet)
library(leaflet)
# Filter Prevention dataset for the State of Maryland
md_data <- subset_data |> filter(StateAbbr == "MD")
# Initialize the leaflet map
map <- leaflet() %>%
setView(lng = -76.6122, lat = 39.2904, zoom = 9) %>%
addProviderTiles("OpenStreetMap.Mapnik")
# Add circles for cholesterol screenings
map <- map %>%
addCircles(
data = md_data |> filter(MeasureId == "CHOLSCREEN"),
radius = sqrt(md_data$PopulationCount) * 0.03, # Adjust the radius for smaller circles
color = "blue",
fillColor = "blue",
fillOpacity = 0.5
)Assuming "long" and "lat" are longitude and latitude, respectively
# Add circles for annual checkups
map <- map %>%
addCircles(
data = md_data |> filter(MeasureId == "CHECKUP"),
radius = sqrt(md_data$PopulationCount) * 0.03, # Adjust the radius for smaller circles
color = "green",
fillColor = "green",
fillOpacity = 0.5
)Assuming "long" and "lat" are longitude and latitude, respectively
# Display the map
map4. Refine your map to include a mouseover tooltip
Refined map chunk here
library(leaflet)
# Filter dataset for Maryland only
md_data <- subset_data |> filter(StateAbbr == "MD")
# Leaflet Map initalization
map <- leaflet() %>%
setView(lng = -76.6122, lat = 39.2904, zoom = 9) %>%
addProviderTiles("OpenStreetMap.Mapnik")
# Markers for Cholesterol screenings
map <- map %>%
addCircleMarkers(
data = md_data |> filter(MeasureId == "CHOLSCREEN"),
radius = sqrt(md_data$PopulationCount) * 0.03,
# Radius for smaller circles
color = "blue",
fillColor = "blue",
fillOpacity = 0.5,
popup = ~paste("City: ", md_data$CityName, "<br>",
"Cholesterol Screening: ", md_data$Data_Value, "%")
)Assuming "long" and "lat" are longitude and latitude, respectively
# Markers for Annual checkups
map <- map %>%
addCircleMarkers(
data = md_data |> filter(MeasureId == "CHECKUP"),
radius = sqrt(md_data$PopulationCount) * 0.03,
# Radius for smaller circles
color = "green",
fillColor = "green",
fillOpacity = 0.5,
popup = ~paste("City: ", md_data$CityName, "<br>",
"Annual Checkup: ", md_data$Data_Value, "%")
)Assuming "long" and "lat" are longitude and latitude, respectively
# Display map
map5. Write a paragraph
In a paragraph, describe the plots you created and what they show.
####. The following plots above show comparisons for Cholesterol screenings and also Annual checkups. I chose these two and compared each of their respective data values as shown in the first scatterplot. It was interesting to see how that even though there was more annual checkups that cholesterol screenings scored higher data values. There was a significant skew for data value for annual check ups compared to actual cholesterol screenings. The next plots I created were the mapping of our state and the coordinates for each data value. The only city in question was Baltimore so there’s overwhelming data surrounding it. The first map plot isn’t interactive like the second plot where you can mouse hover and have a tool tip. Overall, I think the plots do a good job of comparing both cholesterol screenings and Annual checkups in Baltimore, Maryland.