我一直在玩R中的igraph,当我想象一个网络时我很难使用权重。我已经读过这可能不适用于每种布局类型,但应该与Fruchterman-Reingold一起使用。
我的代码和输出在下面(我尝试了两个不同版本的布局功能,我认为他们正在做同样的事情,但是为了以防万一都试过)
我希望塞西尔和鲍勃在第一节中非常接近,因为他们的关系权重很高,但这似乎不会发生。只有当我为Bob和Cecil创建额外的行(vers2)时,这似乎才会发生,但这对于我真正想要用更大的数据集做的事情来说会很痛苦。
我会发布我正在获得的图像,但我是新的堆叠溢出并没有足够的声誉点。
有什么想法吗?提前致谢。
代码:
#vers1
library(igraph)
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob",
"Alice"),
weight=c(1,100,1,1,1,1))
graph<-graph_from_data_frame(relations, directed=F)
coords1<-layout_with_fr(graph, weights=E(graph)$weight)
coords2 <- layout.fruchterman.reingold(graph, weights=E(graph)$weight);
plot(graph,layout=coords1)
plot(graph,layout=coords2)
#vers2
library(igraph)
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda",
"Cecil",
"Cecil",
"Cecil",
"Cecil",
"Cecil"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob",
"Alice",
"Bob",
"Bob",
"Bob",
"Bob",
"Bob"),
weight=c(1,1,1,1,1,1,1,1,1,1,1))
graph<-graph_from_data_frame(relations, directed=F)
coords1<-layout_with_fr(graph, weights=E(graph)$weight)
coords2 <- layout.fruchterman.reingold(graph, weights=E(graph)$weight);
plot(graph,layout=coords1)
plot(graph,layout=coords2)
答案 0 :(得分:1)
以下是使用sna
和network
库的版本。我记录了你的100到3的值,使它成为一种物理上不可能的东西。但您可以使用日志值或其他内容。注意还需要将值从相似度转换为距离。
library(sna)
library(network)
# recode the desired distances to something more reasonable
# (can't phiscally have one distance 100X the others)
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob",
"Alice"),
weight=c(1,3,1,1,1,1))
# convert to network object including edge weights
relNet<-network(relations,ignore.eval = FALSE,names.eval='weight',matrix.type='edgelist',directed=FALSE)
# valued construct adjacency matrix
adjMat<-as.matrix(relNet,attrname='weight')
# convert from distances to similarities
adjMat[adjMat!=0]<-4-adjMat[adjMat!=0]
# construct an appropriate geodesic distance matrix from the similarities
distMat<-geodist(adjMat,ignore.eval=FALSE,inf.replace = sqrt(network.size(relNet)))$gdist
# compute coords using distance matrix and kk algorithm
coords<-network.layout.kamadakawai(relNet,layout.par=list(elen=distMat))
# plot using precomputed coords
plot(relNet,displaylabels=TRUE,coord=coords,edge.label='weight',edge.lwd='weight')