3 列 CSV,到邻接矩阵,到网络图,到 Arcplot

3 column CSV, to adjacency matrix, to networkgraph, to Arcplot

我正在尝试将具有 3 列的 CSV 转换为 arcplot。列 - ABC 始终按 A -> B -> C 的顺序排列。但是,我没有看到将其实现为弧线图的方法,因为它似乎大多数方法都使用两列边缘图。因此,我一直按照说明 here 转换为邻接矩阵。

我将在下面重新创建问题 - 但不会生成虚假数据,因为一个问题是 CSV 可能无法正确读取。

基本上,CSV 包含行,其中每列由 , 分隔,但可能包含多个由 ; 分隔的值,例如:

ENV;MO,echoic;tact,social 
ENV;MO,mand,physical
OVB,intraverbal,social
ENV;OVB,tact,social
OVB,intraverbal;tact,social
OVB;ENV;MO,intraverbal;mand,social
OVB;ENV;MO,intraverbal;mand,physical;social
ENV;MO,mand,social;physical

我正在尝试以下操作,以便在转向 arcplots 之前完成一些网络绘图:

options(stringsAsFactors = F)
lst <- read.csv("abc.csv", header=FALSE)

#this is pretty much straight from the link above
d <- do.call(rbind, lst)
edges <- rbind(d[ ,1:2], d[ ,2:3])
g <- graph.data.frame(edges, directed=TRUE)
adj <- as.matrix(get.adjacency(g)) 
g2 <- new("graphAM", adjMat=adj, edgemode="directed")
plot(g2, attrs = list(graph = list(rankdir="LR"), node = list(fillcolor = "lightblue")))

结果完全不是我所希望的。而不是列 A 中指向 B 的元素指向 C。相反,它只是 A 指向自身的一个元素;一个从 B 指向另一个指向另一个,例如 intraverbal -> mand -> intraverbaltact,一个来自 C 指向它自己,另一个值来自 C.

附录:给定 A -> B -> C 格式,一行

OVB;ENV;MO,intraverbal;mand,social

表示

A(OVB&ENV&MO) -> B(intraverbal&mand) -> C(social)

虽然它可能超出了问题的范围,但最终目标将是类似于此处描述的弧线图 PDF guide to arcplots in R

不确定这是否是您想要的。不过你可以试试:

require(igraph)
df[]<-lapply(df,strsplit,";")
el<-as.matrix(do.call(rbind,apply(df,1,expand.grid)))
g<-graph_from_edgelist(rbind(el[,-3],el[,-1]))
plot(g)

数据

df<-structure(list(V1 = c("ENV;MO", "ENV;MO", "OVB", "ENV;OVB", "OVB", 
"OVB;ENV;MO", "OVB;ENV;MO", "ENV;MO"), V2 = c("echoic;tact", 
"mand", "intraverbal", "tact", "intraverbal;tact", "intraverbal;mand", 
"intraverbal;mand", "mand"), V3 = c("social", "physical", "social", 
"social", "social", "social", "physical;social", "social;physical"
)), .Names = c("V1", "V2", "V3"), row.names = c(NA, -8L), class = "data.frame")

您可以使用此代码(实际上它甚至不需要 igraph...):

# of course you need to install arcdiagram first
# as described in the pdf
library(arcdiagram) 

DF <- read.table(text=
"ENV;MO,echoic;tact,social 
ENV;MO,mand,physical
OVB,intraverbal,social
ENV;OVB,tact,social
OVB,intraverbal;tact,social
OVB;ENV;MO,intraverbal;mand,social
OVB;ENV;MO,intraverbal;mand,physical;social
ENV;MO,mand,social;physical",sep=',')

# replace ";" with "&\n"
DF[] <- lapply(DF,function(x)gsub(';',' &\n',x))

# create adjacency matrix
m <- rbind(as.matrix(DF[,1:2]),as.matrix(DF[,2:3]))

# plot...
arcplot(m ,col.arcs='DodgerBlue',lwd.arcs=2,col.labels='black',las=2)