用于在R中创建多个图进行网络分析的循环或向量 [英] Loop or vector for creating multiple graphs for network analysis in R

查看:217
本文介绍了用于在R中创建多个图进行网络分析的循环或向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用igraph从数据框中创建了一个图表,该图表显示了学生如何在职业课程之间移动.前两列是发件人"和发件人",其中包含资格名称(顶点),每当学生首次注册课程(发件人)然后再注册另一名学生(发件人)时,该名称就会出现.其余列包含与该特定学生的人口统计相关的值.

I've created a graph from a dataframe with igraph that shows how students move between vocational courses. The first two columns are 'From' and 'To' which contain the qualification names (vertices) with an occurrence for each time a student enrols in a course for the first time (From) and then enrols in another (To). The remaining columns contain values relating to that particular student's demographics.

我想为每个单独的课程(788个独特的课程)创建和绘制运动到"或从"的单独网络.我可以通过子图或过滤原始数据帧来实现此目的,但是我认为需要使用循环或lapply函数,这样就无需重复键入公式.我还想为每个网络创建一个图和PDF.不幸的是,我的R技能还不足以让我找出循环/不适的部分.

I'd like to create and plot individual networks for movements 'To' OR 'From' each individual course (788 unique courses). I can achieve this through a subgraph or filtering the original dataframe, but I think a loop or lapply function would be required so that I don't need to type the formula repeatedly. I'd also like to create a plot and PDF of each network. Unfortunately my R skills aren't good enough for me to figure out the loop/lapply part.

我的数据框的前两列概述如下(出于隐私原因,我已删除了学生信息):

The first two columns of my dataframe are outlined below (I've removed student info for privacy reasons):

    > dput(droplevels(head(df)))
structure(list(From = structure(c(5L, 1L, 6L, 4L, 2L, 3L), .Label = c("ADVANCED DIPLOMA OF ACCOUNTING", 
"ADVANCED DIPLOMA OF CONVEYANCING", "ADVANCED DIPLOMA OF LEADERSHIP AND MANAGEMENT", 
"ADVANCED DIPLOMA OF NETWORK SECURITY", "ADVANCED DIPLOMA OF POLICING", 
"ADVANCED DIPLOMA OF VISUAL ARTS"), class = "factor"), To = structure(c(5L, 
1L, 6L, 3L, 2L, 4L), .Label = c("DIPLOMA OF ACCOUNTING", "DIPLOMA OF EVENT MANAGEMENT", 
"DIPLOMA OF INFORMATION TECHNOLOGY NETWORKING", "DIPLOMA OF LEADERSHIP AND MANAGEMENT", 
"DIPLOMA OF POLICING", "DIPLOMA OF VISUAL ARTS"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

然后我按如下方式创建图形

I then create the graph as follows

g <- graph_from_data_frame(df, directed = TRUE, vertices = NULL)
E(g)$weight <- 1
    g <- delete.edges(g, which(E(g)$weight <= 10)) 
g <- delete.vertices(g,which(degree(g)==0))

然后我通过一些颜色和标签更改使图变得漂亮,并制作一个pdf文件,如下所示.

I then make the plot pretty with some colour and label changes and make a pdf as follows.

pdf("Qual_Pathways.pdf", width = 11.7, height = 16.5)
plot(g)
dev.off

我如何做到这一点,这样就可以遍历From或To中的每个唯一值,以构成一系列788个网络,这些网络显示了与该特定资格的联系和来自该特定资格的联系?我真的很感谢任何帮助,我似乎什么也找不到.

How can I make it so each unique value in From or To is looped through to make a series of 788 networks that show the connections to and from that particular qualification? I'd really appreciate any help with this, I can't seem to find anything.

推荐答案

我认为这可能是您正在尝试做的事情.首先,请确保setwd到所有绘图都将转储到的目录.接下来,创建一个对象以包含所有自我图,即仅包含连接到特定节点的节点:

I think maybe this is what you are trying to do. First, be sure to setwd to a directory where all the plots will be dumped into. Next, create an object to contain all of the ego graphs, that is, only the nodes that connect to a particular node:

library(igraph)  # you must have loaded it earlier, but just in case
z <- make_ego_graph(g)

这会生成igraph个对象的列表:

This generates a list of igraph objects:

> z
[[1]]
IGRAPH 3e60c3b DNW- 4 4 -- 
+ attr: name (v/c), salary_income (e/n), other_income (e/n), survey_weights (e/n), weight
| (e/n)
+ edges from 3e60c3b (vertex names):
[1] 1->1 2->1 4->1 6->1

[[2]]
IGRAPH 3e60c3b DNW- 2 2 -- 
+ attr: name (v/c), salary_income (e/n), other_income (e/n), survey_weights (e/n), weight
| (e/n)
+ edges from 3e60c3b (vertex names):
[1] 1->1 2->1

...

[[7]]
IGRAPH 3e60c3c DNW- 3 2 -- 
+ attr: name (v/c), salary_income (e/n), other_income (e/n), survey_weights (e/n), weight 
| (e/n)
+ edges from 3e60c3c (vertex names):
[1] 3->0 5->0

您可以将所有七个自我网络转储到以下文件中:

You can dump all 7 ego networks to files like this:

jpeg(filename="EgoGraph%03d.jpeg")
for (i in 1:length(z)) { plot(z[[i]]) }
dev.off()

现在转到查找setwd所在的目录,然后会有EgoGraph001.jpegEgoGraph007.jpeg.如果对整个数据集执行此操作,并且该数据集具有788个节点,则将在该目录中提供788个图.

Now go look in the directory you setwd to and there will be EgoGraph001.jpeg through EgoGraph007.jpeg. If you do this with the whole dataset and it has 788 nodes it will give 788 plots in that directory.

这篇关于用于在R中创建多个图进行网络分析的循环或向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆