简体   繁体   English

R中的着色图连接

[英]colouring graph connections in R

I built in R a graph and I succeeded in colouring some vertex using if statement in colour, i used the tkplot function to have a better visualization. 我建立了R图,并使用if语句以颜色为某些顶点着色,我使用了tkplot函数来获得更好的可视化效果。
Now I have the following graph: 现在我有了下图:

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

and the following vertex set 和以下顶点集

E  
L

I need to plot the graph coloring the connection incoming and outcoming in E and L in RED colour while all the other in BLACK. 我需要用红色绘制E和L的连接图,用红色绘制E和L,同时用黑色绘制所有其他连接。
To be clear I need in red the following connections lines 为了清楚起见,我需要以下连接线为红色

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

Is there a solution for this? 有解决方案吗?

Just create a color vector with the colours you want, corresponding to the edges in B : 只需创建具有所需颜色的颜色矢量,即可与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)

Using edge.color property isn't the only way. 使用edge.color属性不是唯一的方法。 You can also set a color attribute to each edge, eg : 您还可以为每个边缘设置color属性,例如:
(more informations about V(g) and E(g) functions can be found here ) (有关V(g)E(g)函数的更多信息,请点击此处

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM