2

第一个图像是手绘图像,第二个图像是使用 ggraph 绘制的相同图形。 在此处输入图像描述

这是生成图表的代码。

library(igraph)
library(tidyverse)
library(ggraph)

V <- read.table(text = "x        y
                2 1
                4 2
                4 4
                2 5
                6 4
                3 7
                8 6", 
                header = T) %>%
  rownames_to_column("name")

E <- matrix(c(0,    1,    0,    0,    0, 0, 0,
              0,    0,    1,    0,    0, 0, 0,
              0,    0,    0,    1,    1, 0, 0,
              0,    0,    0,    0,    0, 1, 0,
              0,    0,    0,    0,    0, 0, 1,
              0,    0,    0,    1,    0, 0, 0,
              0,    0,    0,    0,    1, 0, 0), nrow = 7, byrow = T) %>%
  data.frame() %>% 
  rename_all(list(function(x) 1:7)) %>% 
  rownames_to_column(var = "from") %>% 
  gather(to, val, 2:6) %>% 
  filter(val == 1) %>%
  select(from, to)

nodeLables <- c(" I ", " N0", "F", "N1", "N2", "F1","F2")

g <- graph_from_data_frame(E, vertices = V, directed = F)


ggraph(g) + 
  geom_edge_link(edge_width = 1.3) + 
  geom_node_label(aes(label = nodeLables),label.r = unit(0.75, "lines"), 
                  label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
  ggtitle("My plot") +
  coord_flip() +
  expand_limits(x = 0, y = 0) +

  # Using scale_x_reverse and swapping the limits
  scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) + 
  # switching y position to "right" (pre-flip)
  scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
  theme_minimal() 

在第二张图中,节点不是完美的圆,曲率随着标签的大小而变化。我想让节点成为完美的圆圈并为不同类型的节点分配颜色。例如

I - Blue
F - Red
Nx - Green
Fx - Orange

我玩过 geom_node_point 和 geom_node_text 并得到了以下结果。但是,我无法进一步增加节点大小。如何增加节点大小?

nodeColours <- c("blue", "green", "red","green", "green", "orange","orange")

ggraph(g) + 
  geom_edge_link(edge_width = 1.3) + 
  geom_node_point(aes(size = 4), color = nodeColours)+
  geom_node_text(aes(label = nodeLables))+
  ggtitle("My plot") +
  coord_flip() +
  expand_limits(x = 0, y = 0) +
  scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) + 
  scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
  theme_minimal() 

在此处输入图像描述

4

0 回答 0