rigraph icon indicating copy to clipboard operation
rigraph copied to clipboard

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

Open clpippel opened this issue 3 years ago • 9 comments

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

  • R/igraph version: [1] compiler_4.1.3 magrittr_2.0.1 rlang_1.0.5 pkgconfig_2.0.3

  • R version: R version 4.1.3 (2022-03-10)

  • Operating system: Windows 10 x64 (build 22621) edges-disappear-after-second-identical-plot-statement-when-using-transparent-edge-color

clpippel avatar Jan 17 '23 09:01 clpippel

Related problems with alpha channel are observed in Stack Overflow:

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

clpippel avatar Jan 17 '23 09:01 clpippel

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 avatar Jan 20 '23 11:01 ntamas

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

ntamas avatar Jan 20 '23 11:01 ntamas

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?

clpippel avatar Jan 20 '23 13:01 clpippel

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 avatar Jan 20 '23 13:01 ntamas

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.

ntamas avatar Jan 20 '23 14:01 ntamas

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.

clpippel avatar Jan 20 '23 15:01 clpippel

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

ntamas avatar Mar 15 '23 21:03 ntamas

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

krlmlr avatar Mar 18 '23 05:03 krlmlr

The difference in colors comes from the fact that the arrow heads are drawn multiple time when edge.arrow.mode=3 (in the code below it is called code). The problem is the variable r.arr. When the forward edges are drawn, it is duplicated lx times (see 973) https://github.com/igraph/rigraph/blob/fc769f7ba78ba1686dd0114dfb69dcff91377f93/R/plot.R#L966-L973

The variable is not reset to its origin value and then again duplicated lx times for the backward edges (see L1003) https://github.com/igraph/rigraph/blob/fc769f7ba78ba1686dd0114dfb69dcff91377f93/R/plot.R#L987-L1003

Hence, the arrow heads get drawn on top of each other lx times and that becomes evident when the transparency is not 1.

This is fixed in #1709

schochastics avatar Feb 21 '25 07:02 schochastics