library(tidyverse)
library(highcharter)
library(tidyr)
library(leaflet)
setwd('/Users/eabban/College Stuff/R Studio')
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(Measure == ">=18") |>
filter(StateAbbr == "CT") |>
filter(Category == "Unhealthy Behaviors")
head(latlong_clean)# A tibble: 0 × 25
# ℹ 25 variables: Year <dbl>, StateAbbr <chr>, StateDesc <chr>, CityName <chr>,
# GeographicLevel <chr>, DataSource <chr>, Category <chr>, 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>, …
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: 0 × 18
# ℹ 18 variables: Year <dbl>, StateAbbr <chr>, StateDesc <chr>, CityName <chr>,
# GeographicLevel <chr>, Category <chr>, UniqueID <chr>, Measure <chr>,
# 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)
my_clean <- latlong |>
filter(StateDesc != "United States") |>
select(
Year, StateAbbr, CityName, GeographicLevel,
Category, Measure, Short_Question_Text,
Data_Value, Low_Confidence_Limit, High_Confidence_Limit,
PopulationCount, lat, long,
MeasureId, CityFIPS, GeographicLevel
) |>
filter (Year == 2017) |>
filter (StateAbbr == "MT") |>
filter(str_detect(Measure, ">=18")) |>
#filter (CityName =="Billings") |>
filter(!is.na(Data_Value), !is.na(PopulationCount), !is.na(lat), !is.na(long), !is.na(StateAbbr))
#slice_sample(n = 900)
head(my_clean)# A tibble: 6 × 15
Year StateAbbr CityName GeographicLevel Category Measure Short_Question_Text
<dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 MT Billings City Unhealth… Binge … Binge Drinking
2 2017 MT Billings City Health O… Cancer… Cancer (except ski…
3 2017 MT Missoula City Health O… Chroni… COPD
4 2017 MT Billings Census Tract Preventi… Taking… Taking BP Medicati…
5 2017 MT Billings Census Tract Preventi… Taking… Taking BP Medicati…
6 2017 MT Billings Census Tract Health O… Diagno… Diabetes
# ℹ 8 more variables: Data_Value <dbl>, Low_Confidence_Limit <dbl>,
# High_Confidence_Limit <dbl>, PopulationCount <dbl>, lat <dbl>, long <dbl>,
# MeasureId <chr>, CityFIPS <dbl>
#city_health <- my_clean |>
#group_by(PopulationCount, Data_Value, Category, Year)
#mutate(Year = as.character(Year))2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
hchart(my_clean, "bubble",
hcaes(x = MeasureId , y = Data_Value,
size = PopulationCount,
color = Short_Question_Text)) |>
hc_title(text = "Montana health Outcome (Adults 18+)") |>
hc_subtitle(text = "By data value and measure id") |>
hc_xAxis(title = list(text = "Measure")) |>
hc_yAxis(title = list(text = "Data Value")) |>
hc_colorAxis(
stops = color_stops(colors = c("#1a9850", "#fee08b", "#d73027"))
) |>
hc_tooltip(
pointFormat = "<b>{point.CityName}, {point.StateAbbr}</b><br>
Question: {point.Short_Question_Text}<br>
Category: {point.Category}"
) 3. Now create a map of your subsetted dataset.
First map chunk here
leaflet(my_clean) |>
#setView(long, lat, zoom =5.5) |>
addTiles() |>
addCircleMarkers(
lng = ~long,
lat = ~lat,
radius = ~sqrt(PopulationCount/100),
color = "green",
fillOpacity = 0.6,
weight = 1,
popup = ~paste(
"<strong>", CityName, ", ", StateAbbr, "</strong><br>",
"Population: ", format(PopulationCount, big.mark = ",")
))4. Refine your map to include a mouse-click tooltip
Refined map chunk here
leaflet(my_clean) |>
addProviderTiles("CartoDB.Positron") |>
addCircleMarkers(
lng = ~long,
lat = ~lat,
radius = ~sqrt(PopulationCount/80),
color = ~MeasureId,
fillOpacity = 0.6,
popup = ~paste0(
"<b>", CityName, ", ", StateAbbr, "</b><br>",
"Value: ", Data_Value, "%", "<br>",
"Population: ", format(PopulationCount, big.mark = ","), "<br>",
"Category: ", Category, "<br>",
"Issues: ", MeasureId, "<br>",
"Geographic Level: ", GeographicLevel
),
label = ~CityName,
clusterOptions = markerClusterOptions()
)5. Write a paragraph
In a paragraph, describe the plots you created and the insights they show.
I began by cleaning the latlong dataset, sampling the state Montana (2017), removing “United States,” and selecting the relevant columns. I then filtered the data to have only health measures for adults 18 and older using str_detect(), and removed NA’s from TractFIPS, Data_Value, PopulationCount, and the coordinate column. I built a Highcharter bubble chart plotting cities by their Data Value and Measure Id, the bubble size represents the population, with a red-yellow-green color legend. I also created a basic Leaflet map with circle, with the size being the population, and popups showing the city name and population. Finally, I made a better version of the map by switching to a CartoDB Positron basemap, coloring by MeasureId, and adding more information to the popups.