Week 12 Homework

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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)
cities500 <- read_csv("C:/Users/Hamat/Desktop/Hate Crimes Tutorial/500CitiesLocalHealthIndicators.cdc (1).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.
cities500 <- read_csv(file.choose())
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.
library(tidyr)
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>
latlong_clean <- latlong |>
  filter(StateDesc != "United States") |>
  filter(Data_Value_Type == "Crude prevalence") |>
  filter(Year == 2017) |>
  filter(StateAbbr == "MD") |>
  filter(Category == "Unhealthy Behaviors")

nrow(latlong_clean)
[1] 804
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"       
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 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… Curren…
2  2017 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… No lei…
3  2017 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… Obesit…
4  2017 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… No lei…
5  2017 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… Binge …
6  2017 MD        Maryland  Baltimore Census Tract    Unhealth… 2404000… Curren…
# ℹ 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>
smoking <- latlong_clean2 |>
  filter(Measure == "Current smoking among adults aged >=18 Years") |>
  filter(GeographicLevel == "Census Tract")

nrow(smoking)
[1] 200
ggplot(smoking, aes(x = Data_Value)) +
  geom_histogram() +
  labs(title = "Current Smoking in Baltimore Census Tracts",
       x = "Smoking prevalence (%)",
       y = "Number of census tracts")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 1 row containing non-finite outside the scale range
(`stat_bin()`).

MAP

library(leaflet)
Warning: package 'leaflet' was built under R version 4.6.2
leaflet(data = smoking) |>
  addTiles() |>
  addCircleMarkers(lng = ~long, lat = ~lat)

Map with clickable information

leaflet(data = smoking) |>
  addTiles() |>
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    popup = ~paste0("Smoking prevalence: ", Data_Value, "%")
  )

What the plots show

The histogram shows current smoking estimates across Baltimore census tracts in 2017. The estimates range from 8.8% to 38.4%. The map also shows where the tracts are, and clicking a point displays its estimate. Together, the plots show that smoking rates varied across Baltimore.