有权重的最小生成树
Minimum spanning trees with weight
起初我想为我的研究制作一棵最大生成树,但我发现考虑到我在 R 中的时间和知识,它会太复杂。
所以我的选择是通过反转数据的顺序来创建最小生成树。
事实上,处理从 1 到 3 的序数数据时,我只需要颠倒数据库中的顺序即可。
我在这里找到了对我有很大帮助的语法:https://web.stanford.edu/class/bios221/book/Chap-Graphs.html#minimum-spanning-trees
我做了必要的调整以获得图表。但是我不能在这张图上加权重...
这是我想要达到的目标:获得最小生成树,每个点之间的权重没有节点标题重叠,并且节点在图中正确拟合。
以下是我不能做和不明白的事情:
排斥命令不起作用,我不明白为什么。 R 告诉我这个命令是未知的。我插入:geom_node_text(aes(label = name), repel = TRUE。它不会使命令出现问题。我真的不明白为什么
权重:我试过:graph.adjacency(mstree, mode = "undirected",weight=TRUE) 显然我把权重设置为 true 或 false 它不会改变任何东西。再一次,我一定是错过了什么
我试着举了一个尽可能接近我的问题的例子。 (这是我第一次发布示例,希望我做对了):
library(dplyr)
library(igraph)
library(ape)
library(tidyr)
library(ggplot2)
library(ggnetwork)
library(ggraph)
M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21th")
M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))
mstree <- ape::mst(M2)
gr4ph <- graph.adjacency(mstree, mode = "undirected",weight=TRUE)
gg <- ggnetwork(gr4ph, arrow.gap = 0, layout = layout_with_fr(gr4ph))
ggplot(gg, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "black", alpha = 1, curvature = 0.1) +
geom_nodes(aes(color = name), size = 6) + theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3.5) +
theme(plot.margin = unit(c(0, 1, 2, 4), "cm"))+
guides(color = guide_legend(keyheight = 0.09, keywidth = 0.09,
title = "Mots")) + theme(legend.position = c(-0.05, 0.14),
legend.background = element_blank(),
legend.text = element_text(size = 7))
感谢您的帮助
igraph
doc page 有一个名为 weighted
的参数,它有效。
# weighted
n1 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = TRUE)
E(n1)$weight # 1 1 1 1 etc.
# unweighted
n2 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = NULL)
E(n2)$weight # NULL
起初我想为我的研究制作一棵最大生成树,但我发现考虑到我在 R 中的时间和知识,它会太复杂。
所以我的选择是通过反转数据的顺序来创建最小生成树。 事实上,处理从 1 到 3 的序数数据时,我只需要颠倒数据库中的顺序即可。
我在这里找到了对我有很大帮助的语法:https://web.stanford.edu/class/bios221/book/Chap-Graphs.html#minimum-spanning-trees 我做了必要的调整以获得图表。但是我不能在这张图上加权重...
这是我想要达到的目标:获得最小生成树,每个点之间的权重没有节点标题重叠,并且节点在图中正确拟合。
以下是我不能做和不明白的事情:
排斥命令不起作用,我不明白为什么。 R 告诉我这个命令是未知的。我插入:geom_node_text(aes(label = name), repel = TRUE。它不会使命令出现问题。我真的不明白为什么
权重:我试过:graph.adjacency(mstree, mode = "undirected",weight=TRUE) 显然我把权重设置为 true 或 false 它不会改变任何东西。再一次,我一定是错过了什么
我试着举了一个尽可能接近我的问题的例子。 (这是我第一次发布示例,希望我做对了):
library(dplyr)
library(igraph)
library(ape)
library(tidyr)
library(ggplot2)
library(ggnetwork)
library(ggraph)
M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21th")
M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))
mstree <- ape::mst(M2)
gr4ph <- graph.adjacency(mstree, mode = "undirected",weight=TRUE)
gg <- ggnetwork(gr4ph, arrow.gap = 0, layout = layout_with_fr(gr4ph))
ggplot(gg, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "black", alpha = 1, curvature = 0.1) +
geom_nodes(aes(color = name), size = 6) + theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3.5) +
theme(plot.margin = unit(c(0, 1, 2, 4), "cm"))+
guides(color = guide_legend(keyheight = 0.09, keywidth = 0.09,
title = "Mots")) + theme(legend.position = c(-0.05, 0.14),
legend.background = element_blank(),
legend.text = element_text(size = 7))
感谢您的帮助
igraph
doc page 有一个名为 weighted
的参数,它有效。
# weighted
n1 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = TRUE)
E(n1)$weight # 1 1 1 1 etc.
# unweighted
n2 <- igraph::graph.adjacency(mstree, mode = "undirected", weighted = NULL)
E(n2)$weight # NULL