thomasp85 / ggraph

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

Allow different end_shapes to be passed to geom_edge_span #371

Open nicoblokker opened 6 months ago

nicoblokker commented 6 months ago

Hi,

It would be helpful to have the capability of passing a vector of shapes to geom_edge_span instead of a single element. This functionality was available in a previous version (2.0.5), I recall.

library(ggraph)
#> Loading required package: ggplot2
graph <- tidygraph::create_notable('zachary')

# single end_shape works fine
ggraph(graph, 'fabric', sort.by = node_rank_fabric()) + 
  geom_node_range(colour = 'grey') + 
  geom_edge_span(end_shape = 'square') + 
  coord_fixed()

# multiple shapes cause an error
ggraph(graph, 'fabric', sort.by = node_rank_fabric()) + 
  geom_node_range(colour = 'grey') + 
  geom_edge_span(end_shape = c('square', 'circle', 'cross')) + 
  coord_fixed() 
#> Error in `geom_edge_span()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error in `if (is.na(end_shape)) ...`:
#> ! the condition has length > 1
Second Image First Image

The right graph was created by modifying R/geom_edge.R to support conditions longer than 1:

# line 318
if (is.na(end_shape)) return(panel)
if (all(is.na(end_shape))) return(panel)

Created on 2024-05-20 with reprex v2.1.0