library(tidyverse)
library(tidyr)
library(leaflet)
library(knitr)
setwd("~/Library/Mobile Documents/com~apple~CloudDocs/Data110")
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)
head(latlong_clean)# 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 Unhealthy Beh…
4 2017 CA California Indio Census Tract BRFSS Health Outcom…
5 2017 CA California Inglewood Census Tract BRFSS Health Outcom…
6 2017 CA California Lakewood City BRFSS Unhealthy Beh…
# ℹ 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 CA California Hawthorne Census Tract Health … 0632548… Arthri…
2 2017 CA California Hawthorne City Unhealt… 632548 Curren…
3 2017 CA California Hayward City Unhealt… 633000 Obesit…
4 2017 CA California Indio Census Tract Health … 0636448… Arthri…
5 2017 CA California Inglewood Census Tract Health … 0636546… Diagno…
6 2017 CA California Lakewood City Unhealt… 639892 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 “Prevention” is a manageable dataset now.
For your assignment, work with a cleaned dataset.
1. Once you run the above code and learn how to filter in this format, filter this dataset however you choose so that you have a subset with no more than 900 observations.
Create a filtered subset </= 900 observations.
’Filter chunks for the “State Description”, where I selected the DMV to focus on, and the “Short Question Text”, where I chose the condition obesity to focus on. To do this, I created a new tibble titled “latlongDMV” for my narrowed dataset with ~900 observations.
latlongDMV <- latlong |>
filter(StateDesc %in% c("Maryland", "Virginia", "District of C")) |>
filter(Short_Question_Text =="Obesity")
head(latlongDMV)# A tibble: 6 × 25
Year StateAbbr StateDesc CityName GeographicLevel DataSource Category
<dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 VA Virginia Roanoke Census Tract BRFSS Unhealthy…
2 2017 DC District of C Washington Census Tract BRFSS Unhealthy…
3 2017 DC District of C Washington Census Tract BRFSS Unhealthy…
4 2017 DC District of C Washington Census Tract BRFSS Unhealthy…
5 2017 DC District of C Washington Census Tract BRFSS Unhealthy…
6 2017 DC District of C Washington Census Tract BRFSS Unhealthy…
# ℹ 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>
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
Create a density curve plot using the data subset previously filtered.
First plot chunk here using a faceted density curve to compare Washington DC, Baltimore Maryland, and Virginia. The x-axis contains the percentages of the population under the “Obese” category, and the y-axis provides the frequency of populations within that percentage.
plot1 <- ggplot(data=latlongDMV, aes(x=Data_Value, fill = StateDesc))+
geom_density()+
facet_wrap(~StateDesc)+
theme_bw() +
labs(title="Obesity Rates of the DMV in 2017",
x="Percent of the Population",
y="Obesity Rates in the DMV",
caption = "Source: CDC")
plot1Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_density()`).
3. Now create a map of your subsetted dataset.
Map the data subset (latlongDMV) onto the “WorldStreetMap” to visualize the data geographically,
First map chunk here: I used “leaflet” to paste the interactive map into R. I set the latitude and longitude based on the coordinates found with an online search. I used “addcircles”, so the data could be plotted as points on the map.
leaflet() |>
setView(lng = -77, lat = 38, zoom =6.5) |>
addProviderTiles("Esri.WorldStreetMap") |>
addCircles(
data = latlongDMV,
radius = latlongDMV$Data_Value^2
)Assuming "long" and "lat" are longitude and latitude, respectively
4. Refine your map to include a mouse-click tooltip
Improve your plotted map from the previous design.
Refined map chunk here: I decided to focus in on Virginia, instead of the whole DMV because I wanted to learn more detailed information about possible patterns that exist. I chose to select the variables “CityName”, “Data_Value”, “PopulationCount”, and “TractFIPS” to be displayed when a data point (circle) is clicked on with the mouse.
popobese <- paste0(
"<b>City Name: </b>", latlongDMV$CityName, "<br>",
"<b>Obesity Rate: </b>", latlongDMV$Data_Value, "<br>",
"<b>Population Count: </b>", latlongDMV$PopulationCount, "<br>",
"<b>Population Tract FIPS: </b>", latlongDMV$TractFIPS, "<br>"
)Set the new settings for the refined map, so the viewer can easily understand the data.
leaflet() |>
setView(lng = -77.0369, lat = 38.9072, zoom =11.5) |>
addProviderTiles("Esri.WorldStreetMap") |>
addCircles(
data = latlongDMV,
radius = latlongDMV$Data_Value^2,
color = "#8932a8",
fillColor = "#dfc1f5",
fillOpacity = 0.35,
popup = popobese
)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.
Paragraph Explanation:
For my initial plot, I created a faceted density curve to compare the obesity rates of Baltimore MD, Virginia, and Washington DC. I made sure my tibble was limited to 900 observations by narrowing my focus to just obesity rates in the three locations, and used the variables “Short Question Text”, “StateDesc”, and “Data Value”. I did not have to filter for the year because the data only existed in 2017. I created my plotted map using the “Street Map” from the ‘Japan Earthquakes’ tutorial, and was able to model the visualization after it. I found the specific latitude and longitudinal coordinates from an internet search, and it proved to be highly effective despite my visualizations encompassing general areas. My rough map displayed the DMV as a whole, and was much less refined, whereas in my improved map, I decided to focus solely on Virginia. I edited the zoom, and changed the colors of the circles for aesthetic, and easy viewing purposes. From these visualizations, it is easy to learn that Virginia, and the wealthier (possibly the most heavily gentrified) areas of DC had the lowest rates of obesity. In addition, it was sad to see the larger percent of the Baltimore population as obese, and the more frequent large circles on the map in Baltimore, compared to DC, or Virginia. Although disappointing, this data is not surprising due to the known connections between wealth and access to healthcare and grocery stores. Many lower-income food deserts (neigborhoods with no grocery stores withing an accessible radius) still contain unhealthy fast-food or corner-stores that drive obesity, and other chronic disease rates upwards.