r-更改图形的表示形式



我想在图形方面得到帮助。我希望能够更改图形表示算法(LGL或其他(,但我做不到。我该怎么做?尝试了几个不起作用的选项。。。

library(NetworkToolbox)
library(dplyr)
library(igraph)
library(ggplot2)
library(ggnetwork)
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))
gr4ph <-  graph.adjacency(M2, mode = "undirected",weight=TRUE)
MAST <- MaST(M2, normal = False)
gr4ph <-  graph.adjacency(MAST , mode = "lower",weight=TRUE) 
ggplot(gr4ph, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey", alpha = 1) +
geom_nodes(aes(color = name)) +  theme_blank() +
geom_nodetext(aes(label = name), color = "black") +
geom_edgetext(aes(label = weight))+
theme(legend.position = "none")

您可以使用两个解决方案。布局参数是图形表示的关键:下面的示例用";大图形布局";带ggplot:

ggplot(gr4ph1, aes(x = x, y = y, xend = xend, yend = yend),layout = layout_with_lgl(gr4ph1))+
geom_edges(color = "grey", alpha = 1,size=1.5,) +
geom_nodes(aes(color = name), size = 3) +  theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3) +
geom_edgetext(aes(label = weight), size = 3,label.padding=unit(0.01, "lines"))+
theme(legend.position = "none")

带ggraph:

ggraph(gr4ph, layout = "lgl") +
geom_edge_link(aes(label = weight), angle_calc = 'along',label_dodge = unit(2.5, 'mm')) +
geom_node_point(aes(size=3,color= name))  +
theme(legend.position = "none")+
geom_node_text(aes(label = name), repel = TRUE)

最新更新