我正在尝试绘制网络可视化以类似于流程图.我对以下代码非常接近,但我有几个问题:
非常感谢!!
library("igraph")
L3 <- LETTERS[1:8]
d <- data.frame(start = sample(L3, 16, replace = T), end = sample(L3, 16, replace = T),
weight = c(20,40,20,30,50,60,20,30,20,40,20,30,50,60,20,30))
g <- graph.data.frame(d, directed = T)
V(g)$name
E(g)$weight
ideg <- degree(g, mode = "in", loops = F)
col=rainbow(12) # For edge colors
plot.igraph(g,
vertex.label = V(g)$name, vertex.label.color = "gray20",
vertex.size = ideg*25 + 40, vertex.size2 = 30,
vertex.color = "gray90", vertex.frame.color = "gray20",
vertex.shape = "rectangle",
edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
edge.curved = T,
layout = layout.reingold.tilford)
Run Code Online (Sandbox Code Playgroud)
Sac*_*amp 38
igraph中的布局定义在一个矩阵中,每个节点有2列和一行.第一列表示其x位置,第二列表示其y位置,并且比例不相关(它总是重新调整以适合-1到1的绘图区域.您可以在绘图之前通过调用图形上的布局函数来获得此布局:
l <-layout.reingold.tilford(g)
l
[,1] [,2]
[1,] 0 0
[2,] -1 3
[3,] 0 1
[4,] 0 3
[5,] 0 2
[6,] 0 4
[7,] 1 3
Run Code Online (Sandbox Code Playgroud)
这样您可以手动以任何方式更改它,然后将其发送到绘图:
plot.igraph(g,
vertex.label = V(g)$name, vertex.label.color = "gray20",
vertex.size = ideg*25 + 40, vertex.size2 = 30,
vertex.color = "gray90", vertex.frame.color = "gray20",
vertex.shape = "rectangle",
edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
edge.curved = T,
layout = l)
Run Code Online (Sandbox Code Playgroud)
您似乎也可以设置参数params来控制布局abit.这是一个包含一个参数的列表,该参数root显然可用于设置图的根.为此节点分配一些节点(重新记录,igraph使用C类似于节点的索引,第一个节点为0).因此将根设置为"C":
l <- layout.reingold.tilford(g,params=list(root=2))
Run Code Online (Sandbox Code Playgroud)
编辑:还有RGraphViz一些很好的树形布局,可能值得一试.
编辑2:
这是我的包中源代码的修改片段,它使用相同类型的布局矩阵来定义图中节点的位置,您可能会发现它很有用:
gridLayout <- function(x)
{
LmatX <- seq(-1,1,length=ncol(x))
LmatY <- seq(1,-1,length=nrow(x))
loc <- t(sapply(1:max(x),function(y)which(x==y,arr.ind=T)))
layout <- cbind(LmatX[loc[,2]],LmatY[loc[,1]])
return(layout)
}
Run Code Online (Sandbox Code Playgroud)
此函数的作用是将指定网格中布局的矩阵(类似于layout())转换为具有x和y位置的两列布局.定义一个零的矩阵,并为每个节点整数从1到节点的总数(这是igraph ID + 1).
例如,对于一个愚蠢的4节点图:
grid <- matrix(c(
0,0,1,0,0,
2,0,3,0,4),nrow=2,byrow=TRUE)
library("igraph")
g <- graph.adjacency(matrix(1,4,4))
plot(g,layout=gridLayout(L))
Run Code Online (Sandbox Code Playgroud)
如果您想自己分配节点位置,则比上述方法更简单的方法是在数据表中添加标记为x和y的列,并在这些列中为相应节点添加x和y坐标.例如
library('igraph')
nodes <- c('a','b','c','d')
x <- c(0,1,2,3)
y <- c(0,1,2,3)
from <- c('a','b','c')
to <- c('b','c','d')
NodeList <- data.frame(nodes, x ,y)
EdgeList <- data.frame(from, to)
a<- graph_from_data_frame(vertices = NodeList, d= EdgeList, directed = FALSE)
plot(a)
Run Code Online (Sandbox Code Playgroud)