R/obtn_oregon_voices.R

Defines functions obtn_oregon_voices_pride_plot obtn_oregon_voices_appreciation_plot obtn_plot_oregon_voices_participation

#' Create plot for Oregon Voices participation
#'
#' @return
#' @export
#'
#' @examples
obtn_plot_oregon_voices_participation <- function() {

  obtn_oregon_voices_rev <- obtn_oregon_voices_participation %>%
    dplyr::mutate(geography = forcats::fct_reorder(geography, responses)) %>%
    dplyr::mutate(type = stringr::str_to_title(type))

  obtn_oregon_voices_rev_unique <- obtn_oregon_voices_rev %>%
    dplyr::group_by(geography) %>%
    dplyr::summarize(total_responses = sum(responses))

  plot <- ggplot2::ggplot() +
    ggplot2::geom_col(data = obtn_oregon_voices_rev,
                      ggplot2::aes(responses, geography,
                                   fill = type),
                      width = 0.8) +
    ggplot2::geom_text(data = obtn_oregon_voices_rev_unique,
                       ggplot2::aes(total_responses, geography,
                                    label = total_responses),
                       size = 3.5,
                       hjust = -0.25,
                       color = tfff_dark_gray,
                       family = "Calibri") +
    ggplot2::labs(fill = NULL) +
    ggplot2::theme_minimal(base_family = "Calibri") +
    ggplot2::theme(legend.position = "none",
                   # axis.text.y = ggplot2::element_blank(),
                   axis.title = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_blank(),
                   panel.grid.major.y = ggplot2::element_blank()) +
    ggplot2::scale_x_continuous(expand = c(0, 0),
                                limits = c(0, 530)) +
    ggplot2::scale_fill_manual(values = c(obtn::tfff_light_green, obtn::tfff_dark_green),
                               guide = ggplot2::guide_legend(reverse = TRUE))

  plot

  obtn_save_plot(2022, "Oregon Voices Participation", "Oregon", 6.25, 6.25)

  plot

}

# obtn_plot_oregon_voices_participation()



#' Create plot for Oregon Voices appreciation
#'
#' @return
#' @export
#'
#' @examples
obtn_oregon_voices_appreciation_plot <- function() {

  plot <- obtn_oregon_voices_appreciation %>%
    dplyr::filter(response != "No/Negative response") %>%
    dplyr::mutate(pct_formatted = scales::percent(pct, 1)) %>%
    ggplot2::ggplot(ggplot2::aes(x = pct, y = group,
                                 fill = group,
                                 label = pct_formatted)) +
    ggplot2::geom_col(ggplot2::aes(x = 1,
                                   y = group),
                      fill = tfff_light_gray) +
    ggplot2::geom_col() +
    ggplot2::geom_text(family = "Calibri",
                       color = tfff_dark_gray,
                       hjust = -0.5) +
    ggplot2::theme_minimal(base_family = "Calibri") +
    ggplot2::theme(legend.position = "right",
                   strip.text = ggplot2::element_text(hjust = 0,
                                                      margin = ggplot2::margin(l = 10, b = 2),
                                                      face = "bold",
                                                      size = 10),
                   axis.text = ggplot2::element_blank(),
                   axis.title = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_blank(),
                   panel.grid.major = ggplot2::element_blank()) +
    ggplot2::labs(fill = NULL) +
    # ggplot2::scale_x_continuous(expand = c(0, 0)) +
    # ggplot2::scale_y_discrete(expand = c(0, 0)) +
    ggplot2::scale_fill_manual(values = c(obtn::tfff_light_green, obtn::tfff_dark_green),
                               guide = ggplot2::guide_legend(reverse = TRUE)) +
    ggplot2::facet_wrap(~response,
                        ncol = 2)

  plot

  obtn_save_plot(2022, "Oregon Voices Appreciation", "Oregon", 6.5, 2.75)

  plot

}

# obtn_oregon_voices_appreciation_plot()

#' Create plot for Oregon Voices pride in community
#'
#' @return
#' @export
#'
#' @examples
obtn_oregon_voices_pride_plot <- function() {

  obtn_oregon_voices_community_pride_sometimes_positive <- obtn_oregon_voices_community_pride %>%
    dplyr::filter(response == "Sometimes") %>%
    dplyr::mutate(pct_bis = pct,
                  pct = pct / 2) %>%
    dplyr::mutate(response_for_plot = "Sometimes positive")

  obtn_oregon_voices_community_pride_sometimes_negative <- obtn_oregon_voices_community_pride %>%
    dplyr::filter(response == "Sometimes") %>%
    dplyr::mutate(pct = pct / 2) %>%
    dplyr::mutate(response_for_plot = "Sometimes negative")

  obtn_oregon_voices_community_pride_for_diverging_plot <- obtn_oregon_voices_community_pride %>%
    dplyr::filter(response != "Sometimes") %>%
    dplyr::mutate(response_for_plot = response) %>%
    dplyr::bind_rows(obtn_oregon_voices_community_pride_sometimes_positive, obtn_oregon_voices_community_pride_sometimes_negative) %>%
    dplyr::mutate(pct_for_plot = dplyr::case_when(
      response_for_plot %in% c("Never", "Rarely", "Sometimes negative") ~ -pct,
      TRUE ~ pct),
      response_for_plot = forcats::fct_relevel(response_for_plot, "Sometimes negative", "Rarely", "Never", "Sometimes positive", "Often", "Always"),
      type = dplyr::if_else(
        response_for_plot %in% c("Never", "Rarely", "Sometimes negative"),
        "Negative",
        "Positive"
      )
    ) %>%
    dplyr::arrange(response_for_plot) %>%
    dplyr::group_by(group, type) %>%
    dplyr::mutate(
      bar_position_end = cumsum(pct),
      bar_position_begin = dplyr::lag(bar_position_end)
    ) %>%
    tidyr::replace_na(list(bar_position_end = 0, bar_position_begin = 0)) %>%
    dplyr::ungroup() %>%
    dplyr::mutate(
      bar_position_begin = dplyr::if_else(type == "Negative",-bar_position_begin, bar_position_begin),
      bar_position_end = dplyr::if_else(type == "Negative",-bar_position_end, bar_position_end),
      bar_position_middle = (bar_position_begin + bar_position_end) / 2,
      bar_position_middle = dplyr::if_else(response == "Sometimes", 0, bar_position_middle),
      pct = dplyr::if_else(
        response_for_plot == "Sometimes positive",
        pct_bis,
        pct
      ),
      pct = dplyr::if_else(
        response_for_plot == "Sometimes negative" | pct < .1,
        "",
        scales::percent(abs(pct), accuracy = 1)
      ),
      group_num = dplyr::if_else(group == "rural", 1, 0)
    )

  # plot
  plot <- obtn_oregon_voices_community_pride_for_diverging_plot %>%
    ggplot2::ggplot(
      ggplot2::aes(
        fill = response,
        ymin = group_num - 0.3,
        ymax = group_num + 0.3,
        xmin = bar_position_begin,
        xmax = bar_position_end
      )
    ) +
    # ggplot2::geom_vline(xintercept = 0,
    #                     size = 0.5,
    #                     linetype = "dotted",
    #                     color = tfff_medium_gray) +
    ggplot2::geom_rect(color = "transparent") +
    ggplot2::geom_text(
      ggplot2::aes(y = group_num,
                   x = bar_position_middle,
                   label = pct),
      position = ggplot2::position_identity(),
      family = "Calibri",
      color = "white",
      fontface = "bold"
    ) +
    ggplot2::scale_y_continuous(
      name = "",
      labels = c("Urban", "Rural"),
      breaks = c(0, 1)
    ) +
    ggplot2::scale_fill_manual(
      name = "",
      values = c("#7a2c01", tfff_orange, tfff_medium_gray, tfff_light_green, tfff_dark_green),
      guide = ggplot2::guide_legend(
        label.position = "top",
        keywidth = 2.5,
        keyheight = 1,
        reverse = FALSE
      )
    ) +
    ggplot2::scale_x_continuous(labels = scales::percent_format()) +
    ggplot2::theme_minimal(base_family = "Calibri") +
    ggplot2::theme(
      axis.text.y = ggplot2::element_text(size = 12,
                                          face = "bold"),
      axis.text.x = ggplot2::element_blank(),
      axis.title.x = ggplot2::element_blank(),
      panel.grid = ggplot2::element_blank(),
      # panel.grid.major.y = ggplot2::element_blank(),
      # panel.grid.minor.y = ggplot2::element_blank(),
      # panel.grid.minor.x = ggplot2::element_blank(),
      legend.position = "top"
    )

  plot

  obtn_save_plot(2022, "Oregon Voices Pride", "Oregon", 6.5, 2.75)

  plot

}

# obtn_oregon_voices_pride_plot()
rfortherestofus/obtn documentation built on Nov. 13, 2022, 7:25 p.m.