Skip to content

Commit ea7ff74

Browse files
Merge pull request #27 from pepijn-devries/work-in-progress
Work in progress
2 parents 30a1ef2 + 323bc72 commit ea7ff74

File tree

13 files changed

+85
-42
lines changed

13 files changed

+85
-42
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ggsankeyfier
22
Type: Package
33
Title: Create Sankey and Alluvial Diagrams Using 'ggplot2'
4-
Version: 0.1.8.0010
4+
Version: 0.1.8.0011
55
Authors@R: c(person("Pepijn", "de Vries", role = c("aut", "cre", "dtc"),
66
email = "pepijn.devries@outlook.com",
77
comment = c(ORCID = "0000-0002-7961-6646")),

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
ggsankeyfier v0.1.8.0010
1+
ggsankeyfier v0.1.8.0011
22
-------------
33

44
* Added better stacking order features
5+
* Added `curve_weight` parameter to `geom_sankeyedge()`
56
* Added check workflow
67
* Added code coverage workflow and badge
78
* Improved test coverage
9+
* Expanded documentation
810
* Fix for [issue #23](https://github.com/pepijn-devries/ggsankeyfier/issues/23)
911

1012
ggsankeyfier v0.1.8

R/draw_edges.r

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
dplyr::mutate(
4747
bez =
4848
mapply(
49-
function(x, y, xend, yend, y_size, slope, ncp, fill, colour, linetype, linewidth,
49+
function(x, y, xend, yend, y_size, slope, curve_weight,
50+
ncp, fill, colour, linetype, linewidth,
5051
alpha, waist, res, connector) {
5152
gp <- grid::gpar(fill = fill, col = colour,
5253
lwd = linewidth*ggplot2::.pt, lty = linetype, alpha = alpha)
@@ -90,7 +91,9 @@
9091
)
9192
} else {
9293
vwline::offsetBezierGrob(
93-
x = grid::unit(c(x, x + slope2, xend - slope2, xend), "npc"),
94+
x = grid::unit(c(
95+
x, x + slope2 * (curve_weight + 0.5),
96+
xend - slope2 * (1.5 - curve_weight), xend), "npc"),
9497
y = grid::unit(c(y, y, yend, yend), "npc"),
9598
w = grid::unit(c(1, waist, 1)*y_size, "npc")*asp_cor,
9699
stepFn = gridBezier::nSteps(ncp),
@@ -99,7 +102,9 @@
99102
}
100103
},
101104
x = .data$x, y = .data$y, xend = .data$xend,
102-
yend = .data$yend, y_size = .data$edge_size, slope = .data$slope,
105+
yend = .data$yend, y_size = .data$edge_size,
106+
slope = .data$slope,
107+
curve_weight = .data$curve_weight,
103108
ncp = .data$ncp,
104109
fill = .data$fill, colour = .data$colour, linetype = .data$linetype,
105110
linewidth = .data$linewidth,

R/geom_edge.r

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#' @inheritParams ggplot2::geom_segment
2525
#' @param slope Slope parameter (`numeric`) for the Bezier curves used to depict the edges.
2626
#' Any value between 0 and 1 will work nicely. Other non-zero values will also work.
27+
#' @param curve_weight Places weight on the Bezier curve. Values close to zero will
28+
#' pull the inflection point of the curve towards outgoing nodes. Values close to one
29+
#' will pull them towards incoming nodes. The default is 0.5, which will place the
30+
#' inflection point exactly in the middle of the connecting nodes.
2731
#' @param ncp Number of control points on the Bezier curve that forms the edge. Larger
2832
#' numbers will result in smoother curves, but cost more computational time. Default is
2933
#' 100.
@@ -49,17 +53,22 @@ GeomSankeyedge <-
4953
draw_panel = .draw_edges,
5054
setup_data = function(data, params) {
5155
data <- GeomSankeysegment$setup_data(data, params)
52-
data <- data |>
53-
dplyr::mutate(
54-
slope = params$slope,
55-
ncp = params$ncp
56+
unique_x <- unique(data$x) |> sort()
57+
curve_params <-
58+
data.frame(
59+
x = unique_x,
60+
slope = rep(params$slope, length.out = length(unique_x)),
61+
curve_weight = rep(params$curve_weight, length.out = length(unique_x)),
62+
ncp = params$ncp
5663
)
64+
data <- data |>
65+
dplyr::left_join(curve_params, by = "x")
5766
return(data)
5867
},
5968
rename_size = FALSE,
6069
default_aes = c(GeomSankeysegment$default_aes, waist = 1),
6170
draw_key = draw_key_sankeyedge,
62-
extra_params = c("na.rm", "slope", "ncp")
71+
extra_params = c("na.rm", "slope", "curve_weight", "ncp")
6372
)
6473

6574
#' @name geom_sankeyedge
@@ -68,7 +77,7 @@ GeomSankeyedge <-
6877
geom_sankeyedge <-
6978
function(mapping = NULL, data = NULL, stat = "sankeyedge",
7079
position = "sankey", na.rm = FALSE, show.legend = NA,
71-
slope = 0.5, ncp = 100,
80+
slope = 0.5, curve_weight = 0.5, ncp = 100,
7281
width = "auto", align = c("bottom", "top", "center", "justify"),
7382
order = c("ascending", "descending", "ascending+", "descending+", "as_is"),
7483
h_space = "auto", v_space = 0,
@@ -82,6 +91,7 @@ geom_sankeyedge <-
8291
ggplot2::layer(
8392
geom = GeomSankeyedge, mapping = mapping, data = data, stat = stat,
8493
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
85-
params = list(na.rm = na.rm, slope = slope, ncp = ncp, ...)
94+
params = list(na.rm = na.rm, slope = slope, curve_weight = curve_weight,
95+
ncp = ncp, ...)
8696
)
8797
}

R/stat_edge.r

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ StatSankeyedge <-
2828
#' @export
2929
stat_sankeyedge <-
3030
function(mapping = NULL, data = NULL, geom = "sankeyedge",
31-
position = "sankey", na.rm = FALSE, slope = 0.5, ncp = 100,
32-
show.legend = NA, inherit.aes = TRUE, ...) {
31+
position = "sankey", na.rm = FALSE, slope = 0.5, curve_weight = 0.5,
32+
ncp = 100, show.legend = NA, inherit.aes = TRUE, ...) {
3333

3434
ggplot2::layer(
3535
stat = StatSankeyedge, data = data, mapping = mapping, geom = geom,
3636
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
37-
params = list(na.rm = na.rm, slope = slope, ncp = ncp, ...)
37+
params = list(na.rm = na.rm, slope = slope, curve_weight = curve_weight,
38+
ncp = ncp, ...)
3839
)
3940
}

README.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The `ggsankeyfier` packages allows you to visualise your data as Sankey or Alluv
3535
A Sankey diagram is essentially a stacked bar plot, where the bands connect bars across
3636
stages (on the x-axis), to show how quantities flow between them.
3737

38-
## Why use `ggsankeyfier`?
38+
## Why Use `ggsankeyfier`?
3939

4040
`ggsankeyfier` allows you to add Sankey diagram layers to a `ggplot2::ggplot()`. The
4141
package also provides `stat_*` and `position_*` functions that allow you
@@ -56,7 +56,7 @@ install.packages("ggsankeyfier")
5656
devtools::install_github('pepijn-devries/ggsankeyfier')
5757
```
5858

59-
## Important concepts
59+
## Important Concepts
6060

6161
As there is some variation in the definition and terminology used in Sankey diagrams,
6262
there are some introduced here for consistency across the package documentation.
@@ -127,7 +127,7 @@ to `group`. In addition to these 'standard' aesthetics, you also need to specify
127127
`connector` specifying the direction of an edge (one of `'from'` or `'to'`); and an
128128
`edge_id` which is used to determine which connector ends should be paired together.
129129

130-
### Data management
130+
### Data Management
131131

132132
Note that the plotting routines require data organised in a `data.frame`, with in each
133133
row a 'connector'. A connector is either the start or an end of an edge. This allows
@@ -136,7 +136,7 @@ cases this is not the type of data you will be working with. Check
136136
`vignette("data_management")`, on how to rearrange your data for displaying it in a
137137
Sankey diagram.
138138

139-
### Positioning nodes and edges
139+
### Positioning Nodes and Edges
140140

141141
The package gives you much control on the positioning of elements in the diagram. Think of:
142142

@@ -147,7 +147,7 @@ The package gives you much control on the positioning of elements in the diagram
147147

148148
`vignette("positioning")` and `vignette("stacking_order")` will show you how.
149149

150-
### Decorating nodes and edges
150+
### Decorating Nodes and Edges
151151

152152
When creating your own Sankey diagrams you may want to alter its
153153
appearance. You may want to:

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ or Alluvial diagrams. A Sankey diagram is essentially a stacked bar
2222
plot, where the bands connect bars across stages (on the x-axis), to
2323
show how quantities flow between them.
2424

25-
## Why use `ggsankeyfier`?
25+
## Why Use `ggsankeyfier`?
2626

2727
`ggsankeyfier` allows you to add Sankey diagram layers to a
2828
`ggplot2::ggplot()`. The package also provides `stat_*` and `position_*`
@@ -46,7 +46,7 @@ install.packages("ggsankeyfier")
4646
devtools::install_github('pepijn-devries/ggsankeyfier')
4747
```
4848

49-
## Important concepts
49+
## Important Concepts
5050

5151
As there is some variation in the definition and terminology used in
5252
Sankey diagrams, there are some introduced here for consistency across
@@ -133,7 +133,7 @@ addition to these ‘standard’ aesthetics, you also need to specify a
133133
`'to'`); and an `edge_id` which is used to determine which connector
134134
ends should be paired together.
135135

136-
### Data management
136+
### Data Management
137137

138138
Note that the plotting routines require data organised in a
139139
`data.frame`, with in each row a ‘connector’. A connector is either the
@@ -143,7 +143,7 @@ not the type of data you will be working with. Check
143143
`vignette("data_management")`, on how to rearrange your data for
144144
displaying it in a Sankey diagram.
145145

146-
### Positioning nodes and edges
146+
### Positioning Nodes and Edges
147147

148148
The package gives you much control on the positioning of elements in the
149149
diagram. Think of:
@@ -156,7 +156,7 @@ diagram. Think of:
156156
`vignette("positioning")` and `vignette("stacking_order")` will show you
157157
how.
158158

159-
### Decorating nodes and edges
159+
### Decorating Nodes and Edges
160160

161161
When creating your own Sankey diagrams you may want to alter its
162162
appearance. You may want to:
@@ -188,7 +188,7 @@ from the Dutch Ministry of Agriculture, Nature and Food Quality
188188

189189
## Resources
190190

191-
- Piet GJ, Jongbloed RH, Bentley JW, Grundlehner A, Tamis JE, De Vries
191+
- Piet GJ, Bentley JW, Jongbloed RH, Grundlehner A, Tamis JE, De Vries
192192
P (2024) A Cumulative Impact Assessment on the North Sea Capacity to
193193
Supply Ecosystem Services. Science of The Total Environment (498)
194194
[DOI:10.1016/j.scitotenv.2024.174149](https://doi.org/10.1016/j.scitotenv.2024.174149)

man/geom_sankeyedge.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/stat_sankey.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/data_management.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: "Sankey data management"
2+
title: "Sankey Data Management"
33
output: rmarkdown::html_vignette
44
vignette: >
5-
%\VignetteIndexEntry{Sankey data management}
5+
%\VignetteIndexEntry{Sankey Data Management}
66
%\VignetteEngine{knitr::rmarkdown}
77
%\VignetteEncoding{UTF-8}
88
---
@@ -26,7 +26,7 @@ probably organised in a wide format with stages of the Sankey diagram in columns
2626
For plotting this needs to be converted into a long format. Why and how to do this, is
2727
discussed below.
2828

29-
## Wide or long format?
29+
## Wide or Long Format?
3030

3131
A wide format would be typically used when working with
3232
the data. This can be best understood when the framework you wish to visualise represents a
@@ -45,7 +45,7 @@ Now, when do we work with either the wide or the long format? When working with
4545
on chains, it makes sense to work with a wide format. When plotting with `ggsankeyfier` or
4646
modification of flow information is required, a long format is more suitable.
4747

48-
### Converting from wide to long
48+
### Converting from Wide to Long
4949

5050
This package comes with a function that allow you to pivot information with stages organised as
5151
columns (i.e., wide format) to a long format. All you need to do is specify which columns represent
@@ -79,7 +79,7 @@ es_long <-
7979
)
8080
```
8181

82-
## The edge id and connector
82+
## The Edge id and Connector
8383

8484
After pivoting to the long format as illustrated above you will note two additional columns
8585
that contain information that was not available in the wide format. Namely the columns `edge_id`

0 commit comments

Comments
 (0)