简体   繁体   English

在具有复杂数据的 R 中使用 igraph 创建图形

[英]Create graph using igraph in R with complicated data

I want to create a graph from a data frame with multiple data columns, where all of the columns contain vertices, like this: example data我想从具有多个数据列的数据框中创建一个图形,其中所有列都包含顶点,如下所示:示例数据

If two vertices are found in a row together, then they should be connected in the graph.如果在一行中找到两个顶点,则它们应该在图中连接。 In my example, vertex "Case no. 3" should be connected to the following vertices: "case no. 1", "Jon", "case no. 5", "Bill" (NA should be ignored).在我的示例中,顶点“Case no. 3”应该连接到以下顶点:“case no. 1”、“Jon”、“case no. 5”、“Bill”(NA 应该被忽略)。 Thanks in advance!提前致谢!

Your question is about manipulate the raw data, 'cause you need to construct your edgelist correctly.您的问题是关于操作原始数据,因为您需要正确构建边缘列表。 The only way to do this is to indicate two columns, with the sender of the link (col. 1), and the receiver of the link (col. 2).这样做的唯一方法是指明两列,包括链接的发送者(第 1 栏)和链接的接收者(第 2 栏)。 Self-directed links are allowed (eg, from 'a' to 'a').允许自我导向的链接(例如,从“a”到“a”)。 The others columns are caracteristics of the links, ever.其他列是链接的特征,永远。

Your example edgelist show 3 columns of vertices: this is not a valid edgeslist, one of the columns is useless.您的示例边列表显示 3 列顶点:这不是有效的边列表,其中一列无用。 So,所以,

  1. you'll have to construct a valid edgelist, by manipulate the data (see below).您必须通过操作数据来构建一个有效的边列表(见下文)。

  2. Then, you should tell igraph what is your edgelist and construct a graph, like in this answer and/or this one (sorry for the shameless auto-promote).然后,你应该告诉 igraph 你的边列表是什么并构建一个图,就像在这个答案和/或这个答案中一样(对不起无耻的自动推广)。

In order to construct a valid edgelist from the example you provide, with tidyverse tools and the %>% operator:为了从您提供的示例中构建有效的tidyverse ,使用tidyverse工具和%>%运算符:

    # ↓ SAMPLE DATA (colnames are different from the ones you provided) ↓
    
  raw_data <- data.frame(case_no=c(1, 2,3, 4), 
                        related_case =c(3,5,5, NA) ,
                        received_by = c("Jon", "Wendy","Jon", NA) , 
                        packed_by = c(NA, "Wendy", "Bill", NA) ) 

# ↓ First series of links ↓
edges_list <- raw_data %>% 
select(FROM = case_no, related_case, TO = received_by) %>% 
mutate(TYPE = 'Received') # ↑ THIS IS ONLY THE FIRST COLUMNS OF RECEIVERS
  
 # ↓ APPEND THE SECOND LIST OF RECEIVER TO THE FIRST VERSION OF THE EDGESLIST↓ 

 edges_list <- select(raw_data, FROM = case_no, related_case, TO = packed_by) %>% 
 mutate(TYPE = 'Packed') %>% #↑ HERE THE SECOND COLUMN OF RECEIVERS↑
   rbind(edges_list)   

edges_list <- na.omit(edges_list) # ← REMOVE NA FILLED ROWS

edges_list %>% igraph::graph_from_data_frame(directed = T) %>% 
  
  igraph::plot.igraph() # CREATE YOUR GRAPH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM