R网络用于距离矩阵

时间:2013-04-30 12:42:33

标签: r

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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

我发现的一种解决方案是使用距离矩阵制作两列数据框,并将其添加到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
)

希望它可以提供帮助。