library(tidyverse)
library(tidyr)
library(leaflet)
setwd("/Users/wiktor/Downloads")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")
data(cities500)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(Data_Value_Type == "Crude prevalence") |>
filter(Year == 2017) |>
filter(StateAbbr == "CT") |>
filter(Category == "Unhealthy Behaviors")
head(latlong_clean)# A tibble: 6 × 25
Year StateAbbr StateDesc CityName GeographicLevel DataSource Category
<dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 CT Connecticut Bridgeport Census Tract BRFSS Unhealthy B…
2 2017 CT Connecticut Danbury City BRFSS Unhealthy B…
3 2017 CT Connecticut Norwalk Census Tract BRFSS Unhealthy B…
4 2017 CT Connecticut Bridgeport Census Tract BRFSS Unhealthy B…
5 2017 CT Connecticut Hartford Census Tract BRFSS Unhealthy B…
6 2017 CT Connecticut Waterbury Census Tract BRFSS Unhealthy B…
# ℹ 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 CT Connecticut Bridgep… Census Tract Unhealt… 0908000… Obesit…
2 2017 CT Connecticut Danbury City Unhealt… 918430 Obesit…
3 2017 CT Connecticut Norwalk Census Tract Unhealt… 0955990… Obesit…
4 2017 CT Connecticut Bridgep… Census Tract Unhealt… 0908000… Curren…
5 2017 CT Connecticut Hartford Census Tract Unhealt… 0937000… Obesit…
6 2017 CT Connecticut Waterbu… Census Tract Unhealt… 0980000… 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>
The new dataset “latlong_clean2” is a manageable dataset now.
For your assignment, work with a cleaned dataset where you perform your own cleaning and filtering.
1. Once you run the above code and 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 through some inclusion/exclusion criteria.
Filter chunk here (you may need multiple chunks)
latlong_clean2 |> count(Measure)# A tibble: 4 × 2
Measure n
<chr> <int>
1 Binge drinking among adults aged >=18 Years 228
2 Current smoking among adults aged >=18 Years 228
3 No leisure-time physical activity among adults aged >=18 Years 228
4 Obesity among adults aged >=18 Years 228
smoking <- latlong_clean2 |>
filter(str_detect(Measure, regex("smoking", ignore_case = TRUE))) |>
filter(GeographicLevel == "Census Tract") |>
drop_na(lat, long, Data_Value)
nrow(smoking)[1] 220
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
# non map plot
ggplot(smoking, aes(x = reorder(CityName, Data_Value, FUN = median),
y = Data_Value)) +
geom_boxplot(fill = "blue") +
coord_flip() +
labs(title = "Current Smoking Prevalence by City, Connecticut (2017)",
x = "City",
y = "Crude prevalence (%)")3. Now create a map of your subsetted dataset.
First map chunk here
# leaflet()
smoking <- smoking |>
mutate(level = case_when(
Data_Value <= quantile(Data_Value, 0.33) ~ "Low",
Data_Value <= quantile(Data_Value, 0.66) ~ "Medium",
TRUE ~ "High"),
dot_color = case_when(
level == "Low" ~ "yellow",
level == "Medium" ~ "orange",
TRUE ~ "red"))
leaflet(smoking) |>
addTiles() |>
addCircleMarkers(lng = ~long, lat = ~lat,
radius = 5,
color = ~dot_color,
fillOpacity = 0.8,
stroke = FALSE) 4. Refine your map to include a mouse-click tooltip
Refined map chunk here
smoking <- smoking |>
mutate(popup_text = paste0("<b>City:</b> ", CityName, "<br>",
"<b>Tract:</b> ", TractFIPS, "<br>",
"<b>Smoking prevalence:</b> ", Data_Value, "%<br>",
"<b>Population:</b> ", PopulationCount))
leaflet(smoking) |>
addTiles() |>
addCircleMarkers(lng = ~long, lat = ~lat,
radius = 5,
color = ~dot_color,
fillOpacity = 0.8,
stroke = FALSE,
popup = ~popup_text)5. Write a paragraph
In a paragraph, describe the plots you created and the insights they show.
The boxplot compares the crude prevalence of current smoking across census tracts in each Connecticut city in the 500 Cities data for 2017. Cities are ordered by median prevalence, which makes it easy to see which cities have the highest typical smoking rates and which have the widest spread between tracts. The map shows the same tracts geographically, with darker points marking higher smoking prevalence, and clicking a point reveals its city, tract, prevalence, and population. Together, the plots show that smoking is not evenly distributed. This suggests that differences within a city can be as large as differences between cities, so tract-level data reveal patterns that city averages would hide.