Search code examples
rgraph-theoryminimum-spanning-treeweighted-graph

Minimum spanning trees with weight


At first I wanted to make a maximum spanning tree for my research, but I saw that it was going to be too complex given the time and the knowledge I had in R to do it properly.

So my option was to create a minimum spanning tree by reversing the order of the data. Indeed, working on ordinal data going from 1 to 3, I just had to reverse the order in the database.

I managed to find a syntax that helped me a lot here: https://web.stanford.edu/class/bios221/book/Chap-Graphs.html#minimum-spanning-trees I made the necessary adjustments to get a graph. However I can't put the weight on this graph...

Here is the goal I want to reach: to get a minimum spanning tree with the weight between each point without the titles of the nodes overlapping and with the nodes fitting correctly in the plot.

Here is what I can't do and what I don't understand:

The repel command doesn't work and I can't figure out why. R tells me that this command is unknown. I insert: geom_node_text(aes(label = name), repel = TRUE. It doesn't make the command buggy. I don't really understand why

Put the weight : I tried : graph.adjacency(mstree, mode = "undirected",weight=TRUE) visibly that I put weight true or false it does not change anything. Again, I must have missed something

I tried to make an example that is as close as possible to my problem. (This is the first time I publish an example, I hope I did it correctly) :

library(dplyr)
library(igraph)
library(ape)
library(tidyr)
library(ggplot2)
library(ggnetwork)
library(ggraph)

M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
                          "11th","12th","13th","14th","15th","16th","17th","18th","19th",
                          "20th","21th")

M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))

mstree <- ape::mst(M2)
gr4ph <-  graph.adjacency(mstree, mode = "undirected",weight=TRUE)
gg <-  ggnetwork(gr4ph, arrow.gap = 0, layout = layout_with_fr(gr4ph))
ggplot(gg, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_edges(color = "black", alpha = 1, curvature = 0.1) +
  geom_nodes(aes(color = name), size = 6) +  theme_blank() +
  geom_nodetext(aes(label = name), color = "black", size = 3.5) +
  theme(plot.margin = unit(c(0, 1, 2, 4), "cm"))+
  guides(color = guide_legend(keyheight = 0.09, keywidth = 0.09,
                              title = "Mots")) + theme(legend.position = c(-0.05, 0.14),
                                                       legend.background = element_blank(),
                                                       legend.text = element_text(size = 7))

Thank you for your help


Solution

  • The igraph doc page has an argument called weighted, which works.

    # weighted
    n1 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = TRUE)
    E(n1)$weight # 1 1 1 1 etc.
    
    # unweighted
    n2 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = NULL)
    E(n2)$weight # NULL