rigraphvisnetwork

How can I modify the font size of the node labels with visIgraph?


I am plotting a graph that I construct via the igraph package using visNetwork::visIgraph and have seen that font.size is usually used as a parameter to control the sizes of the labels, but it does not work in my code to construct the visualisation, as below.

How do I change the font size of the labels in the below code?

library(igraph)
library(visNetwork)

rnd_dag <- function(p = 25, seed = 123, p_edge = 0.2) {
  if (seed) set.seed(seed)
  A <- matrix(0, p, p)
  A[lower.tri(A)] <- sample(c(0, 1), p*(p-1)/2, replace = TRUE, 
                            prob = c(1 - p_edge, p_edge))
  return(A)
}

rnd_dag(25, p_edge = 0.1) %>% 
  graph_from_adjacency_matrix %>% 
  visIgraph(layout = "layout_with_sugiyama")

Solution

  • The reason you can't control the font size is that your labels are integer-type. The labels have to be character-type for the labels to be recognized as labels.

    x = rnd_dag(25, p_edge = 0.1) %>% 
      graph_from_adjacency_matrix %>% 
      visIgraph(layout = "layout_with_sugiyama") %>% 
      visNodes(font = list(size = 50))
    
    x$x$nodes$label <- as.character(1:25) # change labels to character-type
    
    x
    

    enter image description here