Healthy Cities Assignment

Author

Su Thet Hninn

Load the libraries and set the working directory

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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
library(tidyr)
library(leaflet)
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(knitr)
setwd("/Users/hlinethitzinwai/Documents/1 - College/DATA 110/2 - Dataset/Healthy Cities /Local")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")
Rows: 810103 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

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(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>
md <- prevention |>
  filter(StateAbbr=="MD")
head(md)
# A tibble: 6 × 18
   Year StateAbbr StateDesc CityName  GeographicLevel Category  UniqueID Measure
  <dbl> <chr>     <chr>     <chr>     <chr>           <chr>     <chr>    <chr>  
1  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Chole…
2  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
3  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
4  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Curre…
5  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Curre…
6  2017 MD        Maryland  Baltimore Census Tract    Preventi… 2404000… "Visit…
# ℹ 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

# filter the subset for the population taking high blood pressure medicine
BPMed_subset <- prevention %>%
  filter(MeasureId == "BPMED" & !is.na(Data_Value) & !is.na(long) & !is.na(lat) & !is.na(PopulationCount)) %>% #drop Na values
  filter(StateAbbr != "AK" & StateAbbr != "HI") # drop Alsaka and Hawaii
# Convert PopulationCount to numeric 
BPMed_subset$PopulationCount <- as.numeric(as.character(BPMed_subset$PopulationCount))
  1. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.

First plot chunk here

library(viridis)
Loading required package: viridisLite
# Check the range of PopulationCount 
summary(BPMed_subset$PopulationCount)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     50    2474    3645    7423    4980 8175133 
# Create ggplot visualization
ggplot(BPMed_subset, aes(x = long, y = lat, colour = Data_Value, size = PopulationCount)) +
  geom_point() +
  scale_colour_viridis(name = "Population Count",
                       alpha = 1,
                       begin = 0,
                       end = 1,
                       direction = 1,
                       option = "D") +
  scale_size_continuous(name = "Population",
                        breaks = c(50000, 100000, 200000, 500000, 1000000, 1500000),  # Define breaks for legend
                        labels = c("50k", "100k", "200k", "500k","1000k","1500k"),  # Labels for legend
                        range = c(1, 10)) +  # Adjust range for size scaling
  labs(title = "Distribution of high blood pressure medication usage",
       x = "City Longitude",
       y = "City Latitude",
       colour = "Population Count",
       caption = "Note: Alaska and Hawaii States are dropped from the dataset.") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 10),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5))

3. Now create a map of your subsetted dataset.

First map chunk here

library(rnaturalearth)

# Set up the base map layer
world_map <- map_data("world")
ggplot() +
  geom_polygon(
    data = world_map, 
    aes(x = long, y = lat, group = group), 
    fill = "lightgrey", 
    color = "white"
  ) +
  geom_point(
    data = BPMed_subset, 
    aes(x = long, y = lat, color = Data_Value, size = PopulationCount)
  ) +
  scale_color_gradient2(low = "white", high = "red", name = "BPMed Rate") +
  scale_size_continuous(name = "Population Count",
                        breaks = c(50000, 100000, 200000, 500000, 1000000, 1500000),  
                        labels = c("50k", "100k", "200k", "500k","1000k","1500k"),
                        range = c(1, 10)) +  # Adjust range for size scaling
  labs(
    title = "Distribution of high blood pressure medication usuage",
    x = "Longitude", 
    y = "Latitude"
  ) +
  xlim(
    min(BPMed_subset$long, na.rm = TRUE) - 5, 
    max(BPMed_subset$long, na.rm = TRUE) + 5
  ) +
  ylim(
    min(BPMed_subset$lat, na.rm = TRUE) - 5, 
    max(BPMed_subset$lat, na.rm = TRUE) + 5
  ) +
  # Themes and aesthetics
  theme_minimal() +
  theme(
    legend.position = "right",  
    legend.title = element_text(size = 10),  
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5)  
  ) +
  coord_fixed(1.3)

5. Write a paragraph

I developed three visualizations to analyze the prevalence of high blood pressure medication usage across various locations in the contiguous United States. Initially, I filtered the dataset to focus on populations using medication for high blood pressure, excluding Alaska and Hawaii due to their geographic distance and differing scales of longitude and latitude, which could distort static map visualizations. The first visualization employs ggplot to display cities across the USA, along with their latitude and longitude. The second visualization uses a static world map to depict the distribution of high blood pressure treatment rates. Using a color gradient, this map visually represents varying levels of treatment intensity, facilitating the identification of geographic areas with distinct medication usage rates. Additionally, I utilized leaflet to create an interactive map that enhances functionality by allowing users to click on cities. This interactive feature triggers popups displaying each city’s name alongside its specific rate of high blood pressure treatment, facilitating detailed exploration and analysis of regional health indicators.