Seeding a GIS with R notebook.

First touched on this from the R 251 class. see R_phw251_bonus_problem_set.html

#ref choropleth

^ tin zink ~/tin-gh/PHW251_Fall2022/problem sets/problem set bonus ^> wc problem_set_bonus.Rmd

library(pacman)
p_load(tidyverse)
p_load(plotly)
# ref example from plotly web site 
# https://plotly.com/r/choropleth-maps/

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
                           "Fruits", total.fruits, "Veggies", total.veggies,
                           "<br>", "Wheat", wheat, "Corn", corn))

fig <- plot_geo(df, locationmode = 'USA-states')
fig <- fig %>% add_trace(
    z = ~total.exports, text = ~hover, locations = ~code,
    color = ~total.exports, colors = 'Purples'
  )
fig <- fig %>% colorbar(title = "Millions USD")
fig <- fig %>% layout(
    title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)'
  )

fig

PHW251_bonus_problem_set_Q14 (https://github.com/tin6150/PHW251_Fall2022/blob/main/problem%20sets/problem%20set%20bonus/problem_set_bonus.Rmd)[https://github.com/tin6150/PHW251_Fall2022/blob/main/problem%20sets/problem%20set%20bonus/problem_set_bonus.Rmd]

suicide_CSV_file="https://raw.githubusercontent.com/tin6150/PHW251_Fall2022/main/problem%20sets/problem%20set%20bonus/data/Suicide%20Mortality%20by%20State.csv"

# data pulled from CDC website described above 
#df_suicide <- read_csv("data/Suicide Mortality by State.csv") %>%
df_suicide <- read_csv(suicide_CSV_file) %>%
    filter(YEAR == 2018)
## Rows: 300 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): STATE, NAME, URL
## dbl (2): YEAR, RATE
## num (1): DEATHS
## 
## ℹ 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.
## add hover pop up text as extra col of data to DF
df_suicide$hover = with(
  df_suicide,
  paste( NAME, '<br>',
         'Death Rate: ', RATE,    '<br>',
         'Death: '     , DEATHS,  '<br>'
         ) 
)

map_q14 = plot_ly(df_suicide,
        type="choropleth",
        locationmode = "USA-states") %>%
  layout(geo=list(scope="usa"))   %>% add_trace(
    z            = ~RATE
    , text       = ~hover
    , locations  = ~STATE
    , color      = ~RATE
    , colors     = 'Purples'
  )  %>%   
  layout(      
    title = paste(
      "Suicide Mortality by State in 2018"
      , "<br> The number of deaths per 100,000 total population"
) )


map_q14 # map renders well in Rstudio, but not to pdf.