R/make_random_stacked_density.R

Defines functions make_random_stacked_density

Documented in make_random_stacked_density

#' Make a random stacked density plot
#' @param n_lines num how many density lines
#' @param n_rows num how many points (lower will give more variability)
#' @param n_colors num how many colors to use; defaults to `n_lines`
#' @param color_palette chr which color palette to use
#' @param palette_colors chr vector of colors from color palette, usually generated by `get_palette_colors()`
#' @param ... other arguments passed on to `layer()`
#' @return `ggplot` object
#' @export
make_random_stacked_density <- function(n_lines = 9,
                                        n_rows = 1000,
                                        n_colors = n_lines,
                                        color_palette = NULL,
                                        palette_colors = get_palette_colors(color_palette, n_colors),
                                        ...) {
  df <- data.frame(
    x = stats::runif(n_rows),
    y = factor(sample(1:n_lines, n_rows, replace = TRUE))
  )

  gg <- ggplot2::ggplot(df, ggplot2::aes(x = x, group = y, fill = y)) +
    ggplot2::geom_density(position = 'fill', ...) +
    ggplot2::scale_fill_manual(values = palette_colors)
  gg

  return(gg)

}
jimtheflash/arrrt documentation built on April 5, 2020, 11:49 a.m.