3列CSV、邻接矩阵、网络图、Arcplot

3列CSV、邻接矩阵、网络图、Arcplot,r,graph,social-networking,R,Graph,Social Networking,我正在尝试将包含3列的CSV转换为arcplot。列-A、B、C总是按照A->B->C的顺序排列。然而,我并没有看到一种方法将其实现为arcplot,因为似乎大多数方法都使用了两列边缘图。因此,我一直按照说明转换为邻接矩阵 我将重现下面的问题-但不会生成假数据,因为一个问题是CSV可能无法正确读入 基本上,CSV包含行,其中每个列由分隔,,但可能包含由分隔的多个值例如: 为了在转到arcplots之前完成一些网络图绘制,我尝试以下操作: options(stringsAsFactors = F)

我正在尝试将包含3列的CSV转换为arcplot。列-
A
B
C
总是按照
A
->
B
->
C
的顺序排列。然而,我并没有看到一种方法将其实现为arcplot,因为似乎大多数方法都使用了两列边缘图。因此,我一直按照说明转换为邻接矩阵

我将重现下面的问题-但不会生成假数据,因为一个问题是CSV可能无法正确读入

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

为了在转到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")))
选项(stringsAsFactors=F)

lst不确定这是否是你想要的。但是,您可以尝试:

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)
require(igraph)
df[]您可以使用此代码(实际上它甚至不需要igraph…):

#当然您需要先安装arcdiagram
#如pdf中所述
图书馆(arcdiagram)

DF不确定由“;”分隔的多个值。。。例如,第一行应该是:
ENV->echoic->social+ENV->tact->social+MO->echoic->social+MO->tact->social
对吗?应该是ENV/MO->echoic/tact->social A列有ENV、MO或OVB的一些组合;B柱是触觉、命令、回声或语言内的某种组合;C列为社会、物理或社会/物理或物理/社会。FWIW替换“;”“-”给出了相同的结果fwiw-我刚刚发现了换行符的问题!我使用了dos2unix,并在每个列的值周围添加了引号。我看看这是否有帮助。我不一定要把“;”分开加入值-但看起来很棒。我会尝试一下,然后再给您回复。不知道您是否希望多个值成为图的单个节点。如果是这样的话,从我答案的
df
开始,也许从edgelist(rbind(as.matrix(df)[,-3],as.matrix(df[,-1]))开始的
graph\u就足够了。
生成的图像有点密集-但我会查看igraph选项并尝试调整itHey Nicola这是一个很好的答案。当有赏金的时候,我会给你发一份,谢谢。
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")
# 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)