安装

BiocManager::install(pkgs = "graph")

4个节点,给定邻接矩阵,即可画出关系图

library(graph)
mat <- matrix(c(
  0, 0, 1, 1,
  0, 0, 1, 1,
  1, 1, 0, 1,
  1, 1, 1, 0
), byrow = TRUE, ncol = 4)
rownames(mat) <- letters[1:4]
colnames(mat) <- letters[1:4]
g1 <- graphAM(adjMat = mat)
plot(g1)

graph-01

randomGraph 生成一个随机图

set.seed(123)
V <- letters[1:10] # 图的定点
M <- 1:4 # 
g2 <- randomGraph(V, M, p = 0.2)
numEdges(g2) # 边的个数
[1] 16
edgeNames(g2) # 边的名字,无向图顶点之间用 ~ 连接
 [1] "a~b" "a~d" "a~e" "a~f" "a~h" "b~f" "b~d" "b~e" "b~h" "c~h" "d~e" "d~f"
[13] "d~h" "e~f" "e~h" "f~h"
plot(g2)

graph-02

换个布局

plot(g2, "neato")

graph-03

plot(g2, "twopi")

graph-04