igraph / rigraph

igraph R package
https://r.igraph.org
555 stars 201 forks source link

Edges disappear after second identical plot() statement; when using transparent edge colors (alpha < 1). #640

Open clpippel opened 1 year ago

clpippel commented 1 year ago

Describe the bug Edges disappear after second identical plot() statement when using transparent edge colors (alpha < 1).

Note par()$xpd is flipped from FALSE to TRUE.

To reproduce

library(igraph)
g <- graph_from_literal(A +-+ B)
dev.new(); par()$xpd                # xpd = FALSE

# First  try after dev.new(), edge is  visible.
plot(g,  edge.color=rgb(0,0,0,0.3));
par()$xpd                           # xpd = TRUE

# Second try, edge is not visible.
# Edge becomes visible after save as .pdf").
plot(g,  edge.color=rgb(0,0,0,0.3)) 

# Third try. 
par(xpd=FALSE)                      # reset xpd 
plot(g,  edge.color=rgb(0,0,0,0.3)) # Edge is visible again.

Version information

clpippel commented 1 year ago

Related problems with alpha channel are observed in Stack Overflow:

ntamas commented 1 year ago

Is this happening with the latest development version from the main branch? I have recently fixed an issue that could be related to this in 133f74de8636e6db026f772d7307cd35a2c2e03d

ntamas commented 1 year ago

Re the issue you mentioned in your second comment, there seems to be some issue there, but the edges are okay - the transparencies of the edge arrowheads seem to be incorrect to me with the latest version from main, at least on macOS. See the attached image.

Screenshot 2023-01-20 at 12 17 39
clpippel commented 1 year ago

The transparencies of the arrow heads are also wrong on Windows, igraph version ‘1.3.4’. This is a separate issue.

Regarding the par()$xpd issue: how to download and try the latest development version for Windows?

ntamas commented 1 year ago

Ah sorry, I forgot that you are on Windows. I'll submit the source to win-builder and post a link to the results here.

ntamas commented 1 year ago

Here's the latest dev version for R 4.2:

https://win-builder.r-project.org/DFE1mD2399tN/

It will be online for the next 72 hours. You can install it by downloading it and then typing install.packages(file.choose(), repos=NULL) in the R command line.

clpippel commented 1 year ago

I have downloaded and installed the igraph version as requested. [1] ‘1.3.5.9098’

To summarize: (1) As expected the edges do not disappear. And par$xpd stays FALSE. (2) The Directed graphs (with arrows) causes vertex color transparency problem is solved. The following program shows no vertices with transparent colors:

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.
     ) 

(3) The transparencies of the edge arrowheads are still incorrect.

     plot( make_tree(4)
       , edge.arrow.mode=3
       , edge.color=rgb(0,0,0,0.2)
   )   # some arrow colors are rendered transparent.

The problem does not occur with make_tree(n) with n < 4.

ntamas commented 1 year ago

@krlmlr I'm a bit stumped with this issue; can you take a look at it whenever it's convenient for you.

To reproduce:

library(igraph)
plot(make_tree(4), edge.arrow.mode=3, edge.color=rgb(0,0,0,0.2))

edge.arrow.mode essentially instructs igraph to draw arrows on both endpoints of an edge. The problem is that when the edge color uses alpha transparency, then the "real" arrowhead (i.e. the one at the head of the edge) is drawn correctly, but the "other" arrowhead (at the tail of the edge) is darker (i.e. uses a higher alpha than what it should be). If I change edge.arrow.mode to 2 (which draws an arrowhead only at the tail but not at the head) or 1 (which draws an arrowhead at the head but not at the tail), then everything is okay. There must be a logic error in the internal igraph.Arrows function, but I can't figure out what the problem is.

krlmlr commented 1 year ago

We would need more visual tests to have confidence in fixing or refactoring the plotting code.