How to add Axis labels using networkD3 in R
In the R package, there are already many visualization libraries present. For example ggplot, Plotly, igraph, etc. But not all libraries are used to create network-type graphs and it is also not in 3D plots. That is the main objective behind why it is made.
Syntax: sankeyNetwork( Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = “”, colourScale = NULL, fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, height = NULL, width = NULL, iterations = 32, sinksRight = TRUE )
- Links: dataframe represent link between source and target (node).
- Nodes: dataframe contain node properties
- Source: character string of source node
- Target: character string of target node
- Value: character string of distance value between nodes
- NodeID: node name
- NodeGroup: character string of the nodeIDs in nodes
- LinkGroup: character string for link between groups
- Units: character string to specify physical unit(value)
- colourscale: specify color scale
- nodeWidth: width of node
- nodePadding: padding between node
- sinksRight: Boolean. If it is True, last node moves to right border of graph
Creating a simple sankeyNetwork graph with Title, Subtitle, and Labels
Step: 1 Import the necessary library in Rstudio.
Step: 2 Create network data in a source-target data frame format.
- Here, we are linking A->D, B->E, and C->F nodes and assigning 3 values on each link. final table will look like myDataframe table image.
- The node table is containing every node entry uniquely.
- IDsource and IDtarget is a string character form of the source and the target node.
- match function will match take two-column and create a matching table from 1. But we are subtracting by ‘-1’ so that it can start with 0th character like for A node it is 0, B node it is 1 and so on.
Step: 3 Create a simple Sankey network chart.
As per the above sankeyNetwork syntax, we are assigning some parameters to generate our output.
Step: 4 Add Title and Subtitle in graph1.
- To assign a title, we can use the prependContent function from the htmlwidgets tool.
- Create two h1 and h2 tags with text description and style.
- prepend these tags in our above graph1 by putting all these tags together in the prependContent function.
Step: 5 Add left and right labels in networkGraph.
- create left-tag as leftTx and right-tag as rightTx.
- Now, to combine leftTx, rightTx and graph2, use combineWidget from manipulatewidget library.
- combineWidget take, graph, title, left-column, right-column, and row-number as parameters and combine all these instances into one.
Syntax: combineWidgets( list = NULL, nrow = NULL, ncol = NULL, title = NULL, rowsize = 1, colsize = 1, byrow = TRUE, titleCSS = “”, header = NULL, footer = NULL, leftCol = NULL, rightCol = NULL, width = NULL, height = NULL )