library(tidyverse)
library(tidyr)
setwd("C:/Users/jfgam/Downloads/Data 110")
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 == "TX") |>
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 TX Texas Houston Census Tract BRFSS Unhealthy Beha…
2 2017 TX Texas Houston Census Tract BRFSS Unhealthy Beha…
3 2017 TX Texas Houston Census Tract BRFSS Unhealthy Beha…
4 2017 TX Texas Arlington City BRFSS Unhealthy Beha…
5 2017 TX Texas Arlington City BRFSS Unhealthy Beha…
6 2017 TX Texas Arlington Census Tract BRFSS Unhealthy Beha…
# ℹ 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 TX Texas Houston Census Tract Unhealth… 4835000… Curren…
2 2017 TX Texas Houston Census Tract Unhealth… 4835000… Obesit…
3 2017 TX Texas Houston Census Tract Unhealth… 4835000… Obesit…
4 2017 TX Texas Arlington City Unhealth… 4804000 Curren…
5 2017 TX Texas Arlington City Unhealth… 4804000 Obesit…
6 2017 TX Texas Arlington Census Tract Unhealth… 4804000… 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.
The chunk filtering was done previously in the last 2 chunks of code given for example to make the transition easier, that way the whole filtering process isn’t needlessly redone.
Now create a subset data
texas_data <- latlong_clean2 |>
slice_head(n = 500)
nrow(texas_data)[1] 500
Focus on the # of observations in 2017
texas_data <- latlong |>
filter(StateAbbr == "TX",
Year == 2017,
Data_Value_Type == "Crude prevalence") |>
arrange(desc(CityName == "Dallas")) |>
slice_head(n = 500)
nrow(texas_data)[1] 500
head(texas_data)# A tibble: 6 × 25
Year StateAbbr StateDesc CityName GeographicLevel DataSource Category
<dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 TX Texas Dallas Census Tract BRFSS Prevention
2 2017 TX Texas Dallas Census Tract BRFSS Prevention
3 2017 TX Texas Dallas Census Tract BRFSS Unhealthy Behav…
4 2017 TX Texas Dallas Census Tract BRFSS Prevention
5 2017 TX Texas Dallas Census Tract BRFSS Health Outcomes
6 2017 TX Texas Dallas 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>
Focus on select variables
texas_data <- texas_data |>
select(CityName, Measure, Data_Value, lat, long,
StateAbbr, Year, Category)
head(texas_data)# A tibble: 6 × 8
CityName Measure Data_Value lat long StateAbbr Year Category
<chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <chr>
1 Dallas "Current lack of hea… 44.2 32.7 -96.9 TX 2017 Prevent…
2 Dallas "Current lack of hea… 39.6 32.9 -96.7 TX 2017 Prevent…
3 Dallas "Binge drinking amon… 18.2 33.0 -96.8 TX 2017 Unhealt…
4 Dallas "Taking medicine for… 81 32.8 -96.8 TX 2017 Prevent…
5 Dallas "Cancer (excluding s… 7.6 32.9 -96.9 TX 2017 Health …
6 Dallas "Visits to doctor fo… 67.9 32.9 -96.7 TX 2017 Prevent…
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
Create bar graph
ggplot(texas_data, aes(x = "Health_Outcomes", y = "Data_Value")) +
labs(title = "Health Indicators in Texas",
x = "Health Measure",
y = "Data Value") +
theme_minimal() 3. Now create a map of your subsetted dataset.
First map chunk here
Acquire all the texas-smoking records
smoking_data <- latlong |>
filter(StateAbbr == "TX",
Year == 2017,
Data_Value_Type == "Crude prevalence",
str_detect(Measure, regex("smoking", ignore_case = TRUE)))
nrow(smoking_data)[1] 3162
library(leaflet)Warning: package 'leaflet' was built under R version 4.5.3
leaflet(smoking_data) |>
addTiles() |>
addCircleMarkers(
lng = ~long,
lat = ~lat,
radius = 6,
color = "blue",
fillOpacity = 0.7
)smoking_data <- latlong |>
filter(StateAbbr == "TX",
Year == 2017,
Data_Value_Type == "Crude prevalence",
str_detect(Measure, regex("smoking", ignore_case = TRUE))) |>
slice_head(n = 800)4. Refine your map to include a mouse-click tooltip
Refined map chunk here
Refine the map to make it more detailed and interactive
leaflet(smoking_data) |>
addTiles() |>
addCircleMarkers(
lng = ~long,
lat = ~lat,
radius = 6,
color = "red",
fillOpacity = 0.7,
popup = ~paste(
"City:", CityName,
"<br>Measure:", Measure,
"<br>Prevalence:", Data_Value, "%"
)
)5. Write a paragraph
In a paragraph, describe the plots you created and the insights they show.
The plots that are presented here is regarding the state of Texas and focusing on measuring smoking among adults older than 18. Each dot contains the corresponding city and the prevalence of the smoking factor. Some patterns here are that since texas is such a big state, these smoking records tend to only appear in populated areas