这是我第一次可视化网络,所以我不熟悉 networkD3 包。
使用下面的两个数据帧,我可以生成所需的网络可视化。
唯一的问题是,虽然链接距离和宽度与预期成正比,但它们仍然太小,这意味着节点重叠并相互覆盖太多。
我尝试使用静态数字使用力网络函数中的参数更改链接距离,但问题是它们的距离与链接值不成比例。我不确定如何引用值列,并且完全不熟悉javascript,所以我不确定如何更改参数的默认值。
"charge"变量略有帮助,但不能完全解决问题。
有没有办法将链接距离乘以默认设置的任何距离,以便可视化按比例更加分散?
链接 DF
structure(list(source = c("0", "0", "8", "8", "8", "8", "8",
"8", "8", "8", "8", "8", "5", "5", "5", "5", "5", "5", "5", "5",
"5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5",
"5", "5", "5", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9",
"9", "9", "9", "9", "11", "11", "11", "11", "11", "11", "11",
"11", "11", "11", "11", "11", "11", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "7", "7", "7",
"7", "7", "7", "7", "7", "7", "2", "1", "3", "6", "6", "6", "6",
"6", "6", "6"), target = c("12", "14", "12", "20", "28", "18",
"24", "23", "35", "29", "27", "30", "12", "20", "28", "38", "25",
"21", "26", "14", "39", "46", "24", "17", "16", "40", "23", "35",
"42", "29", "36", "13", "37", "32", "30", "43", "12", "20", "28",
"14", "16", "23", "29", "44", "30", "12", "19", "20", "18", "38",
"25", "26", "31", "24", "40", "23", "29", "15", "27", "30", "12",
"19", "18", "38", "25", "22", "31", "24", "41", "34", "40", "29",
"33", "12", "19", "20", "18", "38", "25", "26", "22", "24", "41",
"47", "29", "36", "15", "30", "12", "20", "24", "16", "23", "36",
"15", "32", "30", "12", "12", "12", "12", "25", "17", "35", "36",
"30", "45"), value = c(4632L, 1L, 2509L, 1L, 2L, 1L, 1248L, 90L,
254L, 2L, 1L, 1L, 8464L, 556L, 2L, 1L, 5L, 11L, 38L, 2461L, 249L,
10L, 14L, 456L, 865L, 134L, 386L, 5L, 160L, 2L, 32L, 795L, 151L,
346L, 194L, 4L, 482L, 1L, 35L, 1L, 1L, 1L, 1L, 14L, 8L, 1640L,
1L, 4L, 1L, 10L, 4L, 1L, 1L, 3L, 1L, 3L, 18L, 1L, 1046L, 194L,
18882L, 434L, 1L, 97L, 1L, 3698L, 821L, 1L, 50L, 143L, 70L, 2L,
450L, 8332L, 3L, 1L, 1781L, 584L, 2L, 1L, 1L, 1L, 80L, 7L, 3363L,
169L, 1L, 1345L, 1251L, 1L, 1L, 17L, 288L, 12L, 201L, 21L, 1L,
980L, 7841L, 558L, 378L, 72L, 39L, 13L, 38L, 1L, 11L)), .Names = c("source",
"target", "value"), row.names = c(NA, -107L), .internal.selfref = <pointer:
0x0000000000120788>, class = c("tbl_df",
"tbl", "data.frame"))
节点 DF
structure(list(name = c("Fear", "Frontier", "Nanny", "Job", "Yard",
"Airport", "Half Pint", "Commando", "Fast Food", "Basketball",
"Bachelorette", "Diva", "Baggage", "College", "Octane", "Clean",
"Sister", "Army", "Drama", "Backyard", "Pirate", "Shark", "Project",
"Model", "Survival", "Justice", "Mom", "New York", "Jersey",
"Ax", "Warrior", "Ancient", "Pawn", "Throttle", "The Great American",
"Knight", "American", "Outback", "Celebrity", "Air", "Restaurant",
"Bachelor", "Family", "Royal", "Surf", "Ulitmate", "Date", "Operation"
), size = c(4633, 7841, 980, 558, 15671, 15341, 552, 1793, 4109,
2928, 544, 24650, 55949, 795, 2463, 203, 883, 495, 1784, 438,
564, 11, 3699, 768, 1268, 84, 40, 1047, 39, 3388, 1744, 822,
367, 450, 143, 272, 251, 151, 692, 249, 205, 130, 160, 4, 14,
11, 10, 7), group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), .Names = c("name",
"size", "group"), row.names = c(NA, -48L), class = "data.frame")
法典
library(networkD3)
forceNetwork(Links = catlinks, Nodes = catnode,
Source = "source", Target = "target",
NodeID = "name",
Group = "group",
Value = "value",
Nodesize = "size",
charge = -600, # this helps
zoom = T)
计算出来:
值列的平方根,因为值相距太远而无法正确查看
forceNetwork(Links = catlinks, Nodes = catnode,
Source = "source", Target = "target",
NodeID = "name",
Group = "group", # color nodes by betweeness calculated earlier
Value = "value",
Nodesize = "size",
charge = -1000,
linkDistance = networkD3::JS("function(d) { return 5*d.value; }"), # mess with the 5 to mess with the distances
zoom = T)