diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 0eca7fb..fdec38f 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -61,7 +61,7 @@ NULL #' @param fontFamily font family for the node text labels. Default is 'Arial'. #' @param fontColor font color for the node text labels. #' @param nodeWidth numeric width of each node. -#' @param nodePadding numeric essentially influences the width height. Default is 12. +#' @param nodePadding numeric essentially influences the width height. By default, it's the same as the fontSize parameter. #' @param nodeStrokeWidth numeric width of the stroke around nodes. #' @param nodeCornerRadius numeric Radius for rounded nodes. #' @param numberFormat number format in tooltips - see https://github.com/d3/d3-format for options. Default is ',.1f'. @@ -85,8 +85,8 @@ NULL #' @param linkColor numeric Color of links. #' @param linkOpacity numeric Opacity of links. #' @param linkGradient boolean Add a gradient to the links? -#' @param dragX boolean Allow moving nodes along the x-axis? -#' @param dragY boolean Allow moving nodes along the y-axis? +#' @param dragX boolean Allow moving nodes along the x-axis? Default is FALSE. +#' @param dragY boolean Allow moving nodes along the y-axis? Default is TRUE. #' @param nodeShadow boolean Add a shadow to the nodes? #' @param xScalingFactor numeric Scale the computed x position of the nodes by this value. #' @param xAxisDomain character[] If xAxisDomain is given, an axis with those value is @@ -127,7 +127,7 @@ sankeyNetwork <- function(Links, fontFamily = "Arial", fontColor = NULL, nodeWidth = 15, - nodePadding = 12, + nodePadding = NULL, nodeStrokeWidth = 1, nodeCornerRadius = 0, margin = NULL, @@ -138,7 +138,7 @@ sankeyNetwork <- function(Links, doubleclickTogglesChildren = FALSE, xAxisDomain = NULL, dragX = FALSE, - dragY = FALSE, + dragY = TRUE, height = 500, width = 800, iterations = 32, @@ -167,6 +167,11 @@ sankeyNetwork <- function(Links, # Hack for UI consistency. Think of improving. colourScale <- as.character(colourScale) + # Change nodePadding to fontSize if it is NULL + if (is.null(nodePadding)) { + nodePadding <- fontSize + } + # If tbl_df convert to plain data.frame Links <- tbl_df_strip(Links) Nodes <- tbl_df_strip(Nodes) diff --git a/docs/articles/Examples.html b/docs/articles/Examples.html index de3145e..61b485e 100644 --- a/docs/articles/Examples.html +++ b/docs/articles/Examples.html @@ -103,8 +103,8 @@

Smallest Example Source = "source", Target = "target" ) -
- +
+

Labels and Values @@ -151,11 +151,11 @@

Labels and Values= nodes, NodeID = "label" )

-
- +
+
-

You can have link-specific and node-specific colors:

@@ -188,14 +188,14 @@ 
-
- +
+
-

Node Positioning +

Vertical Node Positioning

-

You can enforce the vertical order of nodes via NodePosY -and their horizontal position via NodePosX:

+

You can enforce the vertical order of nodes via +NodePosY.

 links <- tribble(
   ~source, ~target, ~value,
@@ -207,12 +207,12 @@ 

Node Positioning) nodes <- tribble( - ~id, ~label, ~yorder, ~xpos, - 0, "German residents", 1, 0, - 1, "Berlin Residents", 1, 1, - 2, "Other cities (>100k)", 3, 1, - 3, "Non-Metropolitans", 2, 2, - 4, "Metropolitans (>100k)", 1, 2 + ~id, ~label, ~yorder, + 0, "German residents", 1, + 1, "Berlin Residents", 1, + 2, "Other cities (>100k)", 3, + 3, "Non-Metropolitans", 2, + 4, "Metropolitans (>100k)", 1 ) sankeyNetwork( @@ -225,9 +225,37 @@

Node Positioning NodeID = "label", NodePosY = "yorder" )

-
-
-sankeyNetwork(
+
+ +
+
+

Horizontal Node Positioning +

+

You can enforce a node’s horizontal position via +NodePosX and label the x-Axis via xAxisDomain. +However, as pointed out in this +issue, changing fontSize unfortunately does not affect +xAxisDomain.

+
+links <- tribble(
+  ~source, ~target, ~value,
+  0,       1,       3.7,
+  0,       2,       22.9,
+  0,       3,       57.7,
+  1,       4,       3.7,
+  2,       4,       22.9
+)
+
+nodes <- tribble(
+  ~id, ~label,                  ~xpos,
+  0,   "German residents",      0,
+  1,   "Berlin Residents",      1,
+  2,   "Other cities (>100k)",  1,
+  3,   "Non-Metropolitans",     2,
+  4,   "Metropolitans (>100k)", 2     
+)
+
+sankeyNetwork(
   Links = links,
   Source = "source",
   Target = "target",
@@ -235,10 +263,11 @@ 

Node Positioning units = "mio. people", Nodes = nodes, NodeID = "label", - NodePosX = "xpos", # only works in combination with align = "none" or align = "left" + NodePosX = "xpos", + xAxisDomain = c("Germany", "Berlin vs. other cities", "City vs. rural") )

-
- +
+
diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index dad94fc..cd0705e 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,7 +3,7 @@ pkgdown: 2.0.7 pkgdown_sha: ~ articles: Examples: Examples.html -last_built: 2023-08-03T08:26Z +last_built: 2023-08-03T13:48Z urls: reference: https://schmidtpaul.github.io/sankeyD3plus/reference article: https://schmidtpaul.github.io/sankeyD3plus/articles diff --git a/docs/reference/sankeyNetwork.html b/docs/reference/sankeyNetwork.html index aec0db2..8ee3092 100644 --- a/docs/reference/sankeyNetwork.html +++ b/docs/reference/sankeyNetwork.html @@ -81,7 +81,7 @@

Create a D3 JavaScript Sankey diagram

fontFamily = "Arial", fontColor = NULL, nodeWidth = 15, - nodePadding = 12, + nodePadding = NULL, nodeStrokeWidth = 1, nodeCornerRadius = 0, margin = NULL, @@ -92,7 +92,7 @@

Create a D3 JavaScript Sankey diagram

doubleclickTogglesChildren = FALSE, xAxisDomain = NULL, dragX = FALSE, - dragY = FALSE, + dragY = TRUE, height = 500, width = 800, iterations = 32, @@ -219,7 +219,7 @@

Arguments

nodePadding
-

numeric essentially influences the width height. Default is 12.

+

numeric essentially influences the width height. By default, it's the same as the fontSize parameter.

nodeStrokeWidth
@@ -268,11 +268,11 @@

Arguments

dragX
-

boolean Allow moving nodes along the x-axis?

+

boolean Allow moving nodes along the x-axis? Default is FALSE.

dragY
-

boolean Allow moving nodes along the y-axis?

+

boolean Allow moving nodes along the y-axis? Default is TRUE.

height
diff --git a/man/sankeyNetwork.Rd b/man/sankeyNetwork.Rd index f176908..79eb636 100644 --- a/man/sankeyNetwork.Rd +++ b/man/sankeyNetwork.Rd @@ -26,7 +26,7 @@ sankeyNetwork( fontFamily = "Arial", fontColor = NULL, nodeWidth = 15, - nodePadding = 12, + nodePadding = NULL, nodeStrokeWidth = 1, nodeCornerRadius = 0, margin = NULL, @@ -37,7 +37,7 @@ sankeyNetwork( doubleclickTogglesChildren = FALSE, xAxisDomain = NULL, dragX = FALSE, - dragY = FALSE, + dragY = TRUE, height = 500, width = 800, iterations = 32, @@ -119,7 +119,7 @@ scale for the nodes. See \item{nodeWidth}{numeric width of each node.} -\item{nodePadding}{numeric essentially influences the width height. Default is 12.} +\item{nodePadding}{numeric essentially influences the width height. By default, it's the same as the fontSize parameter.} \item{nodeStrokeWidth}{numeric width of the stroke around nodes.} @@ -148,9 +148,9 @@ on double-click. Does not hide incoming links of target nodes, yet.} \item{xAxisDomain}{character[] If xAxisDomain is given, an axis with those value is added to the bottom of the plot. Only sensible when also NodeXPos are given.} -\item{dragX}{boolean Allow moving nodes along the x-axis?} +\item{dragX}{boolean Allow moving nodes along the x-axis? Default is FALSE.} -\item{dragY}{boolean Allow moving nodes along the y-axis?} +\item{dragY}{boolean Allow moving nodes along the y-axis? Default is TRUE.} \item{height}{numeric height for the network graph's frame area in pixels. Default is 500.} diff --git a/vignettes/Examples.Rmd b/vignettes/Examples.Rmd index 99b808e..5ba4971 100644 --- a/vignettes/Examples.Rmd +++ b/vignettes/Examples.Rmd @@ -76,7 +76,7 @@ sankeyNetwork( ) ``` -# Link color & Node color +# Link Color & Node Color You can have link-specific and node-specific colors: @@ -112,9 +112,9 @@ sankeyNetwork( ) ``` -# Node Positioning +# Vertical Node Positioning -You can enforce the vertical order of nodes via `NodePosY` and their horizontal position via `NodePosX`: +You can enforce the vertical order of nodes via `NodePosY`. ```{r} links <- tribble( @@ -127,12 +127,12 @@ links <- tribble( ) nodes <- tribble( - ~id, ~label, ~yorder, ~xpos, - 0, "German residents", 1, 0, - 1, "Berlin Residents", 1, 1, - 2, "Other cities (>100k)", 3, 1, - 3, "Non-Metropolitans", 2, 2, - 4, "Metropolitans (>100k)", 1, 2 + ~id, ~label, ~yorder, + 0, "German residents", 1, + 1, "Berlin Residents", 1, + 2, "Other cities (>100k)", 3, + 3, "Non-Metropolitans", 2, + 4, "Metropolitans (>100k)", 1 ) sankeyNetwork( @@ -145,6 +145,31 @@ sankeyNetwork( NodeID = "label", NodePosY = "yorder" ) +``` + +# Horizontal Node Positioning + +You can enforce a node's horizontal position via `NodePosX` and label the x-Axis via `xAxisDomain`. However, as pointed out in [this issue](https://github.com/SchmidtPaul/sankeyD3plus/issues/3), changing `fontSize` unfortunately does not affect `xAxisDomain`. + +```{r} +links <- tribble( + ~source, ~target, ~value, + 0, 1, 3.7, + 0, 2, 22.9, + 0, 3, 57.7, + 1, 4, 3.7, + 2, 4, 22.9 +) + +nodes <- tribble( + ~id, ~label, ~xpos, + 0, "German residents", 0, + 1, "Berlin Residents", 1, + 2, "Other cities (>100k)", 1, + 3, "Non-Metropolitans", 2, + 4, "Metropolitans (>100k)", 2 +) + sankeyNetwork( Links = links, Source = "source", @@ -153,6 +178,8 @@ sankeyNetwork( units = "mio. people", Nodes = nodes, NodeID = "label", - NodePosX = "xpos", # only works in combination with align = "none" or align = "left" + NodePosX = "xpos", + xAxisDomain = c("Germany", "Berlin vs. other cities", "City vs. rural") ) ``` +