如何在R中的igraph中以正确的顺序获取数据
how to get data in correct order in igraph in R
我有图表需要按照数据集中的正确顺序表示:
df <- data.frame(
col1 = c("A1", "B1", "C1", "D1", "E1", "A", "B", "C", "D", "E"),
col2 = c("ABC", "ABC", "B1", "B1", "B1", "A1", "B1", "C1", "D1", "E1"))
在数据集中,col1
和 col2
的前 5 个元素都用于表示层次结构。
library(igraph)
abc <- union(df$col2, df$col1)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
g <- graph.data.frame(df, directed = TRUE, vertices = abc)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
用这段代码我得到了这张图
A 和 B 应该在图的前面并且应该读作 A, B, C, D, E
我相信 igraph
绘制边和顶点 "in order"。因此,我认为你可以重新排序你的顶点:
abc <- abc[order(abc)]
...然后您现有的代码应该可以工作。
我有图表需要按照数据集中的正确顺序表示:
df <- data.frame(
col1 = c("A1", "B1", "C1", "D1", "E1", "A", "B", "C", "D", "E"),
col2 = c("ABC", "ABC", "B1", "B1", "B1", "A1", "B1", "C1", "D1", "E1"))
在数据集中,col1
和 col2
的前 5 个元素都用于表示层次结构。
library(igraph)
abc <- union(df$col2, df$col1)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
g <- graph.data.frame(df, directed = TRUE, vertices = abc)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
用这段代码我得到了这张图
A 和 B 应该在图的前面并且应该读作 A, B, C, D, E
我相信 igraph
绘制边和顶点 "in order"。因此,我认为你可以重新排序你的顶点:
abc <- abc[order(abc)]
...然后您现有的代码应该可以工作。