Search code examples
rigraph

How to make the arrow lines shorter in igraph in R?


I have made an igraph plot. But, I want to make the arrow lines shorter now. This is the data:

dput(sample)
structure(list(NMSUKU = c("Aceh/ Achin/ Akhir/ Asji/ A-Tse/ Ureung Aceh", 
"Alas", "Aneuk Jamee", "Gayo", "Gayo Lut", "Gayo Luwes", "Gayo Serbe Jadi", 
"Kluet", "Sigulai", "Simeulue", "Simeulue", "Simeulue", "Singkil", 
"Singkil", "Tamiang"), TopLang = c("Aceh/ Acheh/ Achi ", "Alas ", 
"Aceh Jamee ", "Gajo/ Gayo ", "Gajo/ Gayo ", "Gajo/ Gayo ", "Gajo/ Gayo ", 
"Aceh Kluet ", "ERROR  TopCol out of range ", "Long Bano/ Simalur/ Simeuloe/ Simeulue/ Simulul ", 
"Aceh Simeleu Barat ", "Aceh Simeleu Tengah ", "Aceh Hulu Singkil ", 
"Aceh Hulu Singkil ", "Tamiang "), Ethnicity = c("1_Aceh/ Achin/ Akhir/ Asji/ A-Tse/ Ureung Aceh  ", 
"2_Alas  ", "3_Aneuk Jamee  ", "4_Gayo  ", "6_Gayo Luwes  ", 
"5_Gayo Lut  ", "7_Gayo Serbe Jadi  ", "8_Kluet  ", "NA  ", "10_Simeulue  ", 
"10_Simeulue  ", "10_Simeulue  ", "11_Singkil  ", "17_Batak Pakpak Dairi  ", 
"12_Tamiang  ")), row.names = c(NA, -15L), class = "data.frame")

This is what got: enter image description here

The arrow lines are too long but I want to make them shorter ( adjust as I want). Any solution? Thank you!


Solution

  • I suspect you cannot shorten all edges since relative lengths are taken for plot. You can see the following two plots

    • Shorten the edges on left part by
    m <- as.matrix(replace(sample, sample == "", "NA"))
    g <- simplify(graph_from_edgelist(rbind(m[, 1:2], m[, 2:3]), directed = TRUE))
    l <- layout_with_sugiyama(g)
    l$layout[, 2][l$layout[, 2] == 1] <- l$layout[, 2][l$layout[, 2] == 1] * 0.1 # this is the line change the horizontal coordinates for edges on the left part
    plot(g,
      layout = -l$layout[, 2:1],
      edge.arrow.size = 0.1,
      vertex.size = 2.5,
      vertex.color = "grey",
      vertex.label.dist = 1,
      edge.arrow.width = 1.5,
      edge.width = seq(0.5, 0.08),
      edge.lty = "solid",
      edge.color = "gray",
      vertex.label.cex = 0.7,
      is.rm = TRUE,
      vertex.label.color = "black"
    )
    

    you will see

    enter image description here

    • If you try to shorten all edges
    m <- as.matrix(replace(sample, sample == "", "NA"))
    g <- simplify(graph_from_edgelist(rbind(m[, 1:2], m[, 2:3]), directed = TRUE))
    l <- layout_with_sugiyama(g)
    l$layout[, 2] <- l$layout[, 2] * 0.1
    plot(g,
      layout = -l$layout[, 2:1],
      edge.arrow.size = 0.1,
      vertex.size = 2.5,
      vertex.color = "grey",
      vertex.label.dist = 1,
      edge.arrow.width = 1.5,
      edge.width = seq(0.5, 0.08),
      edge.lty = "solid",
      edge.color = "gray",
      vertex.label.cex = 0.7,
      is.rm = TRUE,
      vertex.label.color = "black"
    )
    

    you will see

    enter image description here