Search code examples
rigraphtransparencydirected-graphvertices

igraph in R: Directed graphs (with arrows) causes vertex color transparency


I am using the package igraph in R to be able to plot a network. However, when passing the directed = TRUE flag within the graph_from_data_frame command, it appears exactly how it should in R Studio's plot pane. However, once I preview as a PDF (Export, Save as PDF, Preview) OR Save Plot as PDF, all vertices of the graph are transparent. However, setting the directed = FALSE flag, the colors are as expected.

Setting up the data

stack.vtx <- data.frame(Dept = c("C-IIAC",  "EES-16",   "EES-17",   "A-1",  "ISR-1",    "ISR-2",    "ISR-6",    "NEN-5",    "AOT-AE",   "MPA-CINT", "MPA-MAGLAB",   "MPA-Q",    "MST-8",    "P-1",  "P-4",  "SIGMA-1",  "CCS-2",    "CCS-3",    "CCS-6",    "T-1",  "T-2",  "T-3",  "T-4",  "T-5",  "E-6",  "J-4",  "J-6",  "M-3",  "M-6",  "Q-5",  "AMPP-4",   "PT-DO",    "XCP-3",    "XCP-4",    "XCP-8",    "XTD-PRI"), 
                        Count = c(2,    2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  1,  1,  2,  1,  2,  3,  4,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2), 
                        Cols = c("#8B4513", "#8B4513",  "#8B4513",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#00CDCD",  "#00CDCD",  "#8A2BE2",  "#8A2BE2",  "#8A2BE2",  "#8A2BE2"))



stack.edges <- data.frame(from = c("AOT-AE",    "AOT-AE",   "C-IIAC",   "C-IIAC",   "CCS-2",    "EES-16",   "EES-16",   "EES-16",   "ISR-1",    "ISR-1",    "ISR-2",    "ISR-2",    "J-4",  "J-4",  "MPA-CINT", "MPA-CINT", "MPA-Q",    "MPA-Q",    "NEN-5",    "NEN-5",    "P-1",  "Q-5",  "T-1",  "T-1",  "T-2",  "T-2",  "T-2",  "T-2",  "XCP-4",    "XTD-PRI",  "XTD-PRI",  "XTD-PRI",  "XTD-PRI"), 
                          to = c("CCS-3",   "J-6",  "AMPP-4",   "PT-DO",    "T-5",  "T-5",  "XCP-8",    "A-1",  "CCS-6",    "T-2",  "EES-17",   "EES-17",   "P-1",  "M-3",  "T-4",  "MPA-MAGLAB",   "CCS-6",    "C-IIAC",   "XCP-3",    "SIGMA-1",  "T-2",  "T-1",  "MST-8",    "MST-8",    "CCS-2",    "ISR-6",    "MPA-Q",    "MPA-Q",    "T-3",  "P-4",  "E-6",  "XTD-PRI",  "M-6"), 
                          Cols = c("#66666660", "#66666660",    "#F0027F",  "#F0027F",  "#BEAED4",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#386CB0",  "#386CB0",  "#F0027F",  "#F0027F",  "#FDC086",  "#FDC086",  "#386CB0",  "#386CB0",  "#66666660",    "#66666660",    "#66666660",    "#FDC086",  "#7FC97F",  "#BEAED4",  "#66666660",    "#66666660",    "#66666660",    "#66666660",    "#BEAED4",  "#F0027F",  "#F0027F",  "#66666660",    "#66666660"))

Graphing the network

library(igraph)
stack_graph <- graph_from_data_frame(stack.edges, vertices = stack.vtx, directed = TRUE)

plot(stack_graph, 
     layout = layout_in_circle(stack_graph), 
     vertex.color = V(stack_graph)$Cols,
     vertex.shape = "sphere",
     vertex.label.color = "black",
     vertex.size = V(stack_graph)$Count * 4,
     vertex.label = "",
     edge.size = 0.3, 
     edge.color = E(stack_graph)$Cols, 
     edge.curved = 0.2, 
     edge.arrow.size = 0) #Changing this flag with anything > 0 will cause transparency issues of the graph vertices

The resulting graph with directed = TRUE OR edge.arrow.size = 0.1 (or anything greater than zero, will produce a graph with the vertex colors that are transparent. Here is what it looks like:

Transparent Vertices

However, the resulting graph when directed = FALSE OR edge.arrow.size = 0 will produce a graph with the vertex colors that are 'normal' or what is desired from this code. Here is what it looks like:

Non Transparent Vertices

The closest solution I have found can be found in this post: Unexpected arrow color behavior with semi-transparent edges (igraph, R).

it is suggested to make the vertices smaller (ex. vertex.size. However, reducing the vertex.size still keeps the transparent vertices (when directed = TRUE. Additionally, passing the edge.arrow.mode suggested in the post, has no effect.

Any suggestions as to how I can obtain non-transparent vertices when graph_from_data_frame(directed = TRUE)?


Solution

  • Change #66666660 into for example #666666. That is drop the alpha channel RRGGBB60 in #RRGGBBAA.

    This problem is also reproducible on Windows, Rgui.

    Small example

    library(igraph)
    stack_graph <- make_ring(2, directed=TRUE)
    
    plot(stack_graph, 
         layout = layout_in_circle(stack_graph), 
         vertex.shape = "sphere",
         edge.color = c("#66666660"),   # Transparency issues are caused by non valid RBG color code.
         edge.arrow.size = 2            # Changing this flag with anything > 0 will show transparency issues of the graph vertices.
         ) 
    

    Edit: If edge.color = rgb(red=0x66/255, green=0x66/255, blue=0x66/255, alpha=0.3) in plot() is set as transparent, then the vertices will also be rendered transparent after saving in the .pdf file. Most likely an igraph bug.