问题
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)
I get this error :
Error in symbols(x = coords[, 1], y = coords[, 2], bg = vertex.color, : nom de couleur 'WFred' incorrecte
Just ran unique(as.character(V(net_full_phase1)$name))
here is the result :
"BARC" "WFC" "ISP" "TPEIR" "BCP" "SAB" "BNP" "CBK" "BKIR"
I also ran : table(V(net_full_phase1)$color)
with result :
black blue BredP green lightblue purple red redIR slategrey WFred yellow
Why isn't R considering some colors as "BredP", "redIR", "WFred"?
回答1:
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"
Which produces
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"
来源:https://stackoverflow.com/questions/40428534/unable-to-plot-a-network-on-igraph