更改图形的表示

问题描述

我想帮助我的图表。我希望能够更改图形表示算法(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)