Skip to content

Commit d32c2e8

Browse files
committed
move ggstream to suggests
1 parent 3f0ec48 commit d32c2e8

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Imports:
2424
ggpattern,
2525
ggplot2 (>= 3.4.0),
2626
ggraph,
27-
ggstream,
2827
ggvoronoi,
2928
grDevices,
3029
igraph,
@@ -41,6 +40,7 @@ Imports:
4140
Suggests:
4241
deldir,
4342
elevatr,
43+
ggstream,
4444
MetBrewer,
4545
metR,
4646
particles,

R/streams.R

+41-37
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,46 @@ streams <- function(bg_col = "white",
1717
"#94346E", "#6F4070"),
1818
type = "right",
1919
s = 1234) {
20-
if (!(type %in% c("up", "down", "left", "right"))) {
21-
stop('Type must be one of "up", "down", "left", or "right"')
20+
if (!requireNamespace("ggstream")) {
21+
stop("Please install {ggstream} to use this function.")
22+
} else {
23+
if (!(type %in% c("up", "down", "left", "right"))) {
24+
stop('Type must be one of "up", "down", "left", or "right"')
25+
}
26+
set.seed(s)
27+
# make data
28+
n <- length(fill_col)
29+
df <- purrr::map_dfr(.x = 1:n,
30+
.f = ~{
31+
x <- 1:sample(1:10, 1)
32+
tibble::tibble(x = x + sample(1:25, 1)) |>
33+
dplyr::mutate(y = sample(1:10, length(x), replace = TRUE),
34+
z = as.character(.x))
35+
})
36+
# plot
37+
p <- ggplot2::ggplot(data = df,
38+
mapping = ggplot2::aes(x = .data$x,
39+
y = .data$y,
40+
fill = .data$z)) +
41+
ggstream::geom_stream(color = line_col,
42+
sorting = "onset") +
43+
ggplot2::scale_fill_manual(values = fill_col) +
44+
ggplot2::theme_void() +
45+
ggplot2::theme(legend.position = "none",
46+
panel.background = ggplot2::element_rect(fill = bg_col,
47+
colour = bg_col),
48+
plot.background = ggplot2::element_rect(fill = bg_col,
49+
colour = bg_col))
50+
#rotate
51+
if (type == "up") {
52+
p <- p + coord_flip(expand = FALSE)
53+
} else if (type == "left") {
54+
p <- p + coord_cartesian(expand = FALSE) + scale_x_reverse()
55+
} else if (type == "down") {
56+
p <- p + coord_flip(expand = FALSE) + scale_x_reverse()
57+
} else if (type == "right") {
58+
p <- p + coord_cartesian(expand = FALSE)
59+
}
60+
return(p)
2261
}
23-
set.seed(s)
24-
# make data
25-
n <- length(fill_col)
26-
df <- purrr::map_dfr(.x = 1:n,
27-
.f = ~{
28-
x <- 1:sample(1:10, 1)
29-
tibble::tibble(x = x + sample(1:25, 1)) |>
30-
dplyr::mutate(y = sample(1:10, length(x), replace = TRUE),
31-
z = as.character(.x))
32-
})
33-
# plot
34-
p <- ggplot2::ggplot(data = df,
35-
mapping = ggplot2::aes(x = .data$x,
36-
y = .data$y,
37-
fill = .data$z)) +
38-
ggstream::geom_stream(color = line_col,
39-
sorting = "onset") +
40-
ggplot2::scale_fill_manual(values = fill_col) +
41-
ggplot2::theme_void() +
42-
ggplot2::theme(legend.position = "none",
43-
panel.background = ggplot2::element_rect(fill = bg_col,
44-
colour = bg_col),
45-
plot.background = ggplot2::element_rect(fill = bg_col,
46-
colour = bg_col))
47-
#rotate
48-
if (type == "up") {
49-
p <- p + coord_flip(expand = FALSE)
50-
} else if (type == "left") {
51-
p <- p + coord_cartesian(expand = FALSE) + scale_x_reverse()
52-
} else if (type == "down") {
53-
p <- p + coord_flip(expand = FALSE) + scale_x_reverse()
54-
} else if (type == "right") {
55-
p <- p + coord_cartesian(expand = FALSE)
56-
}
57-
return(p)
5862
}

0 commit comments

Comments
 (0)