Based on the example below:
# Load package
library(networkD3)
# Load energy projection data
URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
Energy <- jsonlite::fromJSON(URL)
# Now we have 2 data frames: a 'links' data frame with 3 columns (from, to, value), and a 'nodes' data frame that gives the name of each node.
head( Energy$links )
head( Energy$nodes )
# Thus we can plot it
p <- sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30)
p
I do not underastand how the indexing in this example happens since there is no connection between the nodes
name
and the indexes (source
and target
of the links
dataframe). Also how does this 0
is explained since source
and target
are indexes?
I have tried to create my own sankey chart with:
name<-c("J","B","A")
nodes3<-data.frame(name)
source<-c("B","B","J")
target<-c("A","A","B")
value<-c(5,6,7)
links3<-data.frame(source,target,value)
p <- sankeyNetwork(Links = data.frame(links3), Nodes = data.frame(nodes3), Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "cases", fontSize = 12, nodeWidth = 30)
p
But while everything seems to run I get no plot in RStudio Viewer and no error message.