📜  用 R 和 ggraph 绘制树状图

📅  最后修改于: 2022-05-13 01:55:04.976000             🧑  作者: Mango

用 R 和 ggraph 绘制树状图

在本文中,我们将了解如何自定义树状图。它也被称为树形图,它是项目之间层次关系的直观表示。树状图的主要目的是找出将对象分配给集群的最佳方法。

创建基本树状图

为了创建树状图,我们将使用 igraph 包来创建和操作图以及分析网络图。因此,为了创建树状图,我们将在分层数据结构中创建一个数据框,然后将其转换为边列表。要绘制树状图,我们将使用以下语法:

在 R 工作控制台中安装和导入 igraph 包的语法:

有一些可选函数可以将边创建为对角线,我们将使用geom_edge_diagonal()方法并将节点添加到图中,我们将使用geom_node_point()方法。

R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram', circular = FALSE) +
  geom_edge_diagonal() +
  geom_node_point() +
  theme_void()


R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# plot
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram', circular = TRUE) +
  geom_edge_diagonal()+
  geom_edge_diagonal() +
  geom_node_point() +
  theme_void()


R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_link()+
  geom_node_point() +
  theme_void()


R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name)) +
  theme_void()


R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name, color="red")) +
  theme_void()


R
# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
 
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name, color="red")) +
  geom_node_point(aes(color ="Blue"))+
theme_void()


输出:

圆形布局树状图

要创建圆形布局,我们将使用 circular = True,它将其绘制到圆形布局中。

R

# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# plot
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram', circular = TRUE) +
  geom_edge_diagonal()+
  geom_edge_diagonal() +
  geom_node_point() +
  theme_void()

输出:

边缘样式树状图

为了制作直边树状图,我们将使用geom_edge_diagonal()方法。

R

# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_link()+
  geom_node_point() +
  theme_void()

输出:

树状图中的标签

要将标签添加到树状图中,我们将使用 geom_node_text() 它将标签添加到图中的层次结构中

R

# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name)) +
  theme_void()

输出:

在树状图中自定义标签

为了在树状图中自定义标签,我们使用 geom_node_text() 它将标签和颜色一起添加到图中的层次结构中

R

# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name, color="red")) +
  theme_void()

输出:

在树状图中自定义节点

为了在树状图中自定义标签,我们使用 geom_node_point() 它将节点和颜色一起添加到图中的层次结构中

R

# libraries
library(ggraph)
library(igraph)
library(tidyverse)
  
# create a data frame
data <- data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
  
# transform it to a edge list!
edges_level1_2 <- data %>% select(
  level1, level2) %>% unique %>% rename(
  from=level1, to=level2)
 
edges_level2_3 <- data %>% select(
  level2, level3) %>% unique %>% rename(
  from=level2, to=level3)
 
edge_list=rbind(edges_level1_2, edges_level2_3)
  
# Now we can plot that
graph_Data <- graph_from_data_frame( edge_list )
ggraph(graph_Data, layout = 'dendrogram') +
  geom_edge_diagonal() +
  geom_node_text(aes( label=name, color="red")) +
  geom_node_point(aes(color ="Blue"))+
theme_void()

输出: