使用R绘制Sankey图

时间:2019-12-17 18:01:17

标签: r plot sankey-diagram

由于讨论了on this thread的一个神秘问题,我目前无法使用R绘制Sankey图。

由于有些人报告说无法在其R环境中复制我的问题,并且在这一点上我愿意为我的图表做任何事情,所以它发生在我身上,也许有人可以帮助我绘制所需的实际代码,并且给我发图片...

所以,这是代码:

library(networkD3)

#List of nodes (portfolios & targets)
nodes = data.frame("finalnodes" =
                     c("portfolio1", #0
                       "portfolio2", #1
                       "portfolio3", #2
                       "portfolio4", #3
                       "target1", #4
                       "target2", #5
                       "target3", #6
                       "target4", #7
                       "target5", #8
                       "target6", #9
                       "target7", #10
                       "target8" #11
                     ))

#List of links
links = as.data.frame(matrix(c(
  0,4,1,
  2,10,1,
  2,6,1,
  3,4,1,
  3,5,1,
  3,7,1,
  3,8,1,
  3,9,1,
  3,11,1),
  byrow = TRUE, ncol = 3))

#Column names of data frame
names(links) = c("source", "target", "value")

#check
links

#Sankey Diagram
#Colour scale
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);")

#Diagram
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "source", Target = "target", Value = "value", NodeID = "finalnodes",
              fontSize = 14, nodeWidth = 10, nodePadding = 140, iterations = 0,
              colourScale = colourScale)

不用说,当我绘制它时,格式就搞砸了。

我得到的情节:

img

0 个答案:

没有答案