1

我正在尝试在距离矩阵的 R 中绘制网络,其中节点之间的距离应与距离矩阵值成比例,节点大小应与 nodes.networkin 的值成比例

4

2 回答 2

2

正如 CoffeeRain 所说,下次请提供代码来展示您的工作,并让任何试图回答的人深入了解您的思维过程以及真正的问题可能出在哪里。你在寻找这样的东西吗?

library(maps)

data(us.cities)

#create distance matrix
d <- dist(us.cities[, c("lat", "long")])

#multidimensional scaling so we can plot and retain distance relationships
foo <- cmdscale(d, k = 2)

#everything is upside down and backwards
#plot(foo)

plot(-foo)
plot(-foo, cex = scale(us.cities$pop))
于 2013-04-30T13:35:30.730 回答
1

我找到的一个解决方案是使用距离矩阵制作一个两列数据框并添加到 networkD3 包中。检查此链接https://www.jessesadler.com/post/network-analysis-with-r/

> linksdf
    source target dist
1     6307  14749 1.334
2     6307  14778 1.334
3     6307   2089 1.329
4     6307   2690 1.341
> nodesdf
      nodeID group
6307    6307     n
6336    6336     l
6438    6438     h
6439    6439     o
7046    7046     u
forceNetwork(Links = linksdf, Nodes = nodesdf, Source = "source", 
             Target = "target", NodeID = "nodeID", Group = "group")

或者您可以直接使用距离矩阵(https://www.r-graph-gallery.com/254-use-igraph-input-format-networkd3/):

library(igraph)
library(networkD3)

# Create data
data=matrix(sample(0:1, 400, replace=TRUE, prob=c(0.95,0.05) ), nrow=20)

# Tell Igraph it is an adjency matrix... with default parameters
network=graph_from_adjacency_matrix(data)

# transform Igraph format in something readable by networkD3
network=igraph_to_networkD3(network)

# plot
simpleNetwork(network$links,
        height = 480,                     # height of frame area in pixels
        width = 480,
        linkDistance = 120,               # distance between node. Increase this value to have more space between nodes
        charge = -480,                    # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value)
        fontSize = 27,                    # size of the node names
        linkColour = rgb(0.1,0.9,0.1,0.3),# colour of edges, MUST be a common colour for the whole graph
        nodeColour = "forestgreen",       # colour of nodes, MUST be a common colour for the whole graph
        opacity = 0.9,                    # opacity of nodes. 0=transparent. 1=no transparency
)

希望它可以帮助。

于 2019-06-26T15:18:28.603 回答