thomasp85 / ggraph

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

"width" argument in "geom_edge_link" shows unobserved steps in legend #308

Closed timonelmer closed 2 years ago

timonelmer commented 2 years ago

When passing a numeric vector to the width argument of the geom_edge_link function, the figure legend also shows half-steps of the width (e.g., 1.5,2.5) that are not observed in the original vector e.g., (c(1,2,3,1,2)).

an example:

 gr <- create_notable('bull') %>%
     mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
     activate(edges) %>%
     mutate(class = sample(letters[1:3], n(), replace = TRUE))

 ggraph(gr, 'stress') +
     geom_edge_link(aes(width = c(1,2,3,1,2)))
schochastics commented 2 years ago

Dont think this is an issue with ggraph, because the same behaviour appears in ggplot

library(ggplot2)
data.frame(x=c(0,0,0,0,0),
           xend=c(1,0,-1,0,0.5),
           y=c(0,0,0,0,0),
           yend=c(0,1,0,-1,-0.5),
           w= c(1,2,3,1,2)) |>
  ggplot()+
  geom_segment(aes(x,y,xend=xend,yend=yend,size=w))

Created on 2022-05-03 by the reprex package (v2.0.1)

thomasp85 commented 2 years ago

This is because it is a continuous scale and it ggraph/ggplot2 will create evenly spaced breaks for such guides - this is also what most people will assume to get.

If you have 3 evenly spaced values and you only want to see those you can manually specify the breaks in a number of ways, e.g. by count

gr <- create_notable('bull') %>%
    mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
    activate(edges) %>%
    mutate(class = sample(letters[1:3], n(), replace = TRUE))

ggraph(gr, 'stress') +
    geom_edge_link(aes(width = c(1,2,3,1,2))) + scale_edge_width(n.breaks = 3)

or by setting them directly