2016-12-25 15 views
0

enter image description here 我試過這個三方圖的代碼。但我必須使用.csv文件如何在數據框中使用.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文件,他們的疾病與相關symptoms.and我要讓三方圖形,想畫使用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 

但是,當我執行下面的代碼只有陰謀疾病和症狀的雙方圖..善意幫助我犯錯誤的地方。

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

相關問題