Search code examples
rigraphggraph

Ggraph node color to match edge color


Is it possible to get ggraph to plot node colors the same color as connected edge color? I've tried feeding ggraph the colors for edges and nodes manually without any luck. It seems as if this would be something rather trivial, but I can't find any direction on it. My question is somewhat similar to this question, but I would like to color my nodes the same as their out-degree edges.

library(tidyverse)
library(igraph)
library(ggraph)


g <- graph_from_data_frame(highschool)


ggraph(g)+
  geom_edge_fan(aes(color = from))+
  geom_node_point(aes(color = name), show.legend = F, size = 5)

enter image description here


Solution

  • This might work:

    colfunc <- colorRampPalette(c("#00008B", "#63B8FF"))
    cols <- colfunc(70)
    
    ggraph(g)+
      geom_edge_fan(aes(color = from)) +
      scale_edge_colour_gradient(low = "#00008B", high = "#63B8FF") + 
      geom_node_point(color = cols, show.legend = F, size = 3)
    

    enter image description here