4

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)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Tumaini Kilimba
  • 195
  • 2
  • 15
  • I face the same problem. sankeyNetwork works with a smaller sample of the dataset I am using but if tried with the full set, it displays a blank screen. – Kinjal Jul 14 '15 at 09:15

4 Answers4

1

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)
    
Daniel Sparing
  • 2,163
  • 15
  • 20
1

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)
mcharl02
  • 128
  • 1
  • 12
1
  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

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

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)

Magdalena
  • 401
  • 4
  • 2
  • The asker has posted a code and the answer should be shown in that code, check other answerers – Ibo Feb 21 '18 at 17:56