Sankey Diagram with networkD3 package will not plot

4.3k views Asked by At

I am using the sankeyNetwork function in the networkD3 package in R using as an example the code found here. However, all I get is a blank screen. The diagram is supposed to show the flow of infections between age groups (by gender). My code is as below:

library(RCurl)
library(networkD3)

edges <- read.csv(curl("https://raw.githubusercontent.com/kilimba/data/master/infection_flows.csv"),stringsAsFactors = FALSE )

nodes = data.frame(ID = unique(c(edges$Source, edges$Target)))

nodes$indx =0
for (i in 1:nrow(nodes)){
  nodes[i,]["indx"] = i - 1
}

edges2 <- merge(edges,nodes,by.x = "Source",by.y = "ID")
edges2$Source <-NULL
names(edges2) <- c("target","value","source")
edges2 <- merge(edges2,nodes,by.x = "target",by.y = "ID")
edges2$target <- NULL
names(edges2) <- c("value","source","target")

nodes$indx <- NULL
# Plot
sankeyNetwork(Links = edges2, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "ID",
              width = 700, fontsize = 12, nodeWidth = 30)
4

There are 4 answers

0
mcharl02 On

Adjusting fontsize does work, but your argument is missing a capitalization: fontSize

sankeyNetwork(Links = edges2, Nodes = nodes,
  Source = "source", Target = "target",
  Value = "value", NodeID = "ID",
  width = 700, fontSize = 12,
  nodeWidth = 30)
1
Magdalena On

I solved it for me by making sure that source, target and values were all numeric.

For example: Energy$links$value <- as.numeric(Energy$links$value)

0
Daniel Sparing On

Are you sure there are no errors printed in your R console?

This works for me with two small modifications:

  1. Load the curl package as well at the beginning

    library("curl")
    
  2. The fontsize parameter apparently does not work and should be removed.

    # Plot
    sankeyNetwork(Links = edges2, Nodes = nodes,
          Source = "source", Target = "target",
          Value = "value", NodeID = "ID",
          width = 700, #fontsize = 12,
          nodeWidth = 30)
    
0
CJ Yetman On
  1. you do not need RCurl, read.csv is able to read directly from a URL
  2. it's probably safer to use the stringsAsFactors = FALSE option when creating the nodes data.frame
  3. as others have pointed out, you must make sure that the source and target variables in the links data are numeric, and that they are zero-indexed
  4. as others have pointed out, the font size parameter is properly named fontSize
  5. I have provided a more direct way of creating the links data with numeric indexes of the nodes in the nodes data.frame
library(networkD3)

edges <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/infection_flows.csv",stringsAsFactors = FALSE)

nodes = data.frame(ID = unique(c(edges$Source, edges$Target)), stringsAsFactors = FALSE)

edges$Source <- match(edges$Source, nodes$ID) - 1
edges$Target <- match(edges$Target, nodes$ID) - 1

sankeyNetwork(Links = edges, Nodes = nodes,
              Source = "Source", Target = "Target",
              Value = "Value", NodeID = "ID",
              width = 700, fontSize = 12, nodeWidth = 30)

enter image description here