9

Can the letter "a" be removed from the legend associated with e.g. a fill or colour aesthetic, in a ggraph network plot, like in the simple example below ?

library(igraph)
library(ggraph)

g1 <- make_ring(6)
vertex_attr(g1) <- list(name = LETTERS[1:6],  type =rep(c("typeA", "typeB", "typeC"), 2))

ggraph(g1) + geom_node_label(aes(label = name, fill = type)) + 
  geom_edge_diagonal() + theme_graph()

In the case of geom_text, show.legend = FALSE solves it,

Remove 'a' from legend when using aesthetics and geom_text

but adding show.legend = FALSE within geom_node_label(), removes the legend completely.

is there any solution for this in ggraph?

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • 4
    change the key to `GeomLabel$draw_key <- function (data, params, size) { grobTree(draw_key_rect(data)) }` and redraw . [similar](https://stackoverflow.com/questions/49965758/change-geom-texts-default-a-legend-to-label-string-itself/49966784#49966784) – user20650 Apr 23 '18 at 20:14
  • 2
    (ps you will need `library(grid)`) . actually you can use `GeomLabel$draw_key <- function (data, params, size) { draw_key_rect(data) }` – user20650 Apr 23 '18 at 20:18
  • this worked, thank you @user20650 – Alexandros Papageorgiou Apr 24 '18 at 10:26

1 Answers1

2

Answer to the original question based on comments above: The following line of code has to be added on the top of the script as per @user20650 solution.

library(grid)
GeomLabel$draw_key <- function (data, params, size) { draw_key_rect(data) }

If repel = TRUE argument is used inside geom_node_label, then in addition to the above GeomLabelRepel$draw_key <- GeomLabel$draw_key needs to be added.

Henrik
  • 65,555
  • 14
  • 143
  • 159