我有一个数据帧(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被删除了。有没有一种方法可以构造这样的过渡图。
发布于 2017-01-05 06:57:14
试试这个:
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)
发布于 2017-01-05 07:05:19
我们可以通过编程的方式在list
中成对设置列的子集,使用rbindlist
(来自data.table
)绑定dataset,转换为graph
data.frame和plot
library(data.table)
library(igraph)
plot(graph.data.frame(rbindlist(lapply(seq(ncol(df)-1), function(i) df[i:(i+1)]))))
https://stackoverflow.com/questions/41478736
复制相似问题