Search code examples
rdataframematrixlayoutigraph

How to define the order of rows before writing to file?


I have accepted the answer on my question how-to-save-the-edge-list-of-igraph-object-with-the-predefined-layout. I am using the proposed code in answer:

edges <- as_edgelist(g2)
idx1 <- match(edges[, 1], V(g2)$name)
idx2 <- match(edges[, 2], V(g2)$name)
result <- cbind(data.frame(edges),
        mylayout[idx1, ],
        mylayout[idx2, ]
  )
names(result) <- c("V1", "V2", "V1_x", "V1_y", "V2_x", "V2_y")
write.csv(result, file = "test.csv", row.names = FALSE)

Currently, I have implemented the opposite action: reading data from a file, create the graph and plotting it.

result <- as.data.frame(read.csv(file = "test.csv"), sep = ",", header = TRUE))
g <- graph_from_data_frame(result[1:2], directed = FALSE) 

from <- unique(result[c("V1", "V1_x", "V1_y")])
to   <- unique(result[c("V2", "V2_x", "V2_y")])

names(from) <- c("V", "Vx", "Vy")
names(to)   <- c("V", "Vx", "Vy")

However, I have to do additional processing with the mylayout matrix before graph plotting, because I need to keep the node's positions:

tmp       <- merge(from, to, all = TRUE)
VV        <- unlist(tmp[1])
newlayout <- as.matrix(tmp[2:3], ncol = 2)
newlayout <- newlayout[match(names(V(g)), VV), ] 
plot(g, layout = newlayout)

I think a problem in the merge() function, because it sorts rows alphabetically: 1, 10, 11, 12 and so on:

> merge(from, to, all = TRUE)
    V       Vx        Vy
1   1 146.6092 -186.2981
2  10 135.5057 -209.2721
3  11 177.4670 -209.2720
4  12 148.0673 -178.0015
5   2 140.3174 -180.5523
6   3 145.9292 -210.6084
7   4 167.5780 -210.3411
8   5 166.7762 -185.4851
9   6 137.6438 -218.8938
10  7 172.1216 -219.6955
11  8 175.3288 -180.6742
12  9 175.0615 -188.1577

but I heed the different order

> match(names(V(g)), VV)
 [1]  1  6  7  8  5  9 10 11 12  2  3  4

Question. Is it possible to specificate the order of rows before writing to file? The main problem is that I should to pass the file for further processing on JavaScript.

Edit. Here is the disscusion order-data-frame-rows-according-to-vector-with-specific-order

The example g2 graph with mylayout layout and working code are below.

g2 <- upgrade_graph(structure(list(12, FALSE, c(1, 4, 5, 6, 7, 8, 9, 10, 11, 3, 2), c(0, 0, 1, 2, 3, 3, 1, 2, 0, 0, 1), c(
  0, 10, 9, 1, 2, 3, 4,
  5, 6, 7, 8
), c(0, 9, 1, 8, 10, 2, 6, 3, 7, 4, 5), c(
  0, 0, 1,
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
), c(
  0, 4, 7, 9, 11, 11, 11, 11,
  11, 11, 11, 11, 11
), list(
  c(1, 0, 1), structure(list(), .Names = character(0)),
  list(name = c(
    "1", "3", "4", "5", "2", "6", "7", "8", "9",
    "10", "11", "12"
  )), list()
)), class = "igraph"))

mylayout <-
structure(c(146.60916, 145.92917, 167.57799, 166.77618, 140.31737, 
137.64381, 172.12157, 175.32881, 175.06154, 135.50566, 177.46696, 
148.06731, -186.29814, -210.6084, -210.34111, -185.48505, -180.55231, 
-218.89375, -219.69554, -180.67421, -188.15775, -209.27205, -209.27203, 
-178.00151), .Dim = c(12L, 2L), .Dimnames = list(c("1", "3", 
"4", "5", "2", "6", "7", "8", "9", "10", "11", "12"), NULL))

plot(g2, layout = mylayout)

########################################################################################
edges <- as_edgelist(g2)
idx1 <- match(edges[,1], V(g2)$name)
idx2 <- match(edges[,2], V(g2)$name)
result <- cbind(data.frame(edges),
        mylayout[idx1, ],
        mylayout[idx2, ]
  )
names(result) <- c("V1", "V2", "V1_x", "V1_y", "V2_x","V2_y")
########################################################################################
## uncomment two lines if you want to use file test.csv
## write.csv(result, file = "test.csv", row.names = FALSE)
## result = as.data.frame(read.csv(file = "test.csv"), sep=",", header = TRUE))
########################################################################################

g <- graph_from_data_frame(result[1:2], directed=FALSE) 

from <- unique(result[c("V1","V1_x","V1_y")])
to   <- unique(result[c("V2","V2_x","V2_y")])

names(from) <- c("V", "Vx", "Vy")
names(to)   <- c("V", "Vx", "Vy")

newlayout <- matrix() 
tmp       <- merge(from, to, all=TRUE)
VV        <- unlist(tmp[1])
newlayout <- as.matrix(tmp[2:3], ncol=2)
newlayout <- newlayout[match(names(V(g)), VV),] 
plot(g, layout = newlayout)

Solution

  • I don't think you need merge to produce tmp. You can use unique + rbind over from and to to generate newlayout:

    newlayout <- as.matrix(unique(rbind(from, to))[-1])
    plot(g, layout = newlayout)
    

    enter image description here