L. Tucker
L. Tucker

Reputation: 543

ggraph use fill and color in geom_node_point

I'm trying to make a sample ego network plot, but coloring the nodes using "fill" isn't working for me. This may be a trivial issue, but I can't figure out the problem. This is my code:

library(tidyverse)
library(igraph)
library(ggraph)
library(tidygraph)
library(ggplot2)

edges <- read.table(text = 
"ego    wave    fid1        fid2        fid3        fid4        fid5
 Ego    1       Friend_A    Friend_B    Friend_C    NA        Friend_D
 Ego    2       Friend_E    Friend_F    NA        NA          Friend_G
 Ego    3       Friend_H    NA        Friend_I  Friend_G    Friend_J
 Ego    4       Friend_H    NA        NA        NA        NA
 Ego    5       Friend_K    NA        NA          NA          Friend_F", header = TRUE) %>%

  mutate_all(function(x) gsub("_"," ",x)) %>%
  pivot_longer(.,
               cols = c(fid1:fid5)) %>%
  select(., ego, alter = value, wave) %>% na.omit()

ego <-   as.data.frame(edges$ego) %>%
         rename("id" = "edges$ego")
alter <- as.data.frame(edges$alter) %>%
         rename("id" = "edges$alter")

nodes <- bind_rows(ego, alter) %>% distinct() %>%
         mutate(label = case_when(id == "Ego" ~ 1,
                                  TRUE ~ 0))


g1 <- graph_from_data_frame(d = filter(edges, wave == 1), vertices = nodes, directed = TRUE) %>%
      delete.vertices(., which(degree(.)==0))


as_tbl_graph(g1) %>%
      create_layout(., layout = 'stress') %>%
      ggraph(.) + 
      geom_edge_link(color = "grey", 
                     arrow = arrow(type = "closed",
                                   angle = 25,
                                   length = unit(1.5, 'mm')), 
                     end_cap = circle(3.5, 'mm'), 
                     width = 0.5, show.legend = FALSE) +        
      geom_node_point(aes(fill = factor(label)), size = 7, color = "black") +
      scale_fill_hue(l=40) +
      geom_node_text(aes(label = name), vjust = -1) +
      theme_graph()+
      theme(legend.position = "none")+
      labs(title = "Wave 1")

With this code I get this plot:enter image description here

However, my goal is to have the nodes colored by the label variable (where ego's color is different from alters) and to have a black outline around each node. Any idea what I'm doing wrong here?

Upvotes: 2

Views: 2617

Answers (2)

TarJae
TarJae

Reputation: 78927

Update

With the input of tjebo (Many thanks!) the solution for ("...and to have a black outline around each node") could be:

  • Change this line

geom_node_point(aes(fill = factor(label)), size = 7, color = "black") +

  • to

geom_node_point(aes(fill = factor(label)), shape = 21, size = 7, color = "black") +

Output enter image description here

First try

Change: geom_node_point(aes(fill = factor(label)), size = 7, color = "black") + to geom_node_point(aes(color = factor(label)), size = 7) +

Upvotes: 3

tjebo
tjebo

Reputation: 23747

This is to extend my comment(s) above, and also an answer. The example is taken directly from ?geom_node_point.

You have basically asked a "how to change fill and color of points" question, which has been answered many times. There is of course more than one way, and below I am just using a different shape.

library(ggraph)
#> Loading required package: ggplot2
library(tidygraph)

gr <- create_notable('bull') %>%
  mutate(class = sample(letters[1:3], n(), replace = TRUE))

ggraph(gr, 'stress') + 
  geom_node_point(aes(fill = class), shape = 21, size = 7, color = "black")

Created on 2021-04-05 by the reprex package (v1.0.0)

Upvotes: 2

Related Questions