R/obtn_plot_orpm.R

Defines functions obtn_plot_orpm

Documented in obtn_plot_orpm

#' Make ORPM plot
#'
#' @param obtn_year
#' @param plot_width
#' @param plot_height
#'
#' @return
#' @export
#'
#' @examples
obtn_plot_orpm <- function(obtn_year, plot_width = 6.5, plot_height = 3.4) {

  obtn_data_orpm_summary <- obtn_data_orpm %>%
    dplyr::mutate(child_poverty_rate = child_poverty_rate / 100) %>%
    dplyr::mutate(child_poverty_rate = janitor::round_half_up(child_poverty_rate, digits = 3)) %>%
    dplyr::mutate(child_poverty_rate_label = scales::percent(child_poverty_rate,
                                                             accuracy = 0.1)) %>%
    dplyr::mutate(child_poverty_rate_label = dplyr::case_when(
      (quarter == 1 | quarter == 4) & policy == "alt" ~ NA_character_,
      TRUE ~ child_poverty_rate_label
    )) %>%
    dplyr::mutate(q_2_3_y_text_position = dplyr::case_when(
      policy == "alt" & quarter == 1 ~ NA_real_,
      policy == "alt" & quarter == 4 ~ NA_real_,
      policy == "c19" & quarter == 1 ~ child_poverty_rate,
      policy == "c19" ~ child_poverty_rate - 0.015,
      policy == "alt" ~ child_poverty_rate + 0.015
    )) %>%
    dplyr::mutate(quarter_label = dplyr::case_when(
      quarter == 1 ~ str_glue("QUARTER {quarter}"),
      TRUE ~ str_glue("Q{quarter}")
    )) %>%
    dplyr::mutate(quarter_label = forcats::fct_reorder(quarter_label, quarter)) %>%
    dplyr::mutate(q_2_3_x_text_position = dplyr::case_when(
      quarter == 1 ~ as.numeric(quarter_label) + 0.22,
      quarter == 4 ~ as.numeric(quarter_label) - 0.07,
      policy == "c19" & quarter == 3 ~ as.numeric(quarter_label) + 0.05,
      TRUE ~ as.numeric(quarter_label)
    ))

  hrbrthemes::update_geom_font_defaults(family = "Calibri")

  ggplot2::ggplot(obtn_data_orpm_summary,
                  ggplot2::aes(x = quarter_label,
                               y = child_poverty_rate,
                               label = child_poverty_rate_label,
                               fill = quarter_label,
                               color = policy,
                               group = policy)) +
    ggplot2::geom_line(size = 1) +
    ggplot2::geom_point(size = 5,
                        stroke = 1,
                        fill = "white",
                        shape = 21) +
    shadowtext::geom_shadowtext(ggplot2::aes(label = child_poverty_rate_label,
                                             x = q_2_3_x_text_position,
                                             y = q_2_3_y_text_position),
                                size = 11 / .pt,
                                bg.color = "white",
                                family = "Calibri") +
    ggplot2::annotate("text", x = 1.02, y = 0.043, label = "5%", hjust = 0, color = "#828282") +
    ggplot2::annotate("text", x = 1.02, y = 0.093, label = "10%", hjust = 0, color = "#828282") +
    ggplot2::annotate("text", x = 1.02, y = 0.143, label = "15%", hjust = 0, color = "#828282") +
    ggplot2::annotate("text", x = 1.02, y = 0.193, label = "20%", hjust = 0, color = "#828282") +
    ggplot2::annotate("richtext",
                      x = 1.5, y = 0.165,
                      family = "Calibri",
                      color = tfff_dark_gray,
                      fill = "transparent",
                      angle = 22,
                      label.size = NA,
                      size = 9 / .pt,
                      label = "**WITHOUT**<BR>economic relief") +
    ggplot2::annotate("richtext",
                      x = 1.5, y = 0.08,
                      family = "Calibri",
                      color = tfff_orange,
                      fill = "transparent",
                      angle = -19,
                      label.size = NA,
                      size = 9 / .pt,
                      label = "**WITH**<BR>economic relief") +
    ggplot2::scale_color_manual(values = c(
      "c19" = tfff_orange,
      "alt" = tfff_dark_gray
    )) +
    ggplot2::scale_y_continuous(limits = c(0, 0.2),
                                expand = ggplot2::expansion(0.02, 0),
                                labels = scales::percent_format(accuracy = 1)) +
    ggplot2::scale_x_discrete(expand = ggplot2::expansion(0.04, 0),
                              labels = c("JANUARY 2020", "APRIL", "JULY", "OCTOBER")) +
    ggplot2::labs(y = "ORPM CHILD POVERTY RATE",
                  title = "Impact of 2020 economic relief policies on child poverty") +
    hrbrthemes::theme_ipsum(base_family = "Calibri",
                            axis_title_just = "mc",
                            plot_title_size = 14,
                            grid_col = tfff_light_gray) +
    ggplot2::theme(panel.grid.minor = ggplot2::element_blank(),
                   axis.text.x = ggplot2::element_text(color = "#828282",
                                                       size = 9,
                                                       margin = margin(t = 0)),
                   axis.text.y = ggplot2::element_blank(),
                   axis.title.x = ggplot2::element_blank(),
                   axis.title.y = ggplot2::element_text(color = "#828282",
                                                        size = 11),
                   plot.title = ggplot2::element_text(margin = margin(b = 4),
                                                      hjust = 0.5),
                   plot.margin = ggplot2::margin(3, 3, 3, 3, "pt"),
                   legend.position = "none")

  obtn_save_plot(obtn_year, "ORPM", "Oregon", plot_width, plot_height)
}
rfortherestofus/obtn documentation built on Nov. 13, 2022, 7:25 p.m.