Sankey diagram with networkD3 package won't build

I am using a sankeyNetwork

function in a networkD3

package in R using the code found here as an example . However, all I get is a blank screen. The chart is supposed to show the flow of infections between age groups (by sex). 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


source to share


4 answers


Are you sure your R console has no errors?

This works for me with two minor modifications:



  • Download curl package at the beginning

    library("curl")
    
          

  • The parameter fontsize

    doesn't seem to 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)
    
          

+1


source


The fontsize setting works, but your argument is missing capitalization: fontSize



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

      

+1


source


I solved this for me by making sure the source, purpose, and values ​​were numerical.

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

0


source


  1. you do not need it RCurl

    , read.csv

    can read directly from the URL
  2. it is probably safer to use the parameter stringsAsFactors = FALSE

    when creating data.frame nodes
  3. as others have already pointed out, you have to make sure that the source and target variables in the referenced data are numeric and that they are at index zero
  4. as others have noted, the font size parameter has the correct name fontSize

  5. I have provided a more direct way to create link data using numeric node indices in data.frame nodes.
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

0


source







All Articles