简体   繁体   English

使用R的网络图:基于连接边数的节点大小

[英]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.

相关问题 在 r 中使用网络 package 中的属性从节点列表创建没有边的图 - Create graph with no edges from node list with attributes in the network package in r 使用 R 在网络图中根据连接的节点大小缩放边的起点和终点 - Scale the start- and end-point of the edge against the connected node size in the network plot using R 在 R 中创建一个具有集合节点位置和集中边的网络图,其中包含圆头和箭头 - Creating a network graph with set node positions and concentrated edges with both circleheads and arrowheads in R 计算一条边连接的其他边总数R? - Counting total number of other edges an edge is connected to in R? 如何使用 igraph R 包比较两个图以识别两个图之间相同/不同边的数量 - How to compare two graphs to Identify number of same/different edges between two graph using igraph R package 具有不同大小节点的 R qgraph 网络图? - R qgraph network graph with varying size of nodes? R / Network Analysis-如何通过节点的属性创建边 - R/Network Analysis - How to create edges by node's attributes Quanteda:计算网络图中每个节点的边数 - quanteda: Count number of edges for each node in a network plot 在 r 中生成具有指定边数的随机网络模型 - Generate Random network models with specified number of edges in r 按边数R对igraph中的子图列表进行排序 - Sort sub graph list in igraph by number of edges R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM