Ggraph: Assigning meaningful node annotation to network

How do I meaningfully map task_difficulty, an integer 1,2,3,4,5, to the nodes, with e.g. size, given that each person, performing the task is not a task :thinking:

Elaboration follows here:

I have this example network, where:

  • Names are persons
  • v, w, x, y, z are tasks
  • A, B, C are roles relative to the task


Created using this example code:

# Load libraries ----------------------------------------------------------
library("tidyverse")
library("ggraph")
library("tidygraph")


# Define example data -----------------------------------------------------
set.seed(651906)
n <- 30
my_data <- tibble(
  person = sample(c("Georgina",
                    "Jeanice",
                    "Emmitt",
                    "Agripina",
                    "Gregg"),
                  size = n,
                  replace = TRUE),
  task = sample(letters[22:26],
                size = n,
                replace = TRUE),
  role = sample(LETTERS[1:3],
                size = n,
                replace = TRUE),
  task_difficulty = sample(seq(1,5),
                           size = n,
                           replace = TRUE))


# Wrangle data ------------------------------------------------------------
my_data_graph <- my_data %>%
  as_tbl_graph


# Create graph ------------------------------------------------------------
my_data_graph %>% 
  ggraph(layout = "kk") +
  geom_edge_link(aes(edge_colour = role)) +
  geom_node_point() +
  geom_node_text(aes(label = name),
                 repel = TRUE) +
  theme_graph()

I.e. the example data looks like so:

> my_data
# A tibble: 30 x 4
   person   task  role  task_difficulty
   <chr>    <chr> <chr>           <int>
 1 Georgina x     C                   3
 2 Emmitt   x     C                   4
 3 Gregg    z     C                   5
 4 Jeanice  z     B                   3
 5 Agripina v     B                   1
 6 Emmitt   w     C                   2
 7 Jeanice  w     B                   5
 8 Georgina w     B                   3
 9 Emmitt   x     B                   4
10 Jeanice  w     B                   1
# … with 20 more rows

So, the idea is, that some nodes are only "from" nodes and they should somehow be omitted from mapping?

So thinking and testing, I think this is close to what I was trying to obtain:

# Load libraries ----------------------------------------------------------
library("tidyverse")
library("ggraph")
library("tidygraph")


# Define example data -----------------------------------------------------
set.seed(651906)
n <- 30
my_data <- tibble(
  person = sample(c("Georgina",
                    "Jeanice",
                    "Emmitt",
                    "Agripina",
                    "Gregg"),
                  size = n,
                  replace = TRUE),
  task = sample(letters[22:26],
                size = n,
                replace = TRUE),
  role = sample(LETTERS[1:3],
                size = n,
                replace = TRUE)) %>% 
  mutate(task_class = case_when(task == "v" ~ "class_1",
                                task == "w" ~ "class_2",
                                task == "x" ~ "class_3",
                                task == "y" ~ "class_4",
                                task == "z" ~ "class_5"))


# Wrangle data ------------------------------------------------------------
my_data_graph <- my_data %>%
  as_tbl_graph %>%
  full_join(my_data %>%
              select(task, task_class) %>%
              distinct,
            by = c("name" = "task"))


# Create graph ------------------------------------------------------------
my_data_graph %>% 
  ggraph(layout = "kk") +
  geom_edge_link(aes(edge_colour = role)) +
  geom_node_point(aes(shape = task_class), size = 3) +
  geom_node_text(aes(label = name),
                 repel = TRUE) +
  theme_graph() +
  scale_shape_discrete(na.translate = FALSE)

Yielding:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.