【问题标题】:Change the representation of the graph更改图形的表示
【发布时间】:2021-08-28 17:12:40
【问题描述】:

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

【问题讨论】:

  • mat_kendall 缺失,但使用在引用 mat_kendall 的行之前定义的 gr4ph 这使用 lgl:plot(gr4ph, layout = layout_with_lgl(gr4ph))

标签: r ggplot2 igraph


【解决方案1】:

您可以使用两种解决方案。布局参数是图形表示的关键: 以下示例使用“大图布局”编码 使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多