R/streams.R

Defines functions streams

Documented in streams

#' Streams
#'
#' This function generates a coloured generative art ggplot object from stream charts.
#'
#' @param bg_col Background colour. Default "white".
#' @param line_col Line colour. Default "white".
#' @param fill_col Vector of fill colours.
#' @param type Rotation of stream. Default "right".
#' @param s Seed value. Default 1234.
#' @return A ggplot object.
#' @examples
#' streams()
#' @export

streams <- function(bg_col = "white",
                    line_col = "white",
                    fill_col = c(
                      "#5F4690", "#1D6996", "#38A6A5", "#0F8554",
                      "#73AF48", "#EDAD08", "#E17C05", "#CC503E",
                      "#94346E", "#6F4070"
                    ),
                    type = "right",
                    s = 1234) {
  if (!requireNamespace("ggstream")) {
    stop("Please install {ggstream} to use this function.")
  } else {
    if (!(type %in% c("up", "down", "left", "right"))) {
      stop('Type must be one of "up", "down", "left", or "right"')
    }
    plot_data <- withr::with_seed(
      seed = s,
      code = {
        n <- length(fill_col)
        plot_data <- purrr::map_dfr(
          .x = 1:n,
          .f = ~ {
            x <- 1:sample(1:10, 1)
            tibble::tibble(x = x + sample(1:25, 1)) |>
              dplyr::mutate(
                y = sample(1:10, length(x), replace = TRUE),
                z = as.character(.x)
              )
          }
        )
        plot_data
      }
    )
    p <- ggplot2::ggplot(
      data = plot_data,
      mapping = ggplot2::aes(
        x = .data$x,
        y = .data$y,
        fill = .data$z
      )
    ) +
      ggstream::geom_stream(
        color = line_col,
        sorting = "onset"
      ) +
      ggplot2::scale_fill_manual(values = fill_col) +
      theme_aRt(bg_col)
    # rotate
    if (type == "up") {
      p <- p + ggplot2::coord_flip(expand = FALSE)
    } else if (type == "left") {
      p <- p + ggplot2::coord_cartesian(expand = FALSE) +
        ggplot2::scale_x_reverse()
    } else if (type == "down") {
      p <- p + ggplot2::coord_flip(expand = FALSE) +
        ggplot2::scale_x_reverse()
    } else if (type == "right") {
      p <- p + ggplot2::coord_cartesian(expand = FALSE)
    }
    return(p)
  }
}
nrennie/aRt documentation built on Jan. 7, 2025, 4:26 a.m.