我想制作一个图表,其中不同的链接类别有不同的颜色,我用它作为输入(样本):
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并绘制它,这将给出:
正如您在此网络中看到的,链接是相互叠加的。因此,不可能看到差异。所以我想绘制彼此相邻的链接,而不是彼此重叠。
我无法提供整个数据集,但我想一个简单的例子就足够了:
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)
不确定这是否是R或更多JS,因此我将其标记为