@@ -17,42 +17,46 @@ streams <- function(bg_col = "white",
17
17
" #94346E" , " #6F4070" ),
18
18
type = " right" ,
19
19
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 )
22
61
}
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 )
58
62
}
0 commit comments