gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:41:15 24 4
gpt4 key购买 nike

我正在尝试将具有 3 列的 CSV 转换为弧图。列 - A , B , C总是按顺序走 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


我正在尝试以下操作,以便在移动到弧线图之前完成一些网络绘图:
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 -> intraverbal ; tact ,还有一个来自 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)

enter image description here

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

关于r - 3 列 CSV,到邻接矩阵,到网络图,到 Arcplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com