library(tidyverse)
library(tidyr)
setwd("C:/Users/zivsa/Desktop/OneDrive - montgomerycollege.edu/school/DATA 110/W10")
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(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
unique(latlong$Measure) [1] "Arthritis among adults aged >=18 Years"
[2] "Current smoking among adults aged >=18 Years"
[3] "Coronary heart disease among adults aged >=18 Years"
[4] "Obesity among adults aged >=18 Years"
[5] "Cholesterol screening among adults aged >=18 Years"
[6] "Binge drinking among adults aged >=18 Years"
[7] "Chronic obstructive pulmonary disease among adults aged >=18 Years"
[8] "Diagnosed diabetes among adults aged >=18 Years"
[9] "Mammography use among women aged 50\x9674 Years"
[10] "All teeth lost among adults aged >=65 Years"
[11] "Current asthma among adults aged >=18 Years"
[12] "Current lack of health insurance among adults aged 18\x9664 Years"
[13] "Chronic kidney disease among adults aged >=18 Years"
[14] "Stroke among adults aged >=18 Years"
[15] "Visits to dentist or dental clinic among adults aged >=18 Years"
[16] "No leisure-time physical activity among adults aged >=18 Years"
[17] "Sleeping less than 7 hours among adults aged >=18 Years"
[18] "High blood pressure among adults aged >=18 Years"
[19] "Cancer (excluding skin cancer) among adults aged >=18 Years"
[20] "Visits to doctor for routine checkup within the past Year among adults aged >=18 Years"
[21] "Papanicolaou smear use among adult women aged 21\x9665 Years"
[22] "Physical health not good for >=14 days among adults aged >=18 Years"
[23] "Mental health not good for >=14 days among adults aged >=18 Years"
[24] "Older adult men aged >=65 Years who are up to date on a core set of clinical preventive services: Flu shot past Year, PPV shot ever, Colorectal cancer screening"
[25] "High cholesterol among adults aged >=18 Years who have been screened in the past 5 Years"
[26] "Taking medicine for high blood pressure control among adults aged >=18 Years with high blood pressure"
[27] "Fecal occult blood test, sigmoidoscopy, or colonoscopy among adults aged 50\x9675 Years"
[28] "Older adult women aged >=65 Years who are up to date on a core set of clinical preventive services: Flu shot past Year, PPV shot ever, Colorectal cancer screening, and Mammogram past 2 Years"
#filter for a specific measures in florida cities
fl <- latlong |> filter(Short_Question_Text %in% c("Mental Health", "Physical Health", "Obesity", "Health Insurance", "High Blood Pressure", "Binge Drinking") & GeographicLevel=="City" & DataValueTypeID=="AgeAdjPrv" & StateAbbr=="FL")
#remove unused columns
fl <- fl |> select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote, -TractFIPS, -GeographicLevel, -CategoryID)
#pivot data from long to wide format
fl_wide <- fl|> pivot_wider(id_cols=c(1:4, lat, long, UniqueID), names_from = MeasureId, values_from = c(Data_Value, PopulationCount))
#widening data reduced observations, maybe I can get all 500 cities?
us_wide <- latlong |> filter(Short_Question_Text %in% c("Mental Health", "Physical Health", "Obesity", "Health Insurance", "High Blood Pressure", "Binge Drinking") & GeographicLevel=="City" & DataValueTypeID=="AgeAdjPrv") |>
select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote, -TractFIPS, -GeographicLevel, -CategoryID) |>
pivot_wider(id_cols=c(1:4, lat, long, UniqueID), names_from = MeasureId, values_from = c(Data_Value, PopulationCount))2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
fl |> mutate(Short_Question_Text = factor(Short_Question_Text, c("Health Insurance", "Mental Health", "Physical Health", "Binge Drinking", "High Blood Pressure", "Obesity"))) |>
ggplot(aes(y=CityName, x=Short_Question_Text)) +
geom_tile(aes(fill=Data_Value)) +
scale_fill_distiller(palette = "Greens", direction=1) +
theme(legend.position = "bottom")+
labs(title="Health Measures in Florida Cities",
caption = "Source: CDC",
x= "Health Measure",
y= "City",
fill= "Prevalence in Adult Population (%)")3. Now create a map of your subsetted dataset.
First map chunk here
#load library
library(leaflet)
#create color palette for the markers
pal <- colorNumeric(palette="YlOrRd", domain=fl_wide$Data_Value_MHLTH)
#create map of florida
leaflet()|>
setView(lng = -81.5158, lat = 27.6648, zoom =6.4) |>
addProviderTiles("OpenStreetMap.HOT") |>
addCircleMarkers(data = fl_wide,
radius=sqrt(4^(fl_wide$Data_Value_ACCESS2/10))/2,
fillColor = ~pal(fl_wide$Data_Value_MHLTH),
stroke = F,
fillOpacity = 0.8)Assuming "long" and "lat" are longitude and latitude, respectively
Since widening the data decreased the observations, I decided to map the entire US.
#change color palette to match us range
pal <- colorNumeric(palette="YlOrRd", domain=us_wide$Data_Value_BPHIGH)
#create US map
leaflet()|>
setView(lng = -100.5348, lat = 38.7946, zoom =4) |>
addProviderTiles("OpenStreetMap.HOT") |>
addCircleMarkers(data = us_wide,
radius=sqrt(4^(us_wide$Data_Value_ACCESS2/10))/2,
fillColor = ~pal(us_wide$Data_Value_BPHIGH),
stroke = F,
fillOpacity = 0.6)Assuming "long" and "lat" are longitude and latitude, respectively
4. Refine your map to include a mouse-click tooltip
Refined map chunk here
#create popup text
popuphealth <- paste0(
"<strong>City: </strong>", us_wide$CityName, ", ", us_wide$StateAbbr, "<br>",
"<em>prevalence among adults of:</em><br>",
"<strong>No health insurance: </strong>", us_wide$Data_Value_ACCESS2, "% <br>",
"<strong>High blood pressure: </strong>", us_wide$Data_Value_BPHIGH, "% <br>"
)
#add popup to existing map code
leaflet()|>
setView(lng = -100.5348, lat = 38.7946, zoom =3) |>
addProviderTiles("OpenStreetMap.HOT") |>
addCircleMarkers(data = us_wide,
radius=sqrt(4^(us_wide$Data_Value_ACCESS2/10))/2,
fillColor = ~pal(us_wide$Data_Value_BPHIGH),
stroke = F,
fillOpacity = 0.6,
popup = popuphealth)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.
Across 500 cities in the US, I mapped circles of different sizes scaled to the rate of adults without health insurance. I mapped the colors of the markers to represent the rates of diagnosed high blood pressure among adults, with increasing rates being redder. The map shows an unsurprising increase in the rates of uninsured adults along the southern borders of Florida and Texas, where likely there are more undocumented immigrants. High blood pressure doesn’t seem to be strongly correlated to rates of insurance, instead showing a more distinct pattern geographically, with the highest rates of high blood pressure being around the southeast, but not as high in those more uninsured cities.