我想根据连接的边数看到节点/顶点的大小。例如,如果节点1与其他节点具有更多数量的连接边,则节点1的大小应更大。我已经采用了一个假设的简单数据和尝试网络图,它运作得相当好。基本上,网络图是关于共同作者网络。但是,我想根据连接边的数量调整节点的大小。另外,我想知道如何自定义边缘颜色?例如,如果节点1具有多于4个连接,则所有4个边缘的颜色应为红色。
以下代码表现良好:
library(igraph)
# original data as a list
input_data = list(c(1,2,3),c(1,4,5),c(1,6),c(3),c(4,6))
## function that makes all pairs
pairing <- function(x) {
n = length(x)
if (n<2) {
res <- NULL
} else {
q <- combn(n,2)
x2 <- x[q]
#dim(x2) <- dim(q)
res <- x2
}
return(res)
}
## for each paper create all author pairs
pairing_bypaper = lapply(input_data,pairing)
## remove papers that contribute no edges
pair_noedge = sapply(pairing_bypaper,is.null)
pair2_bypaper <- pairing_bypaper[!pair_noedge]
## combine all 'subgraphs'
pair_all <- do.call('c',pair2_bypaper)
## how many authors are there?
n.authors <- length(unique(pair_all))
## make a graph
my_graph = graph(pair_all, directed = FALSE)
## plot
plot(my_graph)
plot(my_graph, vertex.label.cex = 0.8, edge.width = E(my_graph)$weight)
非常感谢先进。
答案 0 :(得分:9)
首先得到图形的度数 - 衡量每个顶点连接的点数:
degree(my_graph)
[1] 5 2 2 3 2 2
然后我们可以使用它作为顶点大小:
## size
V(my_graph)$vertex_degree <- degree(my_graph)
然后在你的情节电话中:
plot(my_graph,
vertex.label.cex = 0.8,
edge.width = E(my_graph)$weight,
vertex.size = V(my_graph)$vertex_degree #add this
)
如果要向上或向下缩放,可以用以下内容替换上面的行:
scale_factor <- 4
plot(my_graph,
vertex.label.cex = 0.8,
edge.width = E(my_graph)$weight,
vertex.size = V(my_graph)$vertex_degree * scale_factor
)