更改特定边缘igraph

时间:2017-11-24 09:31:04

标签: r for-loop graph igraph

我正在使用无向图。我希望绘制图形,并为那些满足某些标准的节点着色。

我的数据框包含不好的边缘。良好的边缘我设置了列color = darkgrey,边缘为color = red

我的边缘列表如下:

      from     to    color
1    54770  54771 darkgrey
2    54770  54775 darkgrey
3    54770  54776 darkgrey
4    54770  54774 darkgrey
5    54771  54775 darkgrey
6    54771  54776 darkgrey
7    54771  54774      red
8    54775  54776 darkgrey
9    54775  54774 darkgrey
10   54776  54774 darkgrey
11  110780 110781      red

(为此,我在列dob_diff上运行以下行):

df$color <- with(df, ifelse(dob_diff <= 168), "red", "darkgrey"))

我可以绘制图表:

g = graph.data.frame(df, directed=FALSE)
plot(graph)

enter image description here

我可以通过执行以下操作返回那些边缘的索引列表:

a <- which(df$colour == "red") 
> a
 [1]   7  11

如何获取这些边缘然后为连接到坏边缘的节点着色?

此问题here表示您可以按号码

访问节点颜色
V(g)$color[6] <- "red" 

如何告诉igraph获取边缘列表?

编辑:例如:

我可以返回那些不正确的节点列表:

> from <- cluster_data_updated_illegal_combined[c("from")][cluster_data_updated_illegal_combined$illegal_dob == 1,]
> to <- cluster_data_updated_illegal_combined[c("to")][cluster_data_updated_illegal_combined$illegal_dob == 1,]

> from
 [1] "54771"  "110780" "20285"  "83962"  "88825"  "19695"  "33343"  "89446"  "90175"  "98032"  "115768"
[12] "1982"   "71327"  "75017"  "75018"  "89315"  "89316"  "106277" "106956" "111140" "32841"  "72276" 
[23] "111062" "111074" "1904"   "12148"  "17325"  "17412"  "18238"  "18368"  "19681"  "28041"  "30755" 
[34] "31915"  "32825"  "43791"  "54806"  "54808"  "87075"  "94814"  "97877"  "101124" "102311" "107659"
[45] "108778" "108906" "112029" "120840" "127396" "33371"  "90150"  "111095" "122230"
> to
 [1] "54774"  "110781" "20693"  "83961"  "88565"  "20338"  "33340"  "91043"  "90171"  "98035"  "115771"
[12] "1976"   "71439"  "75019"  "75020"  "89929"  "89929"  "106279" "107489" "111139" "32993"  "72272" 
[23] "111060" "111071" "2697"   "12090"  "17323"  "17413"  "20300"  "18561"  "20025"  "28046"  "30744" 
[34] "31587"  "33113"  "44067"  "54799"  "54803"  "87073"  "94816"  "97872"  "101122" "103480" "107679"
[45] "109013" "109256" "112246" "120842" "127401" "32679"  "90259"  "111028" "122228"

然后我尝试为这些节点为红色着色,但是我收到错误:

> V(gg)[from]$color<-"red"
Error in unclass(x)[i] : invalid subscript type 'closure'
> V(gg)[to]$color<-"red"
Error in unclass(x)[i] : invalid subscript type 'closure'

1 个答案:

答案 0 :(得分:1)

你得到了什么:

您的边缘列表如下所示:

$sumval = 0;

$code = strtolower(trim($code));

$arr = str_split($code);
$arr_length = count($arr);

for($i = 0, $j = $arr_length-1; $i < $arr_length; $i++, $j--)
{
    $arr_value = array_search($arr[$i], $alphabets);
    $sumval = $sumval + ($arr_value * pow(26, $j));
}

return $sumval;

这是图表:

}

这些是数据框中红色(“坏”)边缘的索引:

df <- read.table(header=T, text=" from     to    color
1    54770  54771 darkgrey
2    54770  54775 darkgrey
3    54770  54776 darkgrey
4    54770  54774 darkgrey
5    54771  54775 darkgrey
6    54771  54776 darkgrey
7    54771  54774      red
8    54775  54776 darkgrey
9    54775  54774 darkgrey
10   54776  54774 darkgrey
11  110780 110781      red")

您的问题:

  

如何获取这些边缘然后为那些节点着色   连接到坏边缘?

这是为节点着色的一种方法,它连接到红色边缘:

library(igraph)
g = graph.data.frame(df, directed=FALSE)
plot(g)

enter image description here

参见例如which(df$color == "red") # [1] 7 11 。可能还有其他/更好的方法。

  

我是否可以使用其他名称创建另一个列   igraph可以用来自动为节点着色吗?

您可以将vertices <- union( head_of(g, which(E(g)$color == "red")), tail_of(g, which(E(g)$color == "red")) ) plot(g, vertex.color = ifelse(V(g) %in% vertices, 6, 1)) 属性分配给顶点?head_of以及边color

V(g)

enter image description here