how to built tripartite network using .csv file in dataframe?

天涯浪子 提交于 2019-12-12 04:23:23

问题


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)

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)

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.


回答1:


You could try

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



来源:https://stackoverflow.com/questions/41319751/how-to-built-tripartite-network-using-csv-file-in-dataframe

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