0

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.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

The source and target columns/variables in the links data frame should be numeric, where each value is the index (0-indexed, not 1-indexed as usual in R) of the node it refers to in the nodes data frame.

So for instance, in the first example...

head(Energy$links)
#>   source target   value
#> 1      0      1 124.729
#> 2      1      2   0.597
#> 3      1      3  26.862
#> 4      1      4 280.322
#> 5      1      5  81.144
#> 6      6      2  35.000

head(Energy$nodes)
#>                   name
#> 1 Agricultural 'waste'
#> 2       Bio-conversion
#> 3               Liquid
#> 4               Losses
#> 5                Solid
#> 6                  Gas

the first link goes from 0 (row 0 of the nodes data frame which is the node named "Agricultural 'waste'") to 1 (row 1 of the nodes data frame which is the node named "Bio-conversion")

So for the second example, you could achieve that 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)



links3$source_id <- match(links3$source, nodes3$name) - 1
links3$target_id <- match(links3$target, nodes3$name) - 1

library(networkD3)

sankeyNetwork(Links = links3, Nodes = nodes3, 
          Source = "source_id", Target = "target_id", Value = "value", 
          NodeID = "name", units = "cases", fontSize = 12, nodeWidth = 30)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56