Networkd3链接不是彼此叠加而是彼此相邻

时间:2017-12-31 13:23:10

标签: javascript r graph networkd3

我想制作一个图表,其中不同的链接类别有不同的颜色,我用它作为输入(样本):

  source target  cat weight
  <fctr> <fctr>     <chr>  <dbl>
1  human  water       1     30
2  human  water       2     49
3  human  water       3     2
4  human  water       4    14
5  human  water       5    19

然后我将这些数据转换为节点,链接和link.colors并绘制它,这将给出: enter image description here
正如您在此网络中看到的,链接是相互叠加的。因此,不可能看到差异。所以我想绘制彼此相邻的链接,而不是彼此重叠。

我无法提供整个数据集,但我想一个简单的例子就足够了:

library(networkD3)

link <- read.table(text = '  source target value
                        1      0      1    30
                        2      0      1    49', header = T)

nodes <- read.table(text = '   name group size
                              1 human     1   10
                              2 water     1   10', header = T)

link.colors <- c('#e6194b','#3cb44b')

network <- forceNetwork(Links = link, Nodes = nodes,
                        Source = "source", Target = "target",
                        Value = "value", NodeID = "name",
                        Group = "group", opacity = 0.6,
                        zoom = TRUE, charge = -60, 
                        fontSize = 15, linkColour = link.colors)

# plot in R
network

# zooming doesn't work in Rstudio for me so I write it to a file instead of plotting it
# and then open it in my browser
saveNetwork(network, file = 'network_example.html', selfcontained = FALSE)

此代码将生成左侧网络,而我希望有类似右侧的内容 enter image description here

不确定这是否是R或更多JS,因此我将其标记为

0 个答案:

没有答案