Skip to content

Error in validate_guide() when not attaching ggraph #386

@gdagstn

Description

@gdagstn

Hi,

I have a peculiar use case which triggers an error that I do not know how to address. Not sure if this can be really considered a bug, but I thought I'd report it anyway.

I want to use ggraph in a package where I have listed it as an optional dependency in Suggests, so rather than having the package and its functions imported to the NAMESPACE, I am calling the functions directly using ggraph::ggraph().

However, this triggers what I can only understand as some sort of conflict between ggraph and ggplot2 when I try to define a colour scale for edges.

Consider this reprex:

# Loading these packages as they are imported by the package
library(igraph)
library(ggplot2)

gr = make_graph("zachary")

# Mock up numeric attributes to mimic my use case
E(gr)$weight = runif(length(E(gr)))
E(gr)$prop = runif(length(E(gr)))
V(gr)$type = sample(c("A", "B", "C"), size = length(V(gr)), replace = TRUE)
V(gr)$name = paste0(V(gr)$type, V(gr))  

ggraph::ggraph(gr, layout = "fr") + 
  ggraph::geom_edge_link(aes(edge_color = .data[["weight"]], 
                                                     alpha = .data[["prop"]]), 
                                                     width = 1,
                                                     arrow = grid::arrow(length = unit(4, 'mm')),
                                                     end_cap = ggraph::circle(3, 'mm')) + 
  ggraph::geom_node_point(aes(color = .data[["type"]]), size = 3) + 
  ggraph::geom_node_text(aes(label = .data[["name"]])) +
  ggraph::scale_edge_color_continuous()

Error in `validate_guide()`:
! Unknown guide: edge_colourbar
Run `rlang::last_trace()` to see where the error occurred.

validate_guide() is a ggplot2 internal, and in fact last_trace() gives:

---
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. └─ggplot2:::print.ggplot(x)
  3.   ├─ggplot2::ggplot_build(x)
  4.   ├─ggraph:::ggplot_build.ggraph(x)
  5.   ├─base::NextMethod()
  6.   └─ggplot2:::ggplot_build.ggplot(x)
  7.     └─plot$guides$build(npscales, plot$layers, plot$labels, data)
  8.       └─ggplot2 (local) build(..., self = self)
  9.         └─self$setup(scales, aesthetics = aesthetics)
 10.           └─ggplot2 (local) setup(..., self = self)
 11.             └─base::lapply(...)
 12.               └─ggplot2 (local) FUN(X[[i]], ...)
 13.                 └─ggplot2:::validate_guide(guide)
Run rlang::last_trace(drop = FALSE) to see 2 hidden frames.

Note that if I remove the ggraph::scale_edge_color_continuous() line, the plot works:

ggraph::ggraph(gr, layout = "fr") + 
  ggraph::geom_edge_link(aes(edge_color = .data[["weight"]], 
                                                     alpha = .data[["prop"]]), 
                                                     width = 1,
                                                     arrow = grid::arrow(length = unit(4, 'mm')),
                                                     end_cap = ggraph::circle(3, 'mm')) + 
  ggraph::geom_node_point(aes(color = .data[["type"]]), size = 3) + 
  ggraph::geom_node_text(aes(label = .data[["name"]]))

Image

But it is clearly not what I want to do. Similarly, if I attach ggraph the code works, but that means adding it as a dependency, which I would like to avoid if possible.

Is there anything that can be done other than attaching ggraph?

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions