循环以在Ego网络中创建每个节点的PDF,其中节点ID作为R中的文件名 [英] Loop to create PDFs of each node in ego network with node ID as file names in R

查看:128
本文介绍了循环以在Ego网络中创建每个节点的PDF,其中节点ID作为R中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据正在为问题提供的答案,我正在使用R的iGraph在iGraph中创建一个自我网络.一个>.然后,我想将每个自我网络导出为PDF或图像文件(最好是PDF)中的图.

I'm creating ego networks from a graph in iGraph with R, as per the answer that was kindly provided to this question. I then want to export each ego network as a plot in a PDF or image file (preferably PDF).

由于整个数据框中有788个自我网络",我想使PDF的文件名与网络的相关节点ID相匹配,即数据帧的发件人"列.否则,很难找到我想要的特定网络.该怎么办?

As there are 788 'ego networks' in the full dataframe I'd like to make the file name of the PDF match the relevant node ID for the network i.e. the 'From' column of the dataframe. Otherwise it's too hard to find the particular network that I want. How can this be done?

这里是网络示例

library(igraph)


 > 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 make it a graph, then an ego graph

g <- graph_from_data_frame(df, directed = TRUE, vertices = NULL)
z <- make_ego_graph(g)

并使用此循环导出每个网络

And use this loop to export each network

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

推荐答案

好吧,有些自我网络将是发自",有些则是至".生成网络. 自我网络与顶点的顺序相同,因此您可以仅使用顶点名称作为文件名的基础.对于您的示例,此方法有效.

Well, some of the ego networks will be "from" and some will be 'to". I think that you want the name to be whichever node was used to generate the network. The ego networks are in the same order as the vertices, so you can just use the vertex names as the basis of the file names. For your example, this worked.

setwd("Ego_pdf")       # set this to someplace reasonable
for(i in 1:length(z)) {
    pdf(paste0(V(g)[i]$name, ".pdf"))
    plot(z[[i]])
    dev.off()
}

这只是循环遍历顶点索引,将输出名称设置为顶点名称,绘制自我网络并关闭pdf,以便为下一个pdf做准备.

This just loops through the vertex indices, sets the name of the output to the vertex name, plots the ego network and closes the pdf so that you are ready for the next one.

这篇关于循环以在Ego网络中创建每个节点的PDF,其中节点ID作为R中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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