[英]Network graph using R: Node size based on number of connected edges
I would like to see a size of the node/vertex based on number of connected edges. 我想根据连接边的数量查看节点/顶点的大小。 For example, if Node 1 has more number of connected edges with other nodes then size of Node 1 shall be larger. 例如,如果节点1与其他节点的连接边数更多,则节点1的大小将更大。 I have taken a hypothetical simple data and attempted network graph which is working fairly well. 我假设了一个简单的数据,并尝试了运行良好的网络图。 Basically, the network graph is about co-authorship network. 基本上,网络图是关于共同创作网络。 However, I would like to adjust size of node based on number of connected edges. 但是,我想根据连接边的数量来调整节点的大小。 Also, I would like to know how can I customize color of edges?. 另外,我想知道如何自定义边缘的颜色? For example, if Node 1 has more than 4 connections then color of all that 4 edges shall be red. 例如,如果节点1具有多于4个连接,则所有4条边的颜色应为红色。
Following is the code which is doing well: 以下是运行良好的代码:
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)
Thanks a lot in advanced. 非常感谢您。
First get the degree of your graph - a measure of how many points are connected to each vertex: 首先获取图形的程度-度量每个顶点连接多少个点:
degree(my_graph)
[1] 5 2 2 3 2 2
We can then use this as the vertex size: 然后,我们可以将其用作顶点大小:
## size
V(my_graph)$vertex_degree <- degree(my_graph)
And then in your plot call: 然后在您的绘图调用中:
plot(my_graph,
vertex.label.cex = 0.8,
edge.width = E(my_graph)$weight,
vertex.size = V(my_graph)$vertex_degree #add this
)
If you want to scale this up or down, you could replace the above line with something like: 如果要按比例放大或缩小,可以将上面的行替换为:
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.