library(tidyverse)
library(tidyr)
setwd("C:/Users/mutho/Desktop/Fall 2023/Data 110/DATASETS")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")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
latlong2 <- cities500|>
mutate(GeoLocation = str_replace_all(GeoLocation, "[()]", ""))|>
separate(GeoLocation, into = c("lat", "long"), sep = ",", convert = TRUE)
head(latlong2)# 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 <- latlong2 |>
filter(StateDesc != "United States") |>
filter(Category == "Prevention") |>
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 AL Alabama Montgomery City BRFSS Prevention
2 2017 CA California Concord City BRFSS Prevention
3 2017 CA California Concord City BRFSS Prevention
4 2017 CA California Fontana City BRFSS Prevention
5 2017 CA California Richmond Census Tract BRFSS Prevention
6 2017 FL Florida Davie 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>
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
prevention <- latlong_clean |>
select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote)
head(prevention)# A tibble: 6 × 18
Year StateAbbr StateDesc CityName GeographicLevel Category UniqueID Measure
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 AL Alabama Montgome… City Prevent… 151000 Choles…
2 2017 CA California Concord City Prevent… 616000 Visits…
3 2017 CA California Concord City Prevent… 616000 Choles…
4 2017 CA California Fontana City Prevent… 624680 Visits…
5 2017 CA California Richmond Census Tract Prevent… 0660620… Choles…
6 2017 FL Florida Davie Census Tract Prevent… 1216475… Choles…
# ℹ 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 the cleaned “Prevention” dataset
1. Once you run the above code, filter this dataset one more time for any particular subset.
Filter chunk here
dmv_prevention <- prevention |>
select(-UniqueID, -TractFIPS) |>
filter(StateAbbr %in% c("DC", "MD", "VA"))
head(dmv_prevention)# A tibble: 6 × 16
Year StateAbbr StateDesc CityName GeographicLevel Category Measure
<dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 VA Virginia Alexandria Census Tract Prevention Taking medici…
2 2017 VA Virginia Lynchburg Census Tract Prevention Taking medici…
3 2017 VA Virginia Norfolk Census Tract Prevention Taking medici…
4 2017 VA Virginia Norfolk Census Tract Prevention Visits to doc…
5 2017 VA Virginia Norfolk Census Tract Prevention Cholesterol s…
6 2017 VA Virginia Richmond City Prevention Cholesterol s…
# ℹ 9 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
# PopulationCount <dbl>, lat <dbl>, long <dbl>, CategoryID <chr>,
# MeasureId <chr>, CityFIPS <dbl>, Short_Question_Text <chr>
2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.
First plot chunk here
library(viridis)Warning: package 'viridis' was built under R version 4.3.2
Loading required package: viridisLite
ggplot(dmv_prevention, aes(x = StateDesc, y = PopulationCount, color = MeasureId)) +
geom_point(alpha = 0.4) +
labs(title = "Prevention Measures Used in Different Cities in DMV by Population Count",
caption = "Source: USGS") +
facet_wrap(~MeasureId) +
scale_color_viridis_d("Measure") +
theme_bw()library(ggplot2)
# Creating a scatterplot with PopulationCount as point size
ggplot(dmv_prevention, aes(x = MeasureId, y = StateDesc)) +
geom_jitter(aes(size = PopulationCount)) +
labs(title = "DMV Prevention Measures with PopulationCount ",
x = "Prevention Measure Taken",
y = "State Description")3. Now create a map of your subsetted dataset.
library(leaflet)Warning: package 'leaflet' was built under R version 4.3.2
## Create the Leaflet map
dmv_map <- leaflet(data = dmv_prevention) |>
setView(lng = -77, lat = 37.9, zoom = 7) ### central coordinates
## Add OpenStreetMap tiles
dmv_map <- dmv_map |>
addTiles()
## Add circle markers for all coordinates
dmv_map <- dmv_map |>
addCircleMarkers(
lng = ~long, ### 'long' contains longitude information
lat = ~lat, ### 'lat' contains latitude information
radius = ~PopulationCount / 10000000, ### I adjusted the population count data to control the size
color = "blue",
fillOpacity = 0.0001,
popup = ~paste("City: ", CityName, "<br>Population: ", PopulationCount),
label = ~CityName ### displays city names as labels
)
## DMV
dmv_map4. Refine your map to include a mousover tooltip
Refined map chunk here
library(leaflet)
## Create the Leaflet map
dmv_map <- leaflet(data = dmv_prevention) %>%
setView(lng = -77, lat = 38.9, zoom = 12) ### central coordinates
## Add OpenStreetMap tiles
dmv_map <- dmv_map %>%
addTiles()
## Add circle markers for all coordinates
dmv_map <- dmv_map %>%
addCircleMarkers(
lng = ~long, ### 'long' contains longitude information
lat = ~lat, ### 'lat' contains latitude information
radius = ~PopulationCount / 10000000, ### Adjusted the population count data to control the size
color = "blue",
fillOpacity = 0.0001,
popup = ~paste("City: ", CityName, "<br>Population: ", PopulationCount),
label = ~CityName ### Displays city names as labels
)
## Add mouseover tooltip using addLabelOnlyMarkers
dmv_map2 <- dmv_map %>%
addLabelOnlyMarkers(
label = ~paste("City: ", CityName, "<br>Population: ", PopulationCount),
labelOptions = labelOptions(noHide = TRUE)
)Assuming "long" and "lat" are longitude and latitude, respectively
## Display the map
dmv_map25. Write a paragraph
In a paragraph, describe the plots you created and what they show.
There are three main visualizations that help understand different aspects of the DMV (District of Columbia, Maryland, and Virginia) region. The first scatterplot displays the Prevention Measures Used in Different Cities within the DMV by Population Count. Each point on the plot represents a city, and the x-axis represents the State Description while the y-axis shows the Population Count. Different colors indicate various Prevention Measures. This visualization offers insights into how population counts are distributed across cities in the DMV region and the associated prevention measures. The second scatterplot showcases the relationship between the Prevention Measure Taken (x-axis) and State Description (y-axis). The size of the points is determined by Population Count, providing a clear view of how different prevention measures are distributed among the various states in the DMV region. Additionally, the third and fourth visualizations are interactive maps of the DMV region with circle markers representing cities. The circle marker size corresponds to the Population Count of each city, while mouseover popups supply supplementary information, such as the city name and its population count. This map aids in visualizing the geographical distribution of population and prevention measures within the DMV region.