R语言 用圆形包装来实现层次数据的可视化
在这篇文章中,我们谈论的是使用圆形包装可视化处理分层数据。为了用R编程语言准备圆形包装,我们将使用 ggraph 包并准备一个气泡来显示层次结构。
在R语言中用圆形包装来显示层次结构的数据
准备分层数据
在这里,我们将准备层次数据进行演示。为此,我们将使用flare数据集。
# Libraries
library(ggraph) # to prepare visualisation
library(igraph) # for network analysis
library(tidyverse) # for data handling
library(viridis) # for generating the color map
# data for hierarchical structure
edges = flare$edges
head(edges)
输出
创建另一个分层结构的数据框架
vertices = flare$vertices
head(vertices)
输出
准备带数据框架的图表
# preparing the graph
mygraph <- graph_from_data_frame( edges,
vertices = vertices )
mygraph
输出
IGRAPH 6e05b59 DN-- 252 251 --
+ attr: name (v/c), size (v/n), shortName (v/c)
+ edges from 6e05b59 (vertex names):
[1] flare.analytics.cluster->flare.analytics.cluster.AgglomerativeCluster
[2] flare.analytics.cluster->flare.analytics.cluster.CommunityStructure
[3] flare.analytics.cluster->flare.analytics.cluster.HierarchicalCluster
[4] flare.analytics.cluster->flare.analytics.cluster.MergeEdge
[5] flare.analytics.graph ->flare.analytics.graph.BetweennessCentrality
[6] flare.analytics.graph ->flare.analytics.graph.LinkDistance
[7] flare.analytics.graph ->flare.analytics.graph.MaxFlowMinCut
[8] flare.analytics.graph ->flare.analytics.graph.ShortestPaths
+ ... omitted several edges
可视化圆形层次结构
在这里,我们将以分层结构的方式对数据框架进行可视化。
# plot the graph using ggraph
ggraph(mygraph, # graph data
layout = 'circlepack',
# size of bubbles based on
# the size parameter in vertices data
weight = size) +
geom_node_circle(aes(fill = as.factor(depth),
color = as.factor(depth) )) +
# define the color of each different labels
scale_color_manual( values=c("0" = "green", "1" = "red",
"2" = "red",
"3" = "red", "4"="red") ) +
scale_fill_manual(values = c("0" = "green", "1" = viridis(4)[1],
"2" = viridis(4)[2], "3" = viridis(4)[3],
"4" = viridis(4)[4])) +
theme_void()
输出