You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言igraph写入CSV前指定行顺序以保留节点布局的问题问询

我已采纳问题「如何保存带预定义布局的igraph对象边列表」下的回答,使用回答提供的代码如下:

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)

目前我实现了反向操作:从文件读取数据、创建图并绘制,代码如下:

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")

但我需要在绘图前对mylayout矩阵做额外处理以保留节点位置,代码如下:

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)

我发现问题出在merge()函数,它会按字母顺序对行排序:1, 10, 11, 12 等,输出如下:

> 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

但我需要的是其他顺序:

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

问题:是否可以在写入文件前指定行的顺序?我需要将输出文件传给JavaScript做后续处理。

补充:相关讨论可参考「如何按照指定向量的顺序对数据框行排序」

示例g2图、mylayout布局及可运行代码如下:

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)

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 14:39:04