如何在数据框中使用.csv文件建立三方网络? [英] how to built tripartite network using .csv file in dataframe?
问题描述
在此处输入图片描述 我已经尝试过此代码的三方图.但我必须使用.csv文件
enter image description here I have tried this code of tripartite graph. but i have to use .csv file
library(igraph)
data = "From, To
Recipe:Chicken Marsala,flour
Recipe:Chicken Marsala,sage
Recipe:Chicken Marsala,chicken
Recipe:Chicken Marsala,wine
Recipe:Chicken Marsala,butter
Recipe:Glazed Carrots,butter
Recipe:Glazed Carrots,vinegar
Recipe:Glazed Carrots,carrot
Recipe:Glazed Carrots,chive
flour,compound:X2
sage,compound:X3
chicken,compound:X6
chicken,compound:X7
wine,compound:X1
wine,compound:X4
wine,compound:X5
wine,compound:X8
wine,compound:X9
wine,compound:X10
wine,compound:X11
wine,compound:X12
butter,compound:X4
butter,compound:X5
butter,compound:X7
butter,compound:X8
butter,compound:X11
vinegar,compound:X8
vinegar,compound:X13
carrot,compound:X2
carrot,compound:X15
chive,compound:X6
chive,compound:X14
"
Read the data in from the text version above into a data frame:
data=read.csv(textConnection(data),head=TRUE)
Make a graph out of it:
g = graph_from_data_frame(data,directed=FALSE)
Assign numbers to layers by type. layer 2 is ingredients, layer 1 is recipes, layer 3 is compounds:
layer = rep(2, length(V(g)$name))
layer[grep("Recipe:",V(g)$name)]=1
layer[grep("compound:",V(g)$name)]=3
now get rid of the prefix
names = V(g)$name
names = sub("Recipe:","", names)
names = sub("compound:","", names)
V(g)$name = names
Now compute a layout
layout = layout_with_sugiyama(g, layers=layer)
Now plot using the coordinates from the layout. Default seems to be vertical, so use first column as Y coordinate and layer number as X coordinate. Set shape and size etc by layer.
plot(g,
layout=cbind(layer,layout$layout[,1]),
vertex.shape=c("square","circle","none")[layer],
vertex.size=c(50,20,0)[layer],
vertex.label.dist=c(0,0,.8)[layer],
vertex.label.degree=0)
我使用了具有相关症状的人的.csv文件.我想制作三方图,并希望使用R绘制二部网络图.
I have used .csv file of person their diseases with related symptoms.and i want to make tripartite graph and want to draw a bipartite network graph using R.
symptom disease Person
Abdominal pain Abdominal aortic aneurysm Person1
Abdominal pain Acute liver failure Person2
Abdominal pain Addison's disease Person2
Abdominal pain Alcoholic hepatitis Person1
Abdominal pain Anaphylaxis Person1
Abdominal pain Antibiotic-associated diarrhea Person3
Abdominal pain Aortic aneurysm Person4
Abdominal pain Appendicitis Person4
Abdominal pain Ascariasis Person4
Abdominal pain Barrett's esophagus Person4
但是当我执行下面的代码时,这只会绘制疾病和症状的二部图..请在出现错误的地方提供帮助.
but when i execute the code below this only plot bipartite graph of diseases and symptoms.. kindly help where i am making error.
datafile <- "c:\\dp.csv"
el <- read.csv(datafile)
g = graph_from_data_frame(el,directed=FALSE)
layer=rep(2,length(V(g)name))
layer[grep("Diseases",V(g)name)]=1
layer[grep("Symptoms",V(g)name)]=3
names=V(g)name)]=3
names=V(g)
name names = sub("Diseases","", names)
names = sub("Symptoms","", names) V(g)
V(g)$name = names
Now compute a layout
layout = layout_with_sugiyama(g, layers=layer)
plot(g,
layout=cbind(layer,layout$layout[,1]),
vertex.shape=c("square","circle","none")[layer],
vertex.size=c(50,20,0)[layer],
vertex.label.dist=c(0,0,.8)[layer],
vertex.label.degree=0)
我要问的是这样的网络,以及如何使用R而不是三方图使用上面的疾病数据集绘制像三方网络这样的图像.
and how to draw this image like tripartite network using above disease dataset using R not tripartite graph i am asking about network like this.
推荐答案
您可以尝试
df <- read.csv2(text="symptom;disease;Person
Abdominal pain;Abdominal aortic aneurysm;Person1
Abdominal pain;Acute liver failure;Person2
Abdominal pain;Addison's disease;Person2
Abdominal pain;Alcoholic hepatitis;Person1
Abdominal pain;Anaphylaxis;Person1
Abdominal pain;Antibiotic-associated diarrhea;Person3
Abdominal pain;Aortic aneurysm;Person4
Abdominal pain;Appendicitis;Person4
Abdominal pain;Ascariasis;Person4
Abdominal pain;Barrett's esophagus;Person4")
m <- as.matrix(df)
g <- graph_from_edgelist(rbind(m[,1:2], m[,2:3]), directed = F)
l <- layout_with_sugiyama(g, ceiling(match(V(g)$name, m)/nrow(m)))
plot(g, layout=-l$layout[,2:1])
这篇关于如何在数据框中使用.csv文件建立三方网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!