Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
clpippel opened this issue Jan 17, 2023 · 10 comments · Fixed by #1709
Assignees
Labels
bug an unexpected problem or unintended behavior plotting 💹 Issues related to plotting
Milestone

Comments

@clpippel
Copy link
Contributor

clpippel commented Jan 17, 2023

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
Copy link
Contributor Author

clpippel commented Jan 17, 2023

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.

@ntamas
Copy link
Member

ntamas commented Jan 20, 2023

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 133f74d

@ntamas
Copy link
Member

ntamas commented Jan 20, 2023

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
Copy link
Contributor Author

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
Copy link
Member

ntamas commented Jan 20, 2023

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
Copy link
Member

ntamas commented Jan 20, 2023

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
Copy link
Contributor Author

clpippel commented Jan 20, 2023

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
Copy link
Member

ntamas commented Mar 15, 2023

@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
Copy link
Contributor

krlmlr commented Mar 18, 2023

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

@krlmlr krlmlr added the bug an unexpected problem or unintended behavior label May 20, 2023
@krlmlr krlmlr added the plotting 💹 Issues related to plotting label Jun 20, 2023
@krlmlr krlmlr added this to the triage milestone Feb 20, 2024
@schochastics schochastics self-assigned this Feb 21, 2025
@schochastics
Copy link
Contributor

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)

rigraph/R/plot.R

Lines 966 to 973 in fc769f7

## forward arrowhead
if (code %in% c(2, 3)) {
theta <- atan2((by2 - y1) * uin[2], (bx2 - x1) * uin[1])
Rep <- rep(length(deg.arr), lx)
p.x2 <- rep(bx2, Rep)
p.y2 <- rep(by2, Rep)
ttheta <- rep(theta, Rep) + rep(deg.arr, lx)
r.arr <- rep(r.arr, lx)

The variable is not reset to its origin value and then again duplicated lx times for the backward edges (see L1003)

rigraph/R/plot.R

Lines 987 to 1003 in fc769f7

## backward arrow head
if (code %in% c(1, 3)) {
x1 <- bx1
y1 <- by1
tmp <- x1
x1 <- x2
x2 <- tmp
tmp <- y1
y1 <- y2
y2 <- tmp
theta <- atan2((y2 - y1) * uin[2], (x2 - x1) * uin[1])
lx <- length(x1)
Rep <- rep(length(deg.arr), lx)
p.x2 <- rep(x2, Rep)
p.y2 <- rep(y2, Rep)
ttheta <- rep(theta, Rep) + rep(deg.arr, lx)
r.arr <- rep(r.arr, lx)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior plotting 💹 Issues related to plotting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants