使用lapply或loop重复基于多个数据帧创建.png文件的过程 [英] Repeat the process of creating a .png file based on multiple dataframes using lapply or loop
问题描述
我在下面有 nodes
和 edges
个数据框,然后创建一个图形对象 gph
.之后,我创建一个名为 asp
的 igraph.vs
对象的列表,并带有所有简单路径.然后,我希望能够使用 for
循环或 lappy()
来创建与列表长度一样多的数据帧,并使用每个 igraph.vs
对象,作为 nodes
数据集,并基于这些 nodes
数据集创建尽可能多的相对 edges
数据集.然后,我使用 nodes
和 edges
创建所有网络,然后创建尽可能多的.png网络.因此,如果 asp
的列表包含7个 igraph.vs
对象,则应创建7个. png
文件.以下是处理1个文件的过程.
I have the nodes
and edges
dataframes below and then I create a graph object gph
. After that I create an list of igraph.vs
objects named asp
with all simple paths.
Then I want to be able to use a for
loop, or a lappy()
in order to create as many dataframes as the length of the list with the names of each igraph.vs
object, as nodes
datasets, and based on those nodes
datasets to create as many relative edges
datasets.
Then I use nodes
and edges
to create all networks and then as many .png as networks. So if the list of asp
contains 7 igraph.vs
objects I should create 7 .png
files.
Below is the process for 1 file.
name<-c("Robert Zero", "Amy Adams", "Barry Beane", "Henry Handler", "Julie Jones",
"Charlie Cheese", "Karen Klein", "Lolly Landswright", "Imogene Isler", "David Dufresne",
"Frank Flaherty", "Egbert English", "George Graham", "Molly McKee", "Norman Needle",
"Oscar Oliver", "Peter Platteville", "Quincy Quebec", "Roger Rabbit", "Steve Sweet",
"Tom Thompson", "Victor Valentino", "Ulrich Uxbridge" )
label<-c("Robert Zero", "Amy Adams", "Barry Beane", "Henry Handler", "Julie Jones",
"Charlie Cheese", "Karen Klein", "Lolly Landswright", "Imogene Isler", "David Dufresne",
"Frank Flaherty", "Egbert English", "George Graham", "Molly McKee", "Norman Needle",
"Oscar Oliver", "Peter Platteville", "Quincy Quebec", "Roger Rabbit", "Steve Sweet",
"Tom Thompson", "Victor Valentino", "Ulrich Uxbridge" )
nodes<-data.frame(name,label)
from<-c("Robert Zero", "Amy Adams", "Barry Beane", "Barry Beane", "Barry Beane",
"Julie Jones", "Karen Klein", "Imogene Isler", "David Dufresne", "Frank Flaherty",
"Egbert English", "George Graham", "Molly McKee", "Molly McKee", "Amy Adams",
"Peter Platteville", "Amy Adams", "Roger Rabbit", "Julie Jones", "Robert Zero",
"Steve Sweet", "Tom Thompson", "Steve Sweet", "Ulrich Uxbridge", "Henry Handler",
"Barry Beane", "Barry Beane", "Charlie Cheese", "Barry Beane", "Victor Valentino",
"Tom Thompson" )
to<-c( "Amy Adams", "Barry Beane", "Henry Handler", "Julie Jones", "Charlie Cheese",
"Karen Klein", "Lolly Landswright", "Robert Zero", "Frank Flaherty", "Robert Zero",
"George Graham", "Robert Zero", "Norman Needle", "Julie Jones", "Oscar Oliver",
"Robert Zero", "Quincy Quebec", "Robert Zero", "Molly McKee", "Steve Sweet",
"Tom Thompson", "Victor Valentino", "Ulrich Uxbridge", "Barry Beane", "Robert Zero",
"Imogene Isler", "Peter Platteville", "Robert Zero", "Roger Rabbit", "Robert Zero",
"Roger Rabbit" )
edges<-data.frame(from,to)
#Combine them in order to create the graph object
library(igraph)
library(webshot); #webshot::install_phantomjs() #in case phantomjs was not installed
library(visNetwork)
library(dplyr)
gph <- graph_from_data_frame(edges, directed=TRUE, vertices=nodes)
#Create the simple paths asp
asp <- all_simple_paths(gph, "Steve Sweet", "Robert Zero")
anodes<-data.frame()
aedges<-data.frame()
for(i in 1 :length(asp)){
#new nodes
anodes[i]<-data.frame(names(asp[[i]]))
#new edges
aedges<-data.frame(anodes %>% mutate(to = lead(id, default = first(id))))
colnames(aedges)[1]<-"from"
aedges$label<-""
aedges<-merge(edges, aedges, by = c("from", "to"))
#create network
vis<-visNetwork(anodes, aedges, width = "100%")
#save as png in directory
html_name <- tempfile(fileext = ".html")
visSave(vis, html_name)
webshot(html_name, zoom = 2, file = paste("Path", i))
}
推荐答案
我想您要画的是子图,您可以使用 ductive_subgraph
I guess what you are after is to plot sub-graphs, and you may try the code below using induced_subgraph
for (k in seq_along(asp)) {
png(paste0("sub_gph_",k,".png"))
plot(induced_subgraph(gph, asp[[k]]))
dev.off()
}
这篇关于使用lapply或loop重复基于多个数据帧创建.png文件的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!