R中2列图上的合作网络

piwo6bdm  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(96)

我想在R中的igraph中开始创建网络时获得一些帮助。
我的数据如下所示:

data <- data.frame(Country = c("Argentina", "Bolivia", "Chile", "France", "Guam", "US"), Counts = c(2,8,1,5,3,35))

其中计数列是这些国家/地区在特定搜索中出现的次数。
我在igraph上阅读的每一个教程都有一种绘制节点和边的方法。边通常包含一个from和to列。但是在这个数据集中,国家没有链接。我只需要帮助开始这段代码。我已经包含了一张最终结果的图片-线条的粗细与计数列成比例。这可能吗?x1c 0d1x

nbnkbykc

nbnkbykc1#

正如@维达所指出的,我们需要构建一个中心。

library(igraph)
nations   <- data.frame(Country = c("Argentina", "Bolivia", "Chile", "France", "Guam", "US"),
                         Counts = c(2,8,1,5,3,35),
                     Population = c(45.195, 11.673, 19.116, 65.625, 0.172, 332.403)
             )
center <- setNames(data.frame("c",2,0), names(nations))
vtx    <- rbind(center, nations)
rel    <- data.frame( from = head(vtx$Country, 1),
                        to = tail(vtx$Country),
                     width = tail(vtx$Counts)   
             )                    

## >?graph_from_data_frame, read the docs.
g <- graph_from_data_frame(rel,  directed = FALSE, vtx )
vertex_attr_names(g)
g

绘制一个漂亮的图表本身就是一门艺术。例如,最近的查询:How can I make edge betweenness with igraph in R?

## >?plot.igraph, read the docs.
cols             <- c("white", "green", "black", "brown", "blue", "yellow", "purple")
V(g)$color       <- "white"
V(g)$size        <- c(2, rep(40, nrow(vtx)-1))
V(g)$frame.color <- cols
V(g)$frame.width <- 4
E(g)$color       <- tail(cols)
plot(g, layout=layout_as_star)

## An alternative is to change the vertex size.
size <- (rank(V(g)$Population) - 1) * 5 
dev.new(); plot(g, layout=layout_as_star, vertex.size = size , edge.width=4)

相关问题