细灰线作为在 R 中绘制 sankeyNetwork 的链接 [英] Thin gray lines as links plotting sankeyNetwork in R

查看:109
本文介绍了细灰线作为在 R 中绘制 sankeyNetwork 的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是 R 新手,所以我可能忽略了一些非常明显的东西......

我目前正在使用 R 处理 sankeyNetwork 图,我面临一个几乎似乎是错误的问题,但我完全一无所知......我在谷歌上搜索了很多,并没有找到其他人报告同样的情况......

问题是在我的代码中,我目前有 7 个节点和 5 个链接.当我绘制图表时,一切正常:

Disclaimer: I'm an R newbie, so I may be overlooking something really obvious here...

I am currently working on a sankeyNetwork diagram using R, and I am facing a problem that almost seems to be a bug, but I'm completely clueless... I've googled extensively, and haven't been able to find anybody else reporting the same...

The problem is that in my code I currently have 7 nodes, and 5 links. When I plot the diagram, everything works fine: Plot 1, everything working fine

This is the code for Plot 1:

library(networkD3)

# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
                       c("portfolio1", # 0
                         "portfolio2", # 1
                         "portfolio3", # 2
                         "portfolio4", # 3
                         "target1", # 4
                         "target2", # 5
                         "target3" # 6
                       ))

# List of links
links = as.data.frame(matrix(c(
    0,4,2,
    1,6,1,
    2,3,1,
    2,6,1,
    3,5,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 = "trialnodes",
              fontSize = 14, nodeWidth = 10, nodePadding = 140, iterations = 0,
              colourScale = colourScale)

however, as soon as I add one more node, and 1 more link, the plot's format becomes completely broken, showing the links between nodes as simple gray thin lines (not representing the Value). The nodeWidth and nodePadding specifications also get ignored :( Plot 2, links as thin gray lines

This is the code for Plot 2:

library(networkD3)
# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
                       c("portfolio1", # 0
                         "portfolio2", # 1
                         "portfolio3", # 2
                         "portfolio4", # 3
                         "target1", # 4
                         "target2", # 5
                         "target3", # 6
                         "target4" # 7
                       ))

# List of links
links = as.data.frame(matrix(c(
    0,4,2,
    0,7,1,
    1,6,1,
    2,3,1,
    2,6,1,
    3,5,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 = "trialnodes",
              fontSize = 14, nodeWidth = 10, nodePadding = 140, iterations = 0,
              colourScale = colourScale)

Can anybody spot what's going on? I hope someone can help... I'm desperate D: Thank you very much in advance! :)

解决方案

Either reduce your nodePadding value to something reasonable, or make the viewer/browser-window size large enough to show the max number of nodes you have in a column * 140 pixels (plus some for the actual node) and then refresh (in your second example that comes out to ~600px).

library(networkD3)
# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
                     c("portfolio1", # 0
                       "portfolio2", # 1
                       "portfolio3", # 2
                       "portfolio4", # 3
                       "target1", # 4
                       "target2", # 5
                       "target3", # 6
                       "target4" # 7
                     ))

# List of links
links = as.data.frame(matrix(c(
  0,4,2,
  0,7,1,
  1,6,1,
  2,3,1,
  2,6,1,
  3,5,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 = "trialnodes",
              fontSize = 14, nodeWidth = 10, nodePadding = 14, iterations = 0,
              colourScale = colourScale)

这篇关于细灰线作为在 R 中绘制 sankeyNetwork 的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆