Skip to content

Commit

Permalink
move ggstream to suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
nrennie committed Feb 14, 2023
1 parent 3f0ec48 commit d32c2e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Imports:
ggpattern,
ggplot2 (>= 3.4.0),
ggraph,
ggstream,
ggvoronoi,
grDevices,
igraph,
Expand All @@ -41,6 +40,7 @@ Imports:
Suggests:
deldir,
elevatr,
ggstream,
MetBrewer,
metR,
particles,
Expand Down
78 changes: 41 additions & 37 deletions R/streams.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d32c2e8

Please sign in to comment.