如何安排R中的节点

时间:2017-06-28 12:39:24

标签: r igraph

在数据框中有动物,CatDog总是先行,其余动物像最终图片一样在狗下面。

`library(igraph)
 # I create my dataframe with animals
df <- data.frame(col0 = c("Dog Cat Wolf Wolf1", "Dog Cat Wolf Wolf1", "Dog 
Cat Wolf Wolf1", "Dog Cat Wolf Wolf1"),
col1 = c( "Dog", "Cat", "Wolf", "Wolf1"),
col2 = c( "Canis", "Feline", "Canis2", "Canis3"))
# Add extra lines for hierarchy 
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Feline", col2="Animal"))
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Canis", col2 = "Animal"))
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Canis2", col2 ="Canis"))
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Canis3", col2 ="Canis2"))

 df <-df[c('col2', 'col1')]
 names(df) <-c('from', 'to')
 abc <-union(df$to, df$from)

 g <-graph.data.frame(df, directed = TRUE, vertices = abc)
 plot(g, vertex.size = 20, vertex.label.dist = 0.5, vertex.color = c("blue", 
 "red", "green", "white", "orange", "pink", "cyan"  ),
 edge.arrow.size = 0.5, layout = layout.reingold.tilford(g))`

使用代码后,我得到了这个图表:

enter image description here

什么时候应该是这样的:

enter image description here

0 个答案:

没有答案