Hi all,
I'm working on creating a network diagram using igraph
and ggraph
. I would like to map the size of the node text labels and/or points back to the data. Here's some fake data to illustrate the point.
library(tidyverse)
words <- c("one", "two", "three", "four")
probs <- c(0.1, 0.2, 0.3, 0.4)
set.seed(99)
raw <- tibble(
word1 = sample(words, 500, TRUE, prob = probs),
word2 = sample(words, 500, TRUE, prob = probs)
)
counts <- count(raw, word1, word2, sort = TRUE)
head(counts)
#> # A tibble: 6 x 3
#> word1 word2 n
#> <chr> <chr> <int>
#> 1 four four 81
#> 2 three four 65
#> 3 four three 62
#> 4 two four 49
#> 5 four two 36
#> 6 three three 34
The parallel in "base" ggplot2
would be to set the size aesthetic with aes(size = n)
. Here's my set up for the network diagram.
library(igraph)
library(ggraph)
counts %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(
aes(edge_alpha = n),
arrow = grid::arrow(type = "closed", length = unit(0.15, "inches")),
show.legend = FALSE
) +
geom_node_point() +
geom_node_text(
aes(label = name),
vjust = 1,
hjust = 1
) +
theme_void()
The edge_alpha
aesthetic can be mapped to n
, however, when I go to set size
aesthetics, it throws an error. I'm having a tough time deciphering this and finding a potential solution in the documentation. Under the documentation for geom_node_point
it says that the function understands the size
aesthetic.
counts %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(
aes(edge_alpha = n),
arrow = grid::arrow(type = "closed", length = unit(0.15, "inches")),
show.legend = FALSE
) +
# -- map point size to data --
geom_node_point(aes(size = n)) +
geom_node_text(
aes(label = name),
vjust = 1,
hjust = 1
) +
theme_void()
#> Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
#> Error: Column `size` must be a 1d atomic vector or a list
I'm not savvy as to what the data structure is after graph_from_data_frame()
is called, but perhaps this is where I'm going wrong? Or maybe there's a different aesthetic to set the size which I'm unaware of?
Thanks in advanced for the help!