我有一个数据框(df)如下。
V1 V2 V3 V4
A B C D
A G R T
M A R H
我的要求是绘制从 V1 到 V2 到 V3 到 V4 的转换图。当我使用 igraph 命令时
g = graph.data.frame(df)
我看到 V3 和 V4 列被删除了。有没有办法构造这样的转换图。
最佳答案
试试这个:
library(igraph)
df <- as.matrix(df)
# to transform the df to the format that igraph expects, following will suffice for your example
df <- as.data.frame(rbind(df[,1:2], df[,2:3], df[,3:4]))
# if you want to make it generic try the following instead
# df <- as.data.frame(do.call(rbind, lapply(1:(ncol(df)-1), function(i) df[,i:(i+1)])))
g = graph.data.frame(df)
plot(g)
关于r - 对两列以上使用 igraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478736/