user123456 user123456 - 1 year ago 100
R Question

Unable to plot a network on igraph

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=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

here is the result :


I also ran :
with result :

black blue BredP green lightblue purple red redIR slategrey WFred yellow

Why isn't R considering some colors as "BredP", "redIR", "WFred"?

Answer Source

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

g <-, 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"),

# have a look at object
#        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
# [1] "slategrey" "red"       "purple"    "lightblue" "green"    
# [6] "yellow"    "blue"      "black"     "orange"  

Which produces

enter image description here

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
# [1] "slategrey" "red"       "purple"    "lightblue" "green"     "yellow"    "blue"     
# [8] "black"     "orange"  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download