R/obtn_plot_alice.R

Defines functions obtn_plot_alice

Documented in obtn_plot_alice

#' Function to make financial hardship (ALICE) plot
#'
#' @param obtn_year
#' @param county_to_plot
#' @param plot_width
#' @param plot_height
#'
#' @return
#' @export
#'
#' @examples
obtn_plot_alice <- function(obtn_year, county_to_plot, plot_width = 3.25, plot_height = .9) {

  obtn_alice_data_filtered <- obtn_alice_data %>%
    dplyr::filter(geography == county_to_plot) %>%
    dplyr::filter(year == obtn_year) %>%
    dplyr::mutate(level_text = dplyr::case_when(
      level == "Below Poverty Level" ~ "Below Poverty",
      level == "Below ALICE Threshold" ~ "Below ALICE"
    )) %>%
    dplyr::mutate(value_formatted = dplyr::case_when(
      level == "Above ALICE Threshold" ~ NA_character_,
      TRUE ~ scales::percent(value, 1)
    ))

  pct_below_poverty_and_alice <- obtn_alice_data_filtered %>%
    dplyr::filter(level != "Above ALICE Threshold") %>%
    dplyr::mutate(value = scales::percent(value)) %>%
    dplyr::mutate(value = readr::parse_number(value)) %>%
    dplyr::summarize(value = sum(value) / 100) %>%
    dplyr::pull(value)

  pct_below_poverty_and_alice_raw <- obtn_alice_data_filtered %>%
    dplyr::filter(level != "Above ALICE Threshold") %>%
    dplyr::summarize(value = sum(value)) %>%
    dplyr::pull(value)


  y_hl <- 1.3
  y_tails <- 1.0

  padding <- 0.0025

  x_start <- 0.005
  x_ends <- pct_below_poverty_and_alice_raw - padding


  plot <- ggplot2::ggplot(
    obtn_alice_data_filtered,
    ggplot2::aes(1, value,
                 fill = level,
                 label = value_formatted
    )
  ) +
    ggplot2::geom_col(
      color = "white",
      width = 0.5,
    ) +

    # Labels on each bar
    ggplot2::geom_text(
      position = ggplot2::position_stack(vjust = 0.5),
      color = c("white", "black", "transparent"),
      fontface = "bold",
      family = "Calibri"
    ) +

    # Percent labels below bars
    ggplot2::geom_text(
      ggplot2::aes(0.5, value,
                   label = stringr::str_wrap(level_text, 10)),
      position = ggplot2::position_stack(vjust = 0.5),
      color = c(tfff_dark_green, "#91a371", "white"),
      size = 3,
      hjust = 0.5,
      lineheight = 0.75,
      family = "Calibri"
    ) +
    ggplot2::scale_fill_manual(values = c(tfff_medium_gray, tfff_light_green, tfff_dark_green)) +
    ggplot2::scale_y_continuous(expand = c(0.01, 0)) +
    ggplot2::theme_void() +
    ggplot2::theme(legend.position = "none") +
    ggplot2::coord_flip(clip = "off") +
    ggplot2::scale_x_continuous(limits = c(0.25, 1.6)) +
    ## Horizontal Line
    ggplot2::geom_segment(ggplot2::aes(y = x_start, x = y_hl, yend = x_ends, xend = y_hl)) +
    # Left tick
    # ggplot2::geom_segment(ggplot2::aes(y = x_start, x = y_hl + 0.011, yend = x_start, xend = y_tails)) +
    # Right tick
    # ggplot2::geom_segment(ggplot2::aes(y = x_ends, x = y_hl + 0.011, yend = x_ends, xend = y_tails)) +
    # Total percentage
    ggplot2::annotate("text",
                      fontface = "bold",
                      family = "Calibri",
                      label = scales::percent(pct_below_poverty_and_alice),
                      y = pct_below_poverty_and_alice / 2,
                      x = y_hl + .18)


  plot

  obtn_save_plot(obtn_year, "ALICE", county_to_plot, plot_width, plot_height)

  plot
}


# Practice one:
# obtn_plot_alice(2022, "Baker")

# Make them all
# purrr::pwalk(list(2022, obtn_oregon_counties), obtn_plot_alice)
rfortherestofus/obtn documentation built on Nov. 13, 2022, 7:25 p.m.