library(tidyverse)
library(tidyr)
library(leaflet)
library(ggplot2)
#setwd("C:/Users/cassidystauffer/Documents/MCCC/Data Vis 110/Data Sets")
setwd("~/Documents/MCCC/Data Vis 110/Data Sets")
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 “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 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.
Filter chunk here (you may need multiple chunks)
#filter the data by binge drinking
ct_binge_drinking <- filter(latlong_clean2, MeasureId == "BINGE")
view(ct_binge_drinking)ct_binge_drinking_clean <- ct_binge_drinking[, c(2,4,10,11,12,13)]
ct_binge_drinking_clean# A tibble: 228 × 6
StateAbbr CityName Data_Value PopulationCount lat long
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 CT Waterbury 15.8 3928 41.6 -73.0
2 CT Norwalk 17.2 3898 41.1 -73.4
3 CT Stamford 16.1 4896 41.1 -73.6
4 CT Danbury 17.7 5305 41.4 -73.4
5 CT Bridgeport 16.3 2836 41.2 -73.2
6 CT Stamford 18.6 5713 41.0 -73.5
7 CT Danbury 17.7 5025 41.4 -73.4
8 CT Stamford 17.4 122643 41.1 -73.6
9 CT Stamford 16.5 6303 41.0 -73.5
10 CT Hartford 10.4 1622 41.8 -72.7
# ℹ 218 more rows
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
# non map plot, binge drinking in CT
ggplot(ct_binge_drinking_clean) +
geom_bar(aes(x = CityName))ct_binge_drinking_clean2 <- ct_binge_drinking_clean |>
group_by(CityName, Data_Value) |>
summarise(
avg_pop = mean(PopulationCount),
.groups = "drop")
ggplot(ct_binge_drinking_clean2, aes(x=Data_Value, y=avg_pop, color = CityName)) +
geom_point(alpha = 0.4) +
labs(title = "Binge Drinking in CT by City",
caption = "Source: CDC") +
facet_wrap(~CityName) +
scale_color_viridis_d("City") +
theme_bw()#long and lat of CT
ct_lon <- -72.7
ct_lat <- 41.63. Now create a map of your subsetted dataset.
First map chunk here
leaflet() |>
setView(lng = -72.7, lat = 41.6, zoom = 6) |>
addProviderTiles("Esri.WorldStreetMap")|>
addCircles( data = ct_binge_drinking,
radius = ct_binge_drinking_clean2$avg_pop)Assuming "long" and "lat" are longitude and latitude, respectively
4. Refine your map to include a mouse-click tooltip
class("long")[1] "character"
Refined map chunk here
#convert lat and long to numeric
ct_binge_drinking_clean3 <- ct_binge_drinking_clean %>%
mutate(across(c("lat", "long"), as.numeric))library(leaflet)
leaflet() |>
setView(-72.7, lat = 41.6, zoom = 10) |>
addProviderTiles("Esri.WorldStreetMap") |>
addCircles(
data = ct_binge_drinking_clean3,
lng = ~long,
lat = ~lat,
#radius = ~PopulationCount,
popup = ~paste0(CityName, Data_Value)
)5. Write a paragraph
This leaflet looks at the data_value or percent of adults who binge drink in Connecticut’s biggest cities. In order to create this GIS plot, I further filtered and cleaned the data until I only had instances of binge drinking in cities in Connecticut and extracted the following variables: state, city, population, data_value, latitude, and longitude. I then followed the GIS Exploration tutorial from class, to create a version with this data.