4

I need to generate a network with ggraph in r. What I want is adjust the edge width by weight variable (or the size of nodes). Does anyone know how I can do that? Thanks a lot.

Example code below:

library(ggraph)
library(igraph)
data=data.frame(w1=rep('like', 5), 
           w2 = c('apple', 'orange', 'pear','peach', 'banana'),
           weight= c(2,3,5,8, 15))
data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)
zesla
  • 11,155
  • 16
  • 82
  • 147
  • i think this link might help you https://github.com/thomasp85/ggraph/issues/144 – which_command May 16 '19 at 17:03
  • @which_command that issue seems to have been solved due to conflicting packages. It was closed a year ago. – camille May 16 '19 at 17:09
  • 2
    Have you looked at the docs for `geom_edge_link`? They list `edge_width` clearly as one of the aesthetics you can use with this geom – camille May 16 '19 at 17:10
  • @camille where is it? geom_edge_link(mapping = NULL, data = get_edges("short"), position = "identity", arrow = NULL, n = 100, lineend = "butt", linejoin = "round", linemitre = 1, label_colour = "black", label_alpha = 1, label_parse = FALSE, check_overlap = FALSE, angle_calc = "rot", force_flip = TRUE, label_dodge = NULL, label_push = NULL, show.legend = NA, ...) – zesla May 16 '19 at 17:23
  • Geom docs generally have a heading that says "Aesthetics" that lists the accepted aesthetics, i.e. things that can be mapped to data inside an `aes` and passed to `mapping` for that geom. `geom_edge_link` is no exception to this. `edge_width` is one of those aesthetics. – camille May 16 '19 at 17:37
  • thanks a lot. it works. – zesla May 16 '19 at 19:08

1 Answers1

6

Yes, you can do this with the width aes in most geom_edge_*'s. Also, you can use scale_edge_width to finetune the min/max width according to the weighted variable. See the two examples below.

Also, I believe there was an issue with this aesthetic to do with ggforce (it was causing me trouble too). Make sure you've updated to the most recent versions of ggraph and ggforce.

library(ggraph)
library(igraph)
data=data.frame(w1=rep('like', 5), 
                w2 = c('apple', 'orange', 'pear','peach', 'banana'),
                weight= c(2,3,5,8, 15))

The first with default weigthts

data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25, 
                 aes(width = weight)) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)+
  theme_graph()+
  labs(title = 'Graph with weighted edges', 
       subtitle = 'No scaling')

enter image description here

Using scale_edges_width to set the range. NOTE - scale_edges* can take multiple arguments.

data %>% 
  graph_from_data_frame() %>% 
  ggraph(layout = "fr") +
  geom_edge_link(alpha = .25, 
                 aes(width = weight)) +
  geom_node_point(color = "blue", size = 2) + 
  geom_node_text(aes(label = name),  repel = TRUE)+
  scale_edge_width(range = c(1, 20))+ # control size
  theme_graph()+
  labs(title = 'Graph with weighted edges', 
       subtitle = 'Scaling add with scale_edge_width()')

enter image description here

elliot
  • 1,844
  • 16
  • 45