Loop or vector for creating multiple graphs for network analysis in R

六月ゝ 毕业季﹏ 提交于 2020-01-06 07:00:59

问题


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.

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))

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

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.


回答1:


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)

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()

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.



来源:https://stackoverflow.com/questions/51551082/loop-or-vector-for-creating-multiple-graphs-for-network-analysis-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!