library(tidyverse)
library(tidyr)
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")
data(cities500)Healthy Cities GIS Assignment
Spliting GeoLocation variables (lat, long) into two columns:
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>
Filtering the data set:
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>
Variables included:
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"
Removing variables that will not be used:
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>
1. Filtering Data, Subset with no more than 900 observations:
latlong_clean3 <- latlong_clean2 |> filter(GeographicLevel == "City")
head(latlong_clean3)# A tibble: 6 × 18
Year StateAbbr StateDesc CityName GeographicLevel Category UniqueID Measure
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 2017 CT Connecticut Danbury City Unhealt… 918430 Obesit…
2 2017 CT Connecticut Stamford City Unhealt… 973000 Binge …
3 2017 CT Connecticut New Hav… City Unhealt… 952000 Obesit…
4 2017 CT Connecticut Waterbu… City Unhealt… 980000 No lei…
5 2017 CT Connecticut New Bri… City Unhealt… 950370 No lei…
6 2017 CT Connecticut Hartford City Unhealt… 937000 No lei…
# ℹ 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>
nrow(latlong_clean3)[1] 32
2. Creating a plot of subsetted Data set:
library(dplyr)
library(ggplot2)
obesity_data <- filter(
latlong_clean3,
Measure == "Obesity among adults aged >=18 Years"
)
ggplot(obesity_data, aes(x = CityName, y = Data_Value_Type)) +
geom_col() +
coord_flip() +
labs(
title = "Adult Obesity Prevalence in Connecticut Cities",
x = "City",
y = "Obesity Prevalence (%)"
) +
theme_minimal()3. Creating a map of the subsetted data set:
library(ggplot2)
library(maps)
Attaching package: 'maps'
The following object is masked from 'package:purrr':
map
ct <- map_data("state", region = "connecticut")
ggplot() +
geom_polygon(data = ct, aes(long, lat, group = group)) +
geom_point(data = latlong_clean3, aes(long, lat)) +
labs(
title = "Electric Vehicle Rebate Locations in Connecticut",
x = "Longitude",
y = "Latitude"
)4. Refining to include a mouse-click tooltip:
library(ggplot2)
library(maps)
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
connecticut_map <- map_data("state", region = "connecticut")
p <- ggplot() +
geom_polygon(
data = connecticut_map,
aes(x = long, y = lat, group = group),
fill = "lightgray",
color = "white"
) +
geom_point(
data = latlong_clean3,
aes(
x = long,
y = lat,
text = paste(
"Latitude:", lat,
"Longitude:", long
)
),
size = 3
) +
coord_cartesian(
xlim = c(-73.8, -71.7),
ylim = c(40.9, 42.1)
) +
labs(
title = "Longitude and Latitude of City Locations in Connecticut",
x = "Longitude",
y = "Latitude"
) +
theme_minimal()Warning in geom_point(data = latlong_clean3, aes(x = long, y = lat, text =
paste("Latitude:", : Ignoring unknown aesthetics: text
ggplotly(p, tooltip = "tooltip")5. Description
The first visualization is a bar chart showing the prevalence of adult obesity in Connecticut cities in 2017. The chart makes it possible to compare the obesity prevalence of different cities and identify differences between them. The second visualization is a map showing the geographic locations of the cities in the data set. Each point represents a city, allowing the data to be viewed geographically. Together, the bar chart and map provide two different ways to examine the same data: the bar chart focuses on comparing the values between cities, while the map shows where those cities are located geographically.