R中的着色图连接

colouring graph connections in R

我在 R 中构建了一个图形,并成功地使用彩色 if 语句为一些顶点着色,我使用了 tkplot 函数以获得更好的可视化效果。
现在我有下图:

FROM    TO  
A   B
A   D
B   C
B   F
D   E
E   G
E   H
H   M
H   L
L   N

和下面的顶点集

E  
L

我需要绘制图表,将 E 和 L 中的连接传入和传出着色为红色,而所有其他颜色为黑色。
需要说明的是,我需要以下连接线为红色

FROM    TO
D   E
E   G
E   H
H   L
H   M

有解决办法吗?

只需用你想要的颜色创建一个颜色向量,对应于 B 中的边缘:

library(igraph) 
B = matrix( c("A" ,"A", "B" ,"B","D","E", "E", "H", "H", "L", "B","D","C","F","E","G","H","M","L","N"), ncol=2) 
B<- data.frame(B) 
grf<- graph.data.frame (B, directed =TRUE, vertices=NULL) 
error<-array(c("E", "L")) 
V(grf)$color <- ifelse(V(grf)$name %in% error, "red", "yellow") 

col = rep("black", nrow(B)) 
col[B$X1 == "E" | B$X2 == "L"] <- "red" 
# or 
# col[B$X1 %in% c("E", "L") | B$X2 %in% c("E", "L")] <- "red" 
plot(grf, edge.color = col)
# or     
# tkplot(grf, edge.color = col)

使用 edge.color 属性 并不是唯一的方法。 您还可以为每条边设置一个 color 属性,例如:
(有关 V(g)E(g) 函数的更多信息,请参见 here

library(igraph)

# initialize the graph
DF <- 
read.table(text=
"FROM TO  
A B
A D
B C
B F
D E
E G
E H
H M
H L
L N",header=T,stringsAsFactors=T)
g <- graph.data.frame(DF)

# set a color (black) attribute on all edges
E(g)$color <- 'black'
# set red color for the edges that have an incident vertex in the set 'E,L'
nodesSeq <- V(g)[name %in% c('E','L')]
E(g)[inc(nodesSeq)]$color <- 'red'

tkplot(g)