thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.08k stars 116 forks source link

geom_edge_link(aes(alpha= weight)) doesn't change opacity #317

Closed ReeceAdamMoore closed 2 years ago

ReeceAdamMoore commented 2 years ago

I would like to refer to a previous issue that was closed.

When I use the following code:

ggraph(g, layout = "with_kk")+ geom_edge_link(aes(alpha = weight)) + geom_node_point(aes(color = party_code))+ geom_node_text(aes(label = id), repel = TRUE, max.overlaps = 100, color = "red")

It does not change the translucency of the edges as I would hope. All edges remain the same opacity.

I have installed the new R, I have uninstall and reinstalled the packages required. I have installed all updates requested by the developer versions of ggraph & ggforce.

What could the issue be?

schochastics commented 2 years ago

Do you have a reprex for the issue? I cannot reproduce any problem with opacity. The example below shows that ggraph behaves exactly as ggplot2 does.

library(ggplot2)
library(ggraph)
library(igraph)
library(patchwork)

# create a random network with attributes
g <- sample_gnp(20, 0.2)
E(g)$weight <- sample(1:34, ecount(g), replace = TRUE)
V(g)$party_code <- sample(LETTERS[1:5], vcount(g), replace = TRUE)
V(g)$id <- 1:vcount(g)

# plot using ggraph 

p1 <- ggraph(g, layout = "stress") +
  geom_edge_link(aes(alpha = weight), edge_width = 4) +
  geom_node_point(aes(color = party_code), size = 5) +
  theme_minimal() +
  labs(title = "ggraph")

# transform graph to a data frame
xy <- graphlayouts::layout_with_stress(g)
el <- as_data_frame(g, "edges")
el$x <- xy[el[, 1], 1]
el$y <- xy[el[, 1], 2]
el$xend <- xy[el[, 2], 1]
el$yend <- xy[el[, 2], 2]

nodes <- as_data_frame(g, "vertices")
nodes$x <- xy[, 1]
nodes$y <- xy[, 2]

# plot with ggplot2
p2 <- ggplot() +
  geom_segment(data = el, aes(x = x, xend = xend, y = y, yend = yend, alpha = weight), size = 4) +
  geom_point(data = nodes, aes(x = x, y = y, color = party_code), size = 5) +
  theme_minimal() +
  labs(title = "ggplot2")

p1 + p2

Created on 2022-07-04 by the reprex package (v2.0.1)