From 86d0c2b1c28bd1ae3fb000fe6d8b7b0a01114b1b Mon Sep 17 00:00:00 2001 From: "Otenko, Ivan" Date: Tue, 28 Jun 2016 19:52:59 +0100 Subject: [PATCH 1/4] Adding Node Description to sankeyNetwork Adding Node Description as a parameter to sankeyNetwork function --- R/sankeyNetwork.R | 9 ++++++++- inst/htmlwidgets/sankeyNetwork.js | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 45e19949..51444e41 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -16,6 +16,8 @@ #' frame for how far away the nodes are from one another. #' @param NodeID character string specifying the node IDs in the \code{Nodes}. #' data frame. Must be 0-indexed. +#' #' @param NodeDesc character string specifying the description of the Node Id +#' in the \code{Nodes} data frame. If not specified defaults to NodeID. #' @param NodeGroup character string specifying the node groups in the #' \code{Nodes}. Used to color the nodes in the network. #' @param LinkGroup character string specifying the groups in the @@ -71,7 +73,7 @@ #' @export sankeyNetwork <- function(Links, Nodes, Source, Target, Value, - NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = "", + NodeID, NodeDesc = NULL, NodeGroup = NodeID, LinkGroup = NULL, units = "", colourScale = JS("d3.scale.category20()"), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, height = NULL, width = NULL, iterations = 32) @@ -111,6 +113,11 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value, NodesDF <- data.frame(Nodes[, NodeID]) names(NodesDF) <- c("name") + # add node description if specified + if (!is.missing(NodeDesc)) { + NodesDF$desc <- Nodes[, NodeDesc] + } else {NodesDF$desc <- Nodes[, NodeID]} + # add node group if specified if (is.character(NodeGroup)) { NodesDF$group <- Nodes[, NodeGroup] diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index dee53c4d..d9570451 100644 --- a/inst/htmlwidgets/sankeyNetwork.js +++ b/inst/htmlwidgets/sankeyNetwork.js @@ -171,7 +171,7 @@ HTMLWidgets.widget({ .style("opacity", 0.9) .style("cursor", "move") .append("title") - .text(function(d) { return d.name + "
" + format(d.value) + + .text(function(d) { return d.desc + "\n" + format(d.value) + " " + options.units; }); node.append("text") From 124042eaf9d0c0b66d6b437b1cbacb263c7795f0 Mon Sep 17 00:00:00 2001 From: "Otenko, Ivan" Date: Tue, 28 Jun 2016 19:57:43 +0100 Subject: [PATCH 2/4] fixing missing check fixing missing check --- R/sankeyNetwork.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 51444e41..2c032bf4 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -114,7 +114,7 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value, names(NodesDF) <- c("name") # add node description if specified - if (!is.missing(NodeDesc)) { + if (!missing(NodeDesc)) { NodesDF$desc <- Nodes[, NodeDesc] } else {NodesDF$desc <- Nodes[, NodeID]} From 04fb6551a8c30547e9dc517325819b85a531226d Mon Sep 17 00:00:00 2001 From: "Otenko, Ivan" Date: Tue, 28 Jun 2016 20:20:40 +0100 Subject: [PATCH 3/4] Updating docs Updating Docs with NodeDesc details --- R/sankeyNetwork.R | 2 +- man/sankeyNetwork.Rd | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 2c032bf4..d9f3a857 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -16,7 +16,7 @@ #' frame for how far away the nodes are from one another. #' @param NodeID character string specifying the node IDs in the \code{Nodes}. #' data frame. Must be 0-indexed. -#' #' @param NodeDesc character string specifying the description of the Node Id +#' @param NodeDesc character string specifying the description of each Node ID #' in the \code{Nodes} data frame. If not specified defaults to NodeID. #' @param NodeGroup character string specifying the node groups in the #' \code{Nodes}. Used to color the nodes in the network. diff --git a/man/sankeyNetwork.Rd b/man/sankeyNetwork.Rd index b35e188c..b1869124 100644 --- a/man/sankeyNetwork.Rd +++ b/man/sankeyNetwork.Rd @@ -8,7 +8,7 @@ D3.js was created by Michael Bostock. See \url{http://d3js.org/} and, more specifically for Sankey diagrams \url{http://bost.ocks.org/mike/sankey/}. } \usage{ -sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID, +sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, NodeDesc = NULL, NodeGroup = NodeID, LinkGroup = NULL, units = "", colourScale = JS("d3.scale.category20()"), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, height = NULL, width = NULL, iterations = 32) @@ -36,6 +36,9 @@ frame for how far away the nodes are from one another.} \item{NodeID}{character string specifying the node IDs in the \code{Nodes}. data frame. Must be 0-indexed.} +\item{NodeDesc}{character string specifying the description of each node ID +in the \code{Nodes} data frame. If not specified defaults to NodeID.} + \item{NodeGroup}{character string specifying the node groups in the \code{Nodes}. Used to color the nodes in the network.} From 5e54ab739e9a140a440f916703f12b8581035b2f Mon Sep 17 00:00:00 2001 From: "Otenko, Ivan" Date: Thu, 30 Jun 2016 13:02:49 +0100 Subject: [PATCH 4/4] Adding JSON file with Node Description example Adding JSON file with Node Description example --- JSONdata/energywDesc.json | 538 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 538 insertions(+) create mode 100644 JSONdata/energywDesc.json diff --git a/JSONdata/energywDesc.json b/JSONdata/energywDesc.json new file mode 100644 index 00000000..fc36a2bd --- /dev/null +++ b/JSONdata/energywDesc.json @@ -0,0 +1,538 @@ +{ + "nodes": [ + { + "name": "Agricultural 'waste'", + "desc": "Agricultural 'waste'" + }, + { + "name": "Bio-conversion", + "desc": "Bio-conversion\n46.8% - UK land based bioenergy\n32.1% - Agricultural 'waste'\n20.0% - Other waste\n1.1% - Marine algae" + }, + { + "name": "Liquid", + "desc": "Liquid\n94.5% - Oil\n5.4% - Biofuel imports\n0.1% - Bio-conversion" + }, + { + "name": "Losses", + "desc": "Losses\n89.6% - Thermal generation\n6.5% - Electricity grid\n3.1% - Bio-conversion\n0.7% - H2 conversion\n0.2% - Gas" + }, + { + "name": "Solid", + "desc": "Solid\n62.6% - Bio-conversion\n16.9% - Coal\n12.6% - Other waste\n7.8% - Biomass imports" + }, + { + "name": "Gas", + "desc": "Gas\n60.2% - Ngas\n39.8% - Bio-conversion" + }, + { + "name": "Biofuel imports", + "desc": "Biofuel imports" + }, + { + "name": "Biomass imports", + "desc": "Biomass imports" + }, + { + "name": "Coal imports", + "desc": "Coal imports" + }, + { + "name": "Coal", + "desc": "Coal\n84.6% - Coal reserves\n15.4% - Coal imports" + }, + { + "name": "Coal reserves", + "desc": "Coal reserves" + }, + { + "name": "District heating", + "desc": "District heating\n100.0% - Thermal generation" + }, + { + "name": "Industry", + "desc": "Industry\n60.1% - Electricity grid\n21.3% - Liquid\n8.5% - Gas\n8.2% - Solid\n1.9% - District heating" + }, + { + "name": "Heating and cooling - commercial", + "desc": "Heating and cooling - commercial\n52.7% - Pumped heat\n30.5% - Electricity grid\n16.8% - District heating\n0.1% - Gas" + }, + { + "name": "Heating and cooling - homes", + "desc": "Heating and cooling - homes\n51.9% - Pumped heat\n30.6% - Electricity grid\n12.4% - District heating\n5.2% - Solar Thermal" + }, + { + "name": "Electricity grid", + "desc": "Electricity grid\n57.3% - Thermal generation\n31.5% - Wind\n6.5% - Solar PV\n2.1% - Wave\n1.0% - Tidal\n0.8% - Geothermal\n0.8% - Hydro" + }, + { + "name": "Over generation / exports", + "desc": "Over generation / exports\n100.0% - Electricity grid" + }, + { + "name": "H2 conversion", + "desc": "H2 conversion\n100.0% - Electricity grid" + }, + { + "name": "Road transport", + "desc": "Road transport\n69.8% - Liquid\n19.4% - Electricity grid\n10.7% - H2" + }, + { + "name": "Agriculture", + "desc": "Agriculture\n40.0% - Electricity grid\n33.0% - Liquid\n19.0% - Gas\n8.0% - Solid" + }, + { + "name": "Rail transport", + "desc": "Rail transport\n64.1% - Electricity grid\n35.9% - Liquid" + }, + { + "name": "Lighting & appliances - commercial", + "desc": "Lighting & appliances - commercial\n100.0% - Electricity grid" + }, + { + "name": "Lighting & appliances - homes", + "desc": "Lighting & appliances - homes\n100.0% - Electricity grid" + }, + { + "name": "Gas imports", + "desc": "Gas imports" + }, + { + "name": "Ngas", + "desc": "Ngas\n66.9% - Gas reserves\n33.1% - Gas imports" + }, + { + "name": "Gas reserves", + "desc": "Gas reserves" + }, + { + "name": "Thermal generation", + "desc": "Thermal generation\n60.3% - Nuclear\n28.7% - Solid\n10.9% - Gas" + }, + { + "name": "Geothermal", + "desc": "Geothermal" + }, + { + "name": "H2", + "desc": "H2\n100.0% - H2 conversion" + }, + { + "name": "Hydro", + "desc": "Hydro" + }, + { + "name": "International shipping", + "desc": "International shipping\n100.0% - Liquid" + }, + { + "name": "Domestic aviation", + "desc": "Domestic aviation\n100.0% - Liquid" + }, + { + "name": "International aviation", + "desc": "International aviation\n100.0% - Liquid" + }, + { + "name": "National navigation", + "desc": "National navigation\n100.0% - Liquid" + }, + { + "name": "Marine algae", + "desc": "Marine algae" + }, + { + "name": "Nuclear", + "desc": "Nuclear" + }, + { + "name": "Oil imports", + "desc": "Oil imports" + }, + { + "name": "Oil", + "desc": "Oil\n82.4% - Oil imports\n17.6% - Oil reserves" + }, + { + "name": "Oil reserves", + "desc": "Oil reserves" + }, + { + "name": "Other waste", + "desc": "Other waste" + }, + { + "name": "Pumped heat", + "desc": "Pumped heat" + }, + { + "name": "Solar PV", + "desc": "Solar PV\n100.0% - Solar" + }, + { + "name": "Solar Thermal", + "desc": "Solar Thermal\n100.0% - Solar" + }, + { + "name": "Solar", + "desc": "Solar" + }, + { + "name": "Tidal", + "desc": "Tidal" + }, + { + "name": "UK land based bioenergy", + "desc": "UK land based bioenergy" + }, + { + "name": "Wave", + "desc": "Wave" + }, + { + "name": "Wind", + "desc": "Wind" + } + ], + "links": [ + { + "source": 0, + "target": 1, + "value": 124.729 + }, + { + "source": 1, + "target": 2, + "value": 0.597 + }, + { + "source": 1, + "target": 3, + "value": 26.862 + }, + { + "source": 1, + "target": 4, + "value": 280.322 + }, + { + "source": 1, + "target": 5, + "value": 81.144 + }, + { + "source": 6, + "target": 2, + "value": 35 + }, + { + "source": 7, + "target": 4, + "value": 35 + }, + { + "source": 8, + "target": 9, + "value": 11.606 + }, + { + "source": 10, + "target": 9, + "value": 63.965 + }, + { + "source": 9, + "target": 4, + "value": 75.571 + }, + { + "source": 11, + "target": 12, + "value": 10.639 + }, + { + "source": 11, + "target": 13, + "value": 22.505 + }, + { + "source": 11, + "target": 14, + "value": 46.184 + }, + { + "source": 15, + "target": 16, + "value": 104.453 + }, + { + "source": 15, + "target": 14, + "value": 113.726 + }, + { + "source": 15, + "target": 17, + "value": 27.14 + }, + { + "source": 15, + "target": 12, + "value": 342.165 + }, + { + "source": 15, + "target": 18, + "value": 37.797 + }, + { + "source": 15, + "target": 19, + "value": 4.412 + }, + { + "source": 15, + "target": 13, + "value": 40.858 + }, + { + "source": 15, + "target": 3, + "value": 56.691 + }, + { + "source": 15, + "target": 20, + "value": 7.863 + }, + { + "source": 15, + "target": 21, + "value": 90.008 + }, + { + "source": 15, + "target": 22, + "value": 93.494 + }, + { + "source": 23, + "target": 24, + "value": 40.719 + }, + { + "source": 25, + "target": 24, + "value": 82.233 + }, + { + "source": 5, + "target": 13, + "value": 0.129 + }, + { + "source": 5, + "target": 3, + "value": 1.401 + }, + { + "source": 5, + "target": 26, + "value": 151.891 + }, + { + "source": 5, + "target": 19, + "value": 2.096 + }, + { + "source": 5, + "target": 12, + "value": 48.58 + }, + { + "source": 27, + "target": 15, + "value": 7.013 + }, + { + "source": 17, + "target": 28, + "value": 20.897 + }, + { + "source": 17, + "target": 3, + "value": 6.242 + }, + { + "source": 28, + "target": 18, + "value": 20.897 + }, + { + "source": 29, + "target": 15, + "value": 6.995 + }, + { + "source": 2, + "target": 12, + "value": 121.066 + }, + { + "source": 2, + "target": 30, + "value": 128.69 + }, + { + "source": 2, + "target": 18, + "value": 135.835 + }, + { + "source": 2, + "target": 31, + "value": 14.458 + }, + { + "source": 2, + "target": 32, + "value": 206.267 + }, + { + "source": 2, + "target": 19, + "value": 3.64 + }, + { + "source": 2, + "target": 33, + "value": 33.218 + }, + { + "source": 2, + "target": 20, + "value": 4.413 + }, + { + "source": 34, + "target": 1, + "value": 4.375 + }, + { + "source": 24, + "target": 5, + "value": 122.952 + }, + { + "source": 35, + "target": 26, + "value": 839.978 + }, + { + "source": 36, + "target": 37, + "value": 504.287 + }, + { + "source": 38, + "target": 37, + "value": 107.703 + }, + { + "source": 37, + "target": 2, + "value": 611.99 + }, + { + "source": 39, + "target": 4, + "value": 56.587 + }, + { + "source": 39, + "target": 1, + "value": 77.81 + }, + { + "source": 40, + "target": 14, + "value": 193.026 + }, + { + "source": 40, + "target": 13, + "value": 70.672 + }, + { + "source": 41, + "target": 15, + "value": 59.901 + }, + { + "source": 42, + "target": 14, + "value": 19.263 + }, + { + "source": 43, + "target": 42, + "value": 19.263 + }, + { + "source": 43, + "target": 41, + "value": 59.901 + }, + { + "source": 4, + "target": 19, + "value": 0.882 + }, + { + "source": 4, + "target": 26, + "value": 400.12 + }, + { + "source": 4, + "target": 12, + "value": 46.477 + }, + { + "source": 26, + "target": 15, + "value": 525.531 + }, + { + "source": 26, + "target": 3, + "value": 787.129 + }, + { + "source": 26, + "target": 11, + "value": 79.329 + }, + { + "source": 44, + "target": 15, + "value": 9.452 + }, + { + "source": 45, + "target": 1, + "value": 182.01 + }, + { + "source": 46, + "target": 15, + "value": 19.013 + }, + { + "source": 47, + "target": 15, + "value": 289.366 + } + ] +}