R/obtn_plot_pace.R

Defines functions pace_word_positivity_plot pace_ideology_by_urbanicity_plot pace_word_positivity_rural_plot

Documented in pace_ideology_by_urbanicity_plot pace_word_positivity_plot pace_word_positivity_rural_plot

#' PACE word positivity rural plot
#'
#' @return
#' @export
#'
#' @examples
pace_word_positivity_rural_plot <- function() {
  pace_word_positivity_rural |>
    dplyr::mutate(value_formatted = scales::percent(value, 1, suffix = "")) |>
    ggplot2::ggplot(
      ggplot2::aes(
        x = value,
        y = word
      )
    ) +
    ggplot2::geom_col(
      fill = tfff_light_green,
      width = 0.15
    ) +
    ggplot2::geom_point(
      fill = tfff_light_green,
      color = "white",
      shape = 21,
      size = 4,
      stroke = 0.5
    ) +
    ggplot2::geom_text(
      ggplot2::aes(
        label = value_formatted
      ),
      # hjust = 1.2,
      size = 1.75,
      color = "black",
      family = "ProximaNova-Regular"
    ) +
    ggplot2::scale_x_continuous(
      limits = c(0, 1),
      breaks = seq(0, 1, 0.25),
      labels = scales::percent_format(),
      expand = ggplot2::expansion(0, 0)
    ) +
    ggplot2::theme_void(
      base_family = "ProximaNova-Regular",
      base_size = 7
    ) +
    ggplot2::theme(
      panel.grid.major.x = ggplot2::element_line(color = tfff_light_gray),
      axis.text.x = ggplot2::element_text(size = 5),
      axis.text.y = ggplot2::element_text(
        hjust = 1,
        margin = ggplot2::margin(r = 3),
        size = 5
      ),
      strip.text = ggplot2::element_text(
        size = 12,
        margin = ggplot2::margin(b = 5),
        family = "ProximaNova-Bold"
      ),
      plot.margin = ggplot2::margin(10, 10, 10, 10)
    )

  ggplot2::ggsave(
    filename = "inst/plots/2024/pace_word_positivity_rural_lollipop_plot.pdf",
    width = 4,
    height = 3.2,
    unit = "in",
    device = cairo_pdf
  )
}

#' PACE ideology by urbanicity plot
#'
#' @return
#' @export
#'
#' @examples
pace_ideology_by_urbanicity_plot <- function() {
  pace_ideology_by_urbanicity |>
    dplyr::mutate(ideology = stringr::str_wrap(ideology, 10)) |>
    dplyr::mutate(ideology = forcats::fct_inorder(ideology)) |>
    dplyr::mutate(ideology = forcats::fct_rev(ideology)) |>
    dplyr::mutate(value_formatted = scales::percent(value, 1)) |>
    ggplot2::ggplot(
      ggplot2::aes(
        x = ideology,
        y = value,
        fill = geography,
        label = value_formatted
      )
    ) +
    ggplot2::geom_col(
      position = ggplot2::position_dodge(width = 0.9)
    ) +
    ggplot2::geom_text(
      position = ggplot2::position_dodge(width = 0.9),
      size = 1,
      family = "ProximaNova-Regular",
      vjust = 2,
      color = "white",
      show.legend = FALSE
    ) +
    ggplot2::scale_fill_manual(
      values = obtn_choropleth_colors,
      name = NULL
    ) +
    ggplot2::scale_y_continuous(
      label = scales::percent_format(),
      expand = ggplot2::expansion(0, 0)
    ) +
    ggplot2::scale_x_discrete(
      expand = ggplot2::expansion(0, 0.5)
    ) +
    ggplot2::coord_cartesian(clip = "off") +
    ggplot2::theme_minimal(
      base_size = 7,
      base_family = "ProximaNova-Regular"
    ) +
    ggplot2::theme(
      axis.title = ggplot2::element_blank(),
      legend.position = "top",
      legend.key.size = ggplot2::unit(0.25, "cm"),
      legend.text = ggplot2::element_text(size = 4),
      legend.box.margin = ggplot2::margin(0, 0, 0, 0),
      axis.text.y = ggplot2::element_text(size = 4),
      axis.text.x = ggplot2::element_blank(),
      panel.grid.minor = ggplot2::element_blank(),
      panel.grid.major.x = ggplot2::element_blank(),
      panel.spacing = ggplot2::unit(0.5, "lines"),
      strip.text = ggplot2::element_text(
        hjust = 0,
        family = "ProximaNova-Bold"
      )
    )

  ggplot2::ggsave(
    filename = "inst/plots/2024/pace_ideology_by_urbanicity.pdf",
    width = 2.25,
    height = 2.1,
    unit = "in",
    device = cairo_pdf
  )
}


# pace_ideology_by_urbanicity_plot()


#' PACE Word Positivity Plot
#'
#' @return
#' @export
#'
#' @examples
pace_word_positivity_plot <- function() {
  text_padding <- 0.13

  pace_word_positivity_for_plot <-
    pace_word_positivity |>
    dplyr::filter(response == "Positive") |>
    dplyr::mutate(value_formatted = scales::percent(value, 1)) |>
    dplyr::mutate(year = as.factor(year)) |>
    dplyr::mutate(word = forcats::fct(
      word,
      levels = c(
        "Liberty",
        "Unity",
        "Citizen",
        "Democracy",
        "Belonging",
        "Diversity",
        "Patriotism",
        "Civility",
        "Racial Equity",
        "Social Justice",
        "Civic Engagement"
      )
    )) |>
    dplyr::mutate(word = forcats::fct_rev(word)) |>
    dplyr::mutate(positive_growth = dplyr::case_when(
      value - dplyr::lead(value) > 0 & year == 2023 ~ TRUE,
      value - dplyr::lead(value) <= 0 & year == 2023 ~ FALSE
    )) |>
    tidyr::fill(positive_growth, .direction = "down") |>
    dplyr::mutate(text_position = dplyr::case_when(
      year == 2023 & positive_growth == TRUE ~ value + text_padding,
      year == 2023 & positive_growth == FALSE ~ value - text_padding,
      year == 2021 & positive_growth == TRUE ~ value - text_padding,
      year == 2021 & positive_growth == FALSE ~ value + text_padding,
    )) |>
    dplyr::arrange(year)

  pace_word_positivity_for_plot |>
    ggplot2::ggplot(
      ggplot2::aes(
        x = value,
        y = word,
        fill = year,
        color = year
      )
    ) +
    ggplot2::geom_point(
      shape = 21,
      size = 2,
      color = "white",
      stroke = 0.4
    ) +
    # shadowtext::geom_shadowtext(
    #   data = pace_word_positivity_for_plot |> filter(word == "Liberty"),
    #   ggplot2::aes(
    #     x = text_position,
    #     label = year
    #   ),
    #   vjust = -1,
    #   bg.color = "white",
    #   size = 2
    # ) +
    shadowtext::geom_shadowtext(
      ggplot2::aes(
        x = text_position,
        label = value_formatted
      ),
      bg.color = "white",
      size = 2
    ) +
    ggplot2::facet_wrap(
      ggplot2::vars(geography)
    ) +
    ggplot2::scale_fill_manual(
      values = c(
        "2021" = tfff_light_green,
        "2023" = tfff_dark_green
      ),
      name = NULL
    ) +
    ggplot2::scale_color_manual(
      values = c(
        "2021" = tfff_light_green,
        "2023" = tfff_dark_green
      ),
      name = NULL
    ) +
    ggplot2::scale_x_continuous(
      limits = c(0, 1),
      breaks = seq(0, 1, 0.25),
      label = scales::percent_format()
    ) +
    ggplot2::scale_y_discrete(
      expand = ggplot2::expansion(0, 0.25)
    ) +
    ggplot2::coord_cartesian(clip = "off") +
    ggplot2::theme_minimal(
      base_size = 7,
      base_family = "ProximaNova-Regular"
    ) +
    ggplot2::theme(
      axis.title = ggplot2::element_blank(),
      axis.text.x = ggplot2::element_blank(),
      legend.position = "top",
      legend.box.margin = ggplot2::margin(-10, 0, -10, 0),
      legend.spacing = ggplot2::unit(0, "cm"),
      panel.grid.minor = ggplot2::element_blank(),
      panel.spacing = ggplot2::unit(0.5, "lines"),
      strip.text = ggplot2::element_text(
        hjust = 0,
        family = "ProximaNova-Bold"
      )
    )

  ggplot2::ggsave(
    filename = "inst/plots/2024/pace_word_positivity.pdf",
    width = 4.4,
    height = 2.5,
    unit = "in",
    device = cairo_pdf
  )
}

# pace_word_positivity_plot()
rfortherestofus/obtn documentation built on Feb. 10, 2025, 1:30 a.m.