diff --git a/DESCRIPTION b/DESCRIPTION index 7d9309b..bd2fdf8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,7 +24,6 @@ Imports: ggpattern, ggplot2 (>= 3.4.0), ggraph, - ggstream, ggvoronoi, grDevices, igraph, @@ -41,6 +40,7 @@ Imports: Suggests: deldir, elevatr, + ggstream, MetBrewer, metR, particles, diff --git a/R/streams.R b/R/streams.R index f5a2a9f..a743a3e 100644 --- a/R/streams.R +++ b/R/streams.R @@ -17,42 +17,46 @@ streams <- function(bg_col = "white", "#94346E", "#6F4070"), type = "right", s = 1234) { - if (!(type %in% c("up", "down", "left", "right"))) { - stop('Type must be one of "up", "down", "left", or "right"') + 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"') + } + set.seed(s) + # make data + n <- length(fill_col) + df <- 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 + p <- ggplot2::ggplot(data = df, + 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) + + ggplot2::theme_void() + + ggplot2::theme(legend.position = "none", + panel.background = ggplot2::element_rect(fill = bg_col, + colour = bg_col), + plot.background = ggplot2::element_rect(fill = bg_col, + colour = bg_col)) + #rotate + if (type == "up") { + p <- p + coord_flip(expand = FALSE) + } else if (type == "left") { + p <- p + coord_cartesian(expand = FALSE) + scale_x_reverse() + } else if (type == "down") { + p <- p + coord_flip(expand = FALSE) + scale_x_reverse() + } else if (type == "right") { + p <- p + coord_cartesian(expand = FALSE) + } + return(p) } - set.seed(s) - # make data - n <- length(fill_col) - df <- 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 - p <- ggplot2::ggplot(data = df, - 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) + - ggplot2::theme_void() + - ggplot2::theme(legend.position = "none", - panel.background = ggplot2::element_rect(fill = bg_col, - colour = bg_col), - plot.background = ggplot2::element_rect(fill = bg_col, - colour = bg_col)) - #rotate - if (type == "up") { - p <- p + coord_flip(expand = FALSE) - } else if (type == "left") { - p <- p + coord_cartesian(expand = FALSE) + scale_x_reverse() - } else if (type == "down") { - p <- p + coord_flip(expand = FALSE) + scale_x_reverse() - } else if (type == "right") { - p <- p + coord_cartesian(expand = FALSE) - } - return(p) }