如何按正确顺序放置 igraph 边
how to put igraph edges in correct order
我有动物名称的数据框:
df <- data.frame(
col1 = c("dog", "cat", "bird", "mammal", "avis", "canis", "feline"),
col2 = c("canis", "feline", "avis", "animal", "animal", "mammal", "mammal"))
library(igraph)
species <- union(df$col2, df$col1)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
species <- species[order(species)]
species <- sort(union(df$col2, df$col1))
g <- graph.data.frame(df, directed = TRUE, vertices = species)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
在我 运行 代码之后,我得到了这个图表(下图)。在数据框中,前 3 个元素是 "dog", "cat", "bird"
,而在图形中它们是 "bird", "dog", "cat"
。简而言之,单词的顺序被颠倒了,我希望它们按照特定的顺序排列,而不改变数据框。
为了告诉 igraph 我希望我的数据以什么顺序表示,我使用 species <- species[order(species)]
此代码行,虽然它适用于具有单个字母的数据框但不适用于此或任何其他使用完整单词的数据框。
你的意思是这样?
我只是重新排列了数据的顺序,例如:
df <- data.frame(from = c('animal', 'animal', 'mammal', 'mammal', 'avis', 'canis', 'feline'),
to = c('mammal', 'avis', 'canis', 'feline', 'bird', 'dog', 'cat'))
igraph 按 edgelist
的顺序绘制边,这与数据帧不同:
get.edgelist(g)
这是需要更改的信息。我只创建了小图表并且总是按照我想要的顺序输入我的数据,但我认为这是要调查的内容 set.edge.attribute()
。
目前,您的顶点是根据向量 species
中的顺序排序的。没有 vertices
参数,顺序是根据两列数据帧 df
中的联合元素。
> g <- graph.data.frame(df, directed = TRUE)
> V(g)$name
[1] "canis" "feline" "avis" "animal" "mammal" "dog" "cat" "bird"
> g <- graph.data.frame(df, directed = TRUE, vertices = species)
> V(g)$name
[1] "animal" "avis" "bird" "canis" "cat" "dog" "feline" "mammal"
为了解决你的问题,根据df
、两列的并集对species
向量进行排序,但先取第二列。这样,"dog", "cat", "bird"
序列会引出 species
向量。
df <- data.frame(
col1 = c("dog", "cat", "bird", "mammal", "avis", "canis", "feline"),
col2 = c("canis", "feline", "avis", "animal", "animal", "mammal", "mammal"))
library(igraph)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
species <- union(df$to, df$from) #NOTE do not sort the vector!
g <- graph.data.frame(df, directed = TRUE, vertices = species)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
现在顶点按照物种的顺序排列,把"dog", "cat", "bird"
放在第一位。
> species
[1] "dog" "cat" "bird" "mammal" "avis" "canis" "feline" "animal"
> V(g)$name
[1] "dog" "cat" "bird" "mammal" "avis" "canis" "feline" "animal"
我有动物名称的数据框:
df <- data.frame(
col1 = c("dog", "cat", "bird", "mammal", "avis", "canis", "feline"),
col2 = c("canis", "feline", "avis", "animal", "animal", "mammal", "mammal"))
library(igraph)
species <- union(df$col2, df$col1)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
species <- species[order(species)]
species <- sort(union(df$col2, df$col1))
g <- graph.data.frame(df, directed = TRUE, vertices = species)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
在我 运行 代码之后,我得到了这个图表(下图)。在数据框中,前 3 个元素是 "dog", "cat", "bird"
,而在图形中它们是 "bird", "dog", "cat"
。简而言之,单词的顺序被颠倒了,我希望它们按照特定的顺序排列,而不改变数据框。
为了告诉 igraph 我希望我的数据以什么顺序表示,我使用 species <- species[order(species)]
此代码行,虽然它适用于具有单个字母的数据框但不适用于此或任何其他使用完整单词的数据框。
你的意思是这样?
我只是重新排列了数据的顺序,例如:
df <- data.frame(from = c('animal', 'animal', 'mammal', 'mammal', 'avis', 'canis', 'feline'),
to = c('mammal', 'avis', 'canis', 'feline', 'bird', 'dog', 'cat'))
igraph 按 edgelist
的顺序绘制边,这与数据帧不同:
get.edgelist(g)
这是需要更改的信息。我只创建了小图表并且总是按照我想要的顺序输入我的数据,但我认为这是要调查的内容 set.edge.attribute()
。
目前,您的顶点是根据向量 species
中的顺序排序的。没有 vertices
参数,顺序是根据两列数据帧 df
中的联合元素。
> g <- graph.data.frame(df, directed = TRUE)
> V(g)$name
[1] "canis" "feline" "avis" "animal" "mammal" "dog" "cat" "bird"
> g <- graph.data.frame(df, directed = TRUE, vertices = species)
> V(g)$name
[1] "animal" "avis" "bird" "canis" "cat" "dog" "feline" "mammal"
为了解决你的问题,根据df
、两列的并集对species
向量进行排序,但先取第二列。这样,"dog", "cat", "bird"
序列会引出 species
向量。
df <- data.frame(
col1 = c("dog", "cat", "bird", "mammal", "avis", "canis", "feline"),
col2 = c("canis", "feline", "avis", "animal", "animal", "mammal", "mammal"))
library(igraph)
df <- df[c('col2', 'col1')]
names(df) <- c('from', 'to')
species <- union(df$to, df$from) #NOTE do not sort the vector!
g <- graph.data.frame(df, directed = TRUE, vertices = species)
plot(g,vertex.size=2, vertex.label.dist=0.5, vertex.color="cyan",
edge.arrow.size=0.5, layout=layout.reingold.tilford(g))
现在顶点按照物种的顺序排列,把"dog", "cat", "bird"
放在第一位。
> species
[1] "dog" "cat" "bird" "mammal" "avis" "canis" "feline" "animal"
> V(g)$name
[1] "dog" "cat" "bird" "mammal" "avis" "canis" "feline" "animal"