Search code examples
rggraph

ggraph specify both fill and colour of nodes


I have seen examples of how to get node fill from the aesthetic and set the colour to have a black margin around the node. But if I try to specify a fixed color and fill I just get black nodes.

ggraph(igraph::graph_from_adjacency_matrix(matrix(sample(0:1, 100, replace=TRUE, 
                                                         prob=c(0.8,0.2)), nrow=10), 
                                           mode='undirected', diag=F)) + 
  geom_edge_link(colour = "grey") + 
  geom_node_point(fill = "red", 
                  colour = "black", 
                  size = 10) + 
  theme(legend.position = 'none') +
  theme_graph(background = "white")

How can I specify the colour in this case?


Solution

  • By Default, it takes a solid shape (shape = 19) in which 'fill' would be meaningless. Specifying the shape and adjusting the stroke would help.

    ggraph(igraph::graph_from_adjacency_matrix(matrix(sample(0:1, 100, replace=TRUE, 
                                                             prob=c(0.8,0.2)), nrow=10), 
                                               mode='undirected', diag=F)) + 
      geom_edge_link(colour = "grey") + 
      geom_node_point(fill = "red", 
                      colour = "black", 
                      size =10,
                      shape = 21, # Change the shape
                      stroke = 2) + # Specify the width of margin
      theme(legend.position = 'none') +
      theme_graph(background = "white")
    

    enter image description here