R/gg_spell_sarid.R

Defines functions gg_spell_sarid

Documented in gg_spell_sarid

#' Generate a ggplot2 which spells SARID in a geom_col().
#'
#' The function generates a bar chart which spells out SARID.
#' The output is a ggplot.
#'
#' @param color1 The text color.
#' @param color2 The non-text color.
#' @return A ggplot2 as specified above.
#'
#' @examples
#' gg_spell_sarid()
#' gg_spell_sarid("blue", "red")
#'
#'
#' @importFrom magrittr %>%
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_col
#' @importFrom ggplot2 scale_fill_manual
#' @importFrom ggplot2 geom_label
#' @importFrom ggplot2 theme_bw
#' @importFrom ggplot2 theme
#' @importFrom ggplot2 xlab
#' @importFrom ggplot2 ylab
#' @importFrom ggplot2 element_blank
#' @importFrom dplyr mutate
#' @importFrom tibble tribble
#' @importFrom forcats fct_inorder
#' @importFrom forcats fct_rev
#' @importFrom purrr set_names
#' @export
gg_spell_sarid <- function(color1 = "#252161", color2 = "#abd9e9"){

  # Sets the tibble which is the basis for the plot
  sarid_letters <- tribble(~bar, ~height, ~fill,
                           1, 0.1, F,
                           1, 0.1, T,
                           1, 0.45, F,
                           1, 0.2, T,
                           1, 0.15, F,
                           2, 0.1, T,
                           2, 0.30, F,
                           2, 0.2, T,
                           2, 0.30, F,
                           2, 0.1, T,
                           3, 0.15, F,
                           3, 0.2, T,
                           3, 0.45, F,
                           3, 0.1, T,
                           3, 0.1, F,
                           4, 1, F,
                           5, 0.2, T,
                           5, 0.8, F,
                           6, 0.4, F,
                           6, 0.15, T,
                           6, 0.25, F,
                           6, 0.2, T,
                           7, 0.2, T,
                           7, 0.8, F,
                           8, 1, F,
                           9, 1, T,
                           10, 0.3, F,
                           10, 0.3, T,
                           10, 0.3, F,
                           10, 0.1, T,
                           11, 0.3, T,
                           11, 0.3, F,
                           11, 0.4, T,
                           12, 1, F,
                           13, 1, T,
                           14, 1, F,
                           15, 1, T,
                           16, 0.2, T,
                           16, 0.6, F,
                           16, 0.2, T,
                           17, 0.2, F,
                           17, 0.6, T,
                           17, 0.2, F) %>%
    mutate(segment = fct_inorder(factor(seq_along(bar)))) %>%
    mutate(segment = fct_rev(segment)) %>%
    mutate(colors = ifelse(fill, color1, color2))

  set_my_colors <- sarid_letters$colors %>%
    set_names(sarid_letters$segment)

  ggplot(sarid_letters, aes(x = bar, y = height, fill = segment)) +
    geom_col(position = "stack", color = "black") +
    scale_fill_manual(values = set_my_colors) +
    theme_bw() +
    theme(legend.position = "none",
          axis.ticks = element_blank(),
          axis.text = element_blank()) +
    xlab("") +
    ylab("")

}
sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.