1

I am trying to visualize a network while customizing some styles. I have not succeeded in the task up to now.

Requirements:

  • Only edges with a weight of 2 should have an arrow pointing in the direction of the link.
  • An edge should be of the same colour as the node it is departing from.

Code:

library(ggraph)
library(igraph)
library(graphlayouts)
library(tibble)
library(dplyr)

nodes <- data.frame(name = c('a','b','c','d','e')) 

links <- data.frame(
  from = c('a', 'b', 'd', 'a', 'd', 'a', 'c'),
  to = c('b', 'd', 'e', 'c', 'a', 'e', 'e'),
  weight = c(1, 1, 2, 2, 2, 1, 2),
  linetype = c('dashed', 'dashed', 'solid', 'solid', 'solid', 'dashed', 'solid')
)


tbl_graph_net <- as.igraph(tidygraph::tbl_graph(nodes = nodes, edges = links))

layout <- create_layout(tbl_graph_net, 
                        layout = 'igraph', 
                        algorithm = "nicely")

colfunc <- colorRampPalette(c("blue", "red"))
cols <- colfunc(5)

ggraph(tbl_graph_net, layout = "stress") +
  # Edges
  geom_edge_fan(aes(colour = from, 
                    edge_width = weight, 
                    linetype = linetype)) +
  scale_edge_linetype_manual(limits = as.factor(links$linetype), 
                             values = links$linetype) +
  scale_edge_colour_gradient(low = "blue", high = "red") +
  # Nodes
  geom_node_point(colour = cols, size = 10) +
  # Labels
  geom_node_text(aes(label = name), 
                 size = 7, 
                 colour = "#FFFFFF") +
  # Theme
  theme_graph() +
  theme(legend.position = "none")

I am aware of the existence of scale_edge_color_manual but don't know how to correctly use it. Similarly, I don't know if I am scaling the nodes' colours correctly with scale_color_manual.

Matching edge and node colours should be definitely possible, but I have no idea of how to draw an arrow only on some edges. If ggraph does not allow it, I am happy to accept another solution with igraph or something similar.

EDIT:

I managed to apparently match edge and node colour. Code is updated. Only arrows are missing

Luisda
  • 190
  • 13

1 Answers1

0

You can use the geom_edge_link2() variant in ggraph to make edges the same colour as the node they are coming from.

As for arrows, I recently asked a similar question. I was provided a solution using base/igraph here

Jatson
  • 43
  • 6
  • I am trying both geom_edge_link2 and geom_edge_fan2 but cannot make it work. Do I need to pass a particular aesthetic? – Luisda Sep 11 '20 at 09:05