Search code examples
rgraphdata-visualizationnodes

R: change size of points in a graph


Using R and igraph, I have been able to plot graphs where:

A) The size of points (nodes) depend on the number of connections between points

B) The size of the points (nodes) depend on some pre-defined vector of sizes

The code for A) looks like this:

library(igraph)

my_data <- data.frame(

"Col_C" = c("AAA", "AAA", "AAB", "AAB", "AAB", "ABC", "CDE", "CDE", "CDE", "AAB", "AAB", "AAE"),
"Col_D" = c("AAB", "AAC", "AAC", "ABC", "CDE", "CDE", "AAB", "AAE", "ADF", "AAE", "ADF","ADF")
)

final <- data.frame(col = unique(unlist(my_data)))



graph <- graph.data.frame(my_data, directed=F)
graph <- simplify(graph)


V(graph)$vertex_degree <-  degree(graph)

plot(graph,
     vertex.label.cex = 0.8,
     edge.width = E(graph)$weight,
     vertex.size = V(graph)$vertex_degree 
     )

And the code for B) looks like this:

library(igraph)

my_data <- data.frame(

"Col_C" = c("AAA", "AAA", "AAB", "AAB", "AAB", "ABC", "CDE", "CDE", "CDE", "AAB", "AAB", "AAE"),
"Col_D" = c("AAB", "AAC", "AAC", "ABC", "CDE", "CDE", "AAB", "AAE", "ADF", "AAE", "ADF","ADF")
)

final <- data.frame(col = unique(unlist(my_data)))

final$size = c("6","10","1","20","50","1","5")

graph_B <- graph.data.frame(my_data, directed=F)
graph_B <- simplify(graph)

plot(graph_B, vertex.size = as.numeric(final$size))

However, when I try to make these graphs "interactive" (using the visNetwork library), the node sizes revert to equal sizes:

library(visNetwork)
visIgraph(graph)
visIgraph(graph_B)

Can someone please tell me what I am doing wrong?

Thanks


Solution

  • Perhaps this is what you are looking for?

     library("igraph")
    
     library("visNetwork")
    
     my_data <- data.frame( "Col_C" = c("AAA", "AAA", "AAB", "AAB", "AAB", "ABC", "CDE", "CDE", "CDE", "AAB", "AAB", "AAE"), "Col_D" = c("AAB", "AAC", "AAC", "ABC", "CDE", "CDE", "AAB", "AAE", "ADF", "AAE", "ADF","ADF") )
    
     final <- data.frame(col = unique(unlist(my_data)))
    
     final$size = c(6,10,1,20,50,1,5) 
    
     graph_B <- graph_from_data_frame(my_data, directed = FALSE, vertices = final)
    
     visIgraph(graph_B)