R/obtn_plot_choropleth_map.R

Defines functions obtn_plot_choropleth_map

Documented in obtn_plot_choropleth_map

#' Make Choropleth Map
#'
#' @param measure_to_plot
#' @param obtn_year
#'
#' @return
#' @export
#'
#' @examples
obtn_plot_choropleth_map <- function(obtn_year, measure_to_plot, plot_width = 4.3684, plot_height = 3.25) {

  obtn_data_by_measure_filtered <- obtn_data_by_measure %>%
    dplyr::filter(year == obtn_year) %>%
    dplyr::filter(measure == measure_to_plot) %>%
    dplyr::filter(geography %in% obtn_oregon_counties) %>%

    # I'm recalculating the tertile stuff because I couldn't get it to work in import-data.R
    dplyr::mutate(tertile_numeric = santoku::chop_equally(rank, 3)) %>%
    dplyr::mutate(tertile_numeric = as.numeric(tertile_numeric)) %>%
    dplyr::mutate(tertile_text = dplyr::case_when(
      tertile_numeric == 1 ~ "Top third",
      tertile_numeric == 2 ~ "Middle third",
      tertile_numeric == 3 ~ "Bottom third"
    )) %>%
    dplyr::mutate(tertile_text = tidyr::replace_na(tertile_text, "ID")) %>%
    dplyr::mutate(tertile_text = factor(tertile_text, levels = c("Top third",
                                                          "Middle third",
                                                          "Bottom third",
                                                          "No college",
                                                          "ID")))

  obtn_data_by_measure_geospatial <- dplyr::left_join(obtn_data_by_measure_filtered, obtn_boundaries_oregon_counties, by = "geography") %>%
    sf::st_as_sf()

  obtn_data_by_measure_geospatial %>%
    count(tertile_text)

  ggplot2::ggplot(obtn_data_by_measure_geospatial) +
    ggplot2::geom_sf(ggplot2::aes(fill = tertile_text),
                     color = "white",
                     size = .5) +
    ggplot2::coord_sf(datum = NA) +
    ggplot2::scale_fill_manual(values = obtn_choropleth_colors) +
    ggplot2::theme_void() +
    ggplot2::theme(text = ggplot2::element_text(family = "Calibri",
                                                size = 10),
                   legend.box.margin = ggplot2::margin(10,10,10,10),
                   legend.position = "bottom") +
    ggplot2::scale_x_continuous(expand = c(0, 0)) +
    ggplot2::scale_y_continuous(expand = c(0, 0)) +
    ggplot2::labs(fill = NULL)

  measure_to_plot_name <- stringr::str_glue("Choropleth {measure_to_plot}")

  obtn_save_plot(obtn_year, measure_to_plot_name, "Oregon", plot_width, plot_height)

}

# obtn_plot_choropleth_map(2022, "Child Poverty")

# temp <-obtn_plot_choropleth_map(2022, "Child Poverty")
# temp %>%
#   count(tertile_text)
rfortherestofus/obtn documentation built on Nov. 13, 2022, 7:25 p.m.