无法在igraph上绘制网络 [英] Unable to plot a network on igraph
问题描述
我的二进制图基于边列表。每个顶点都是股票市场上的股票代码(例如:BARC =巴克莱的股票)
My binary graph is based on an edgelist. Every vertex is a ticker on stock markets (eg : BARC= Barclay's)
net_full_phase1=graph.edgelist(full_phase1, directed=FALSE)
V(net_full_phase1)$color=V(net_full_phase1)$name
V(net_full_phase1)$size=degree(net_full_phase1)
V(net_full_phase1)$color=gsub("BARC", "slategrey", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("BNP", "blue", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("CBK", "black", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("WFC", "red", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("BKIR", "orange", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("ISP", "purple", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("TPEIR", "lightblue", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("SAB", "yellow", V(net_full_phase1)$color)
V(net_full_phase1)$color=gsub("BCP", "green", V(net_full_phase1)$color)
plot(net_full_phase1, layout=layout.fruchterman.reingold)
我收到此错误:
符号错误(x = coords [,1],y = coords [,2],bg = vertex.color,:
nom de couleur' WFred'correcte
Error in symbols(x = coords[, 1], y = coords[, 2], bg = vertex.color, : nom de couleur 'WFred' incorrecte
只需运行 unique(as.character(V(net_full_phase1)$ name))
这是结果:
Just ran unique(as.character(V(net_full_phase1)$name))
here is the result :
"BARC" "WFC" "ISP" "TPEIR" "BCP" "SAB" "BNP" "CBK" "BKIR"
我也跑了: table(V(net_full_phase1)$ color)
结果:
black blue BredP green lightblue purple red redIR slategrey WFred yellow
R为什么不将某些颜色视为 BredP, redIR, WFred?
Why isn't R considering some colors as "BredP", "redIR", "WFred"?
推荐答案
我想您使用时会出现问题gsub
。以下是我处理此问题的方法。
I am guessing that something went amiss with your use of gsub
. Below is the way I would approach this.
# Your vector of unique names
nms <- c("BARC", "WFC", "ISP" ,"TPEIR" ,"BCP" ,"SAB" ,"BNP", "CBK" ,"BKIR")
使用与示例中相同的顶点名称创建小图
Create small graph with same vertex names as in your example
library(igraph)
g <- random.graph.game(length(nms), 0.5)
V(g)$name <- nms
创建查找表以将名称与颜色匹配:这会将颜色分配给唯一的顶点名称
Create lookup table to match the names to colours: this assigns colours to the unique vertex names
lookup <- setNames(
c("slategrey", "red", "purple", "lightblue", "green", "yellow", "blue", "black", "orange"),
nms)
# have a look at object
lookup
# BARC WFC ISP TPEIR BCP
# "slategrey" "red" "purple" "lightblue" "green"
# SAB BNP CBK BKIR
# "yellow" "blue" "black" "orange"
然后我们可以使用子集( [
)将它们分配给顶点颜色属性
We can then use subset ([
) to assign these to the vertex colour attribute
V(g)$color <- lookup[V(g)$name]
# have a look at what is produced
V(g)$color
# [1] "slategrey" "red" "purple" "lightblue" "green"
# [6] "yellow" "blue" "black" "orange"
哪个生产
< a href = https://i.stack.imgur.com/y47JJ.jpg rel = nofollow noreferrer>
PS,我无法复制您的 gsub
结果:代码可以正常工作
PS, I can't reproduce your gsub
result: the code works okay
# Your vector of unique names
nms <- c("BARC", "WFC", "ISP" ,"TPEIR" ,"BCP" ,"SAB" ,"BNP", "CBK" ,"BKIR")
nms = gsub("BARC", "slategrey", nms )
nms = gsub("BNP", "blue", nms )
nms = gsub("CBK", "black", nms )
nms = gsub("WFC", "red", nms )
nms = gsub("BKIR", "orange", nms )
nms = gsub("ISP", "purple", nms )
nms = gsub("TPEIR", "lightblue", nms )
nms = gsub("SAB", "yellow", nms )
nms = gsub("BCP", "green", nms )
# look at result
nms
# [1] "slategrey" "red" "purple" "lightblue" "green" "yellow" "blue"
# [8] "black" "orange"
这篇关于无法在igraph上绘制网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!