使用基于属性的布局从数据框创建图形
Create a graph from data frame with a layout base on attribute
我根据数据框创建图表。我希望根据 data.rw$Hamming.
中包含的汉明值排列和移动顶点
我需要一些帮助
data.rw <- structure(list(g1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
11, 11, 12), g2 = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 6, 7, 8, 9, 10, 11, 12, 13,
7, 8, 9, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 11, 12,
13, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13), Hamming = c(116,
86, 101, 92, 84, 78, 83, 102, 87, 100, 96, 97, 90, 111, 98, 90,
92, 87, 114, 95, 108, 104, 109, 85, 74, 68, 60, 67, 84, 71, 84,
78, 79, 83, 85, 79, 78, 101, 90, 101, 91, 92, 72, 66, 67, 92,
77, 90, 82, 83, 62, 59, 88, 71, 86, 78, 81, 59, 78, 63, 74, 68,
73, 83, 60, 77, 75, 72, 89, 100, 94, 97, 79, 75, 82, 90, 93,
91)), row.names = c(NA, -78L), class = "data.frame")
set.seed(1234)
vertice.df <- unique(c(data.rw$name1,data.rw$name2))
g <- graph_from_data_frame(d = data.rw, vertices = vertice.df, directed = F)
plot(g)
我为这项任务推荐 distance-based 布局,想到多维缩放:
m <- get.adjacency(g, attr = "Hamming", sparse = F)
# optionally: m <- dist(m)
l <- layout_with_mds(g, dist = m, dim = 2)
首先从图中提取加权邻接矩阵并将其输入布局函数(dist = m
)。 returns 一个二维矩阵 l
(dim = 2
),您可以将其用作节点位置的布局。
plot(g, layout = l)
如果您对 MDS 感兴趣,请查看 ?cmdscale
,特别是 eig
参数以稍后评估 goodness-of-fit。很可能二维不足以充分反映节点之间的距离。但这由你决定。
我根据数据框创建图表。我希望根据 data.rw$Hamming.
我需要一些帮助
data.rw <- structure(list(g1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
11, 11, 12), g2 = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 6, 7, 8, 9, 10, 11, 12, 13,
7, 8, 9, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 11, 12,
13, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13), Hamming = c(116,
86, 101, 92, 84, 78, 83, 102, 87, 100, 96, 97, 90, 111, 98, 90,
92, 87, 114, 95, 108, 104, 109, 85, 74, 68, 60, 67, 84, 71, 84,
78, 79, 83, 85, 79, 78, 101, 90, 101, 91, 92, 72, 66, 67, 92,
77, 90, 82, 83, 62, 59, 88, 71, 86, 78, 81, 59, 78, 63, 74, 68,
73, 83, 60, 77, 75, 72, 89, 100, 94, 97, 79, 75, 82, 90, 93,
91)), row.names = c(NA, -78L), class = "data.frame")
set.seed(1234)
vertice.df <- unique(c(data.rw$name1,data.rw$name2))
g <- graph_from_data_frame(d = data.rw, vertices = vertice.df, directed = F)
plot(g)
我为这项任务推荐 distance-based 布局,想到多维缩放:
m <- get.adjacency(g, attr = "Hamming", sparse = F)
# optionally: m <- dist(m)
l <- layout_with_mds(g, dist = m, dim = 2)
首先从图中提取加权邻接矩阵并将其输入布局函数(dist = m
)。 returns 一个二维矩阵 l
(dim = 2
),您可以将其用作节点位置的布局。
plot(g, layout = l)
如果您对 MDS 感兴趣,请查看 ?cmdscale
,特别是 eig
参数以稍后评估 goodness-of-fit。很可能二维不足以充分反映节点之间的距离。但这由你决定。