#Interactive Graph #1 with Numeric Variable
#| warning: false
#fixing up states data to match death data formatting of state names
states <- states |>
mutate(name = str_to_lower(name))
#begin filtering and mutating death data
int_death_data <- death_data |>
clean_names() |>
select(year, cause_name, state, deaths) |>
mutate(state = str_to_lower(state)) |>
filter(!(state %in% c("alaska", "hawaii", "united states"))) |>
filter(cause_name %in% c("Heart disease"), year == 2017) |>
#join death data with death data to get geometry column
right_join(states, by = c("state" = "name")) |>
rename(name = state)
#geometry column not recognized, use old sf function
int_death_data2 <- sf::st_as_sf(int_death_data) |>
filter(!(name %in% c("alaska", "hawaii", "puerto rico"))) |>
mutate(name = str_to_title(name))
#defining color palette
pal <- colorNumeric(palette = "PuRd", domain = c(0, 65000))
#leaflet plot
leaflet(int_death_data2) |>
setView(-96, 37.8, 3) |>
addTiles() |>
addPolygons(
weight = 1,
opacity = 1,
dashArray = "3",
fillOpacity = 0.8,
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
popup = ~paste(name, "</br>", "Amount of deaths due to heart disease: ", deaths),
color = ~pal(deaths)) |>
addLegend(pal = pal, values = ~int_death_data2$deaths,
opacity = 0.7, title = "Number of Deaths", position = "bottomright") |>
addControl("<h4>Deaths Due To Heart Disease in the U.S in 2017.</h4>",
position = "topright",
className = "map-title") |>
addControl("<h6>Data Source: data.gov.</h6>",
position = "bottomleft",
className = "map-title")interactive-mini1
#Graph #2 Interactive Categorical
#| warning: false
states <- states |>
mutate(name = str_to_lower(name))
int_cat_death_data <- death_data |>
#cleaning col names and selecting cols
clean_names() |>
select(year, cause_name, state, deaths) |>
mutate(state = str_to_lower(state)) |>
#filtering cols for join
filter(!(state %in% c("alaska", "hawaii", "united states"))) |>
filter(cause_name %in% c("Alzheimer's disease", "Stroke"), year == 2017) |>
#pivot so no more two rows per state - tidy data
pivot_wider(
names_from = cause_name,
values_from = deaths
) |>
#get counts and condition for cat var
clean_names() |>
count(state, stroke, alzheimers_disease) |>
mutate(death_type = ifelse(stroke > alzheimers_disease, "Stroke", "Alzheimer's")) |>
#right join to get states data
right_join(states, by = c("state" = "name")) |>
rename(name = state)
#fixes error about not having my geometry column recognized
int_cat_death_data2 <- sf::st_as_sf(int_cat_death_data) |>
filter(!(name %in% c("alaska", "hawaii", "puerto rico"))) |>
mutate(name = str_to_title(name))
pal <- colorFactor("PRGn", int_cat_death_data2$death_type)
leaflet(int_cat_death_data2) |>
setView(-95, 37.8, 3) |>
addTiles() |>
addPolygons(
weight = 1,
opacity = 1,
dashArray = "3",
fillOpacity = 0.8,
highlightOptions = highlightOptions(
weight = 5,
color = "#666", # state outline darkness
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
color = ~pal(int_cat_death_data2$death_type),
popup = ~paste(name, "</br>", "Cause of Death: ", death_type)) |>
addLegend(pal = pal, values = ~int_cat_death_data2$death_type,
opacity = 0.7, title = "Cause of Deaths" , position = "bottomright") |>
addControl("<h4>Greater Cause of Deaths Between Stroke or Alzheimer's in the U.S.</h4>",
position = "topright",
className = "map-title") |>
addControl("<h6>Data Source: data.gov.</h6>",
position = "bottomleft",
className = "map-title")library(tidyverse)
library(maps)
library(viridis)
library(janitor) # r tip of day??
library(leaflet)
library(htmltools)
library(glue)
library(sf)
#data for proj
death_data <- read_csv("~/SDS264_F24/SDS264/Data/death.csv")
#data for map
us_states <- map_data("state")
head(us_states)
#sf
states <- read_sf("https://rstudio.github.io/leaflet/json/us-states.geojson")
class(states)
states long lat group order region subregion
1 -87.46201 30.38968 1 1 alabama <NA>
2 -87.48493 30.37249 1 2 alabama <NA>
3 -87.52503 30.37249 1 3 alabama <NA>
4 -87.53076 30.33239 1 4 alabama <NA>
5 -87.57087 30.32665 1 5 alabama <NA>
6 -87.58806 30.32665 1 6 alabama <NA>
[1] "sf" "tbl_df" "tbl" "data.frame"
Simple feature collection with 52 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -188.9049 ymin: 17.92956 xmax: -65.6268 ymax: 71.35163
Geodetic CRS: WGS 84
# A tibble: 52 × 4
id name density geometry
<chr> <chr> <dbl> <MULTIPOLYGON [°]>
1 01 Alabama 94.6 (((-87.3593 35.00118, -85.60667 34.98475…
2 02 Alaska 1.26 (((-131.602 55.11798, -131.5692 55.28229…
3 04 Arizona 57.0 (((-109.0425 37.00026, -109.048 31.33163…
4 05 Arkansas 56.4 (((-94.47384 36.50186, -90.15254 36.4963…
5 06 California 242. (((-123.2333 42.00619, -122.3789 42.0116…
6 08 Colorado 49.3 (((-107.9197 41.00391, -105.729 40.99843…
7 09 Connecticut 739. (((-73.05353 42.03905, -71.79931 42.0226…
8 10 Delaware 464. (((-75.41409 39.80446, -75.5072 39.68396…
9 11 District of Columbia 10065 (((-77.03526 38.99387, -76.90929 38.8952…
10 12 Florida 353. (((-85.49714 30.99754, -85.00421 31.0030…
# ℹ 42 more rows