Larissa Carvalho
Larissa Carvalho

Reputation: 35

sankeyNetwork add legend of link width value in R

I have created a sankey chart to display the statistically significant relationships between my nodes and path. The connector line width indicates de (log10)P-value of each interaction, i.e. the wider the line the higher the (log10)P-value. I would like to add a legend indicating the connector width values, similar to this: enter image description here

Here is the code that I used to create the sankey chart:

library(networkD3)
library(htmlwidgets)



data <- read.csv("sankeydata.csv")
path <- unique(as.character(data$to))
nodes <- data.frame(node = c(0:43), 
                    name = c(path, "T0", "F_T24", "F_T72", "F_T96", "P_T24", "P_T48"))


links <- data[ , c("node.x", "node.y", "logpvalue")]
colnames(links) <- c("source", "target", "logpvalue")

sn <- sankeyNetwork(Links = links, Nodes = nodes,
                    Source = "target", Target = "source",
                    Value = "logpvalue", NodeID = "name", fontFamily = "Arial",
                    fontSize= 12, nodeWidth = 20, margin = list(left = 260))
sn

sankey <- onRender(
  sn,
  '
  function(el, x) {
  d3.selectAll(".node text").attr("text-anchor", "begin").attr("x", 21);
  }
  '
)
sankey

library(webshot)
webshot("sankey.html", "sankeySO.png")

Here is the chart: enter image description here

I have no experience with JavaScript, so I would really appreciate any help!

Thank you!

Upvotes: 1

Views: 241

Answers (0)

Related Questions