2016-12-25 3 views
0

enter image description here 私はこの三者グラフのコードを試しました。しかし、私は三者グラフを作りたいとR.データフレームに.csvファイルを使用して三者ネットワークを構築する方法は?

を使用して二部ネットワークグラフを描画したい私は、関連symptoms.andとその病気の人の.CSVファイルを使用している

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ファイルを使用する必要があります

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 

私がこのコードを実行すると、病気や症状の2部グラフだけが表示されます。私が間違っているところで親切に助けてください。

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の三者でないグラフを用いて疾患データセットの上に使用して三者間のネットワークのように、この画像を描画する方法について説明します。

+0

を試みることができる三者グラフになるようです。したがって、あなたが何を求めているのかは明らかではありません。詳しく教えてください。 – lukeA

+0

上記のコードは正しく動作しています。しかし、私は私の.csvファイルから三部グラフを描こうとしているとき(私は上記のデータセットを述べた)。それは病気と症状のグラフを描くだけです。それは私が私の必要な結果を得ていない理由を尋ねている人を含んでいません – student123

答えて

1

あなたはコード

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

enter image description here

+0

ようこそ。レイアウトアルゴリズムはレイヤーを自動的に計算しようとします。これは簡単です: 'l < - layout_with_sugiyama(g)'。 – lukeA

+0

注目...それは動作します – student123

関連する問題