Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NodePosY argument to sankeyNetwork.R to control y position of nodes #21

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions R/sankeyNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ NULL
#' \code{Links}. Used to color the links in the network.
#' @param NodePosX character specifying a column in the \code{Nodes} data
#' frame that specifies the 0-based ordering of the nodes along the x-axis.
#' @param NodePosY character specifying a column in the \code{Nodes} data
#' frame that specifies the 0-based ordering of the nodes along the y-axis.
#' @param NodeValue character specifying a column in the \code{Nodes} data
#' frame with the value/size of each node. If \code{NULL}, the value is
#' calculated based on the maximum of the sum of incoming and outoging
Expand Down Expand Up @@ -156,7 +158,7 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
height = NULL, width = NULL, iterations = 32, zoom = FALSE, align = "justify",
showNodeValues = TRUE, linkType = "bezier", curvature = .5, linkColor = "#A0A0A0",
nodeLabelMargin = 2, linkOpacity = .5, linkGradient = FALSE, nodeShadow = FALSE,
scaleNodeBreadthsByString = FALSE, xScalingFactor = 1,
scaleNodeBreadthsByString = FALSE, xScalingFactor = 1, NodePosY = NULL,
yOrderComparator = NULL)
{
# Check if data is zero indexed
Expand Down Expand Up @@ -201,6 +203,13 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
NodesDF$group <- Nodes[, NodeGroup]
}

if (is.character(NodePosY)) {
NodesDF$posY <- Nodes[, NodePosY]
orderByPosY <- TRUE
}else{
orderByPosY <- FALSE
}

if (is.character(NodePosX)) {
NodesDF$posX <- Nodes[, NodePosX]
}
Expand Down Expand Up @@ -228,7 +237,7 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
margin <- margin_handler(margin)

# create options
options = list(NodeID = NodeID, NodeGroup = NodeGroup, LinkGroup = LinkGroup,
options = list(NodeID = NodeID, NodeGroup = NodeGroup, LinkGroup = LinkGroup, orderByPosY = orderByPosY,
colourScale = colourScale, fontSize = fontSize, fontFamily = fontFamily, fontColor = fontColor,
nodeWidth = nodeWidth, nodePadding = nodePadding, nodeStrokeWidth = nodeStrokeWidth,
nodeCornerRadius = nodeCornerRadius, dragX = dragX, dragY = dragY,
Expand Down
15 changes: 13 additions & 2 deletions inst/htmlwidgets/lib/d3-sankey/src/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ d3.sankey = function() {
linkType = 'bezier',
reverse = false,
orderByPath = false,
orderByPosY = false,
scaleNodeBreadthsByString = false,
curvature = .5,
showNodeValues = false,
Expand All @@ -20,7 +21,13 @@ d3.sankey = function() {
yOrderComparator = function ascendingDepth(a, b) {
if (orderByPath) {
return ( a.path < b.path ? -1 : (a.path > b.path ? 1 : 0 ));
} else {
} else if(orderByPosY) {
if(a.posY != b.posY){
return a.posY - b.posY;
}else{
return a.y - b.y
}
}else {
return a.y - b.y;
}
}
Expand Down Expand Up @@ -54,7 +61,11 @@ d3.sankey = function() {
orderByPath = _;
return sankey;
};

sankey.orderByPosY = function(_) {
if (!arguments.length) return orderByPosY;
orderByPosY = _;
return sankey;
};
sankey.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = _;
Expand Down
1 change: 1 addition & 0 deletions inst/htmlwidgets/sankeyNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ HTMLWidgets.widget({
.scaleNodeBreadthsByString(options.scaleNodeBreadthsByString)
.curvature(options.curvature)
.orderByPath(options.orderByPath)
.orderByPosY(options.orderByPosY)
.showNodeValues(options.showNodeValues)
.nodeCornerRadius(options.nodeCornerRadius);

Expand Down