library(tidyr)Healthy Cities GIS Assignment
Load the libraries and set the working directory
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ purrr 1.2.2
✔ forcats 1.0.1 ✔ readr 2.2.0
✔ ggplot2 4.0.3 ✔ stringr 1.6.0
✔ lubridate 1.9.5 ✔ tibble 3.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv",n_max=900)Rows: 900 Columns: 24
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (17): StateAbbr, StateDesc, CityName, GeographicLevel, DataSource, Categ...
dbl (6): Year, Data_Value, Low_Confidence_Limit, High_Confidence_Limit, Cit...
num (1): PopulationCount
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
The GeoLocation variable has (lat, long) format
Split GeoLocation (lat, long) into two columns: lat and long
library(tidyverse)
latlong <- cities500|>
mutate(
GeoLocation = str_replace_all(GeoLocation, "[()]", ""))|>
tidyr::separate(
GeoLocation,
into = c("lat", "long"),
sep = ",",
convert=TRUE)names(cities500) [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] "GeoLocation" "CategoryID"
[21] "MeasureId" "CityFIPS"
[23] "TractFIPS" "Short_Question_Text"
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_clean2 <-latlong|>
filter(StateDesc!= "United States") |>
filter(Data_Value_Type == "Crude prevalence") |>
filter(Year == 2017) |>
filter(StateAbbr == "CT") |>
filter(Category == "Prevention")What variables are included? (can any of them be removed?)
names(cities500) <- gsub(" ",".",names(cities500))
names(cities500) <- tolower(names(cities500))
head(cities500)# A tibble: 6 × 24
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…
# ℹ 17 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>, geolocation <chr>, categoryid <chr>,
# measureid <chr>, cityfips <dbl>, tractfips <dbl>, short_question_text <chr>
Remove the variables that will not be used in the assignment
latlong_clean2 <- latlong |>
dplyr::select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote)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)
my_subset<- latlong |>
filter(StateAbbr=="CT")
nrow(my_subset)[1] 14
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
# non map plot
ggplot(my_subset,aes(x=CityName,y=Data_Value, fill= Measure))+
geom_col(position="dodge") +
theme_minimal() +
labs(title= "Health indicator across CT Cities") x="City"
y="Prevalance"3. Now create a map of your subsetted dataset.
First map chunk here
# leaflet()
ggplot(my_subset, aes(x=long, y=lat, clor=Data_Value)) +
geom_point(size=3) +
scale_color_viridis_c() +
coord_quickmap() +
theme_light() +
labs(title= "Geographic Distribution of Health Distributions") x='Longitude'
y='Latitude'
color="Prevalence"4. Refine your map to include a mouse-click tooltip
Refined map chunk here
map_plot <- ggplot(latlong_clean2, aes(
x = long,
y = lat,
color = Data_Value,
text = paste0("City: ", CityName, "<br>",
"Measure: ", Measure, "<br>",
"Value: ", Data_Value, "%")
)) +
geom_point(size = 2, alpha = 0.8) +
scale_color_viridis_c() +
coord_quickmap() +
theme_light() +
labs (
title = "Interactive Map of Health Indicators",
x = 'Longitude',
y = 'Latitude',
color = "Prevalence (%)")5. Write a paragraph
In a paragraph, describe the plots you created and what they show.
The plots I have created are from the 500 cities dataset. The boxplot continues highlights the cities that highest preventative measures. While, the map plot showed regional trends overeall. Both of the graph demonstrate statistical and spatial outputs.