循环打包以可视化 R 中的层次结构数据
在本文中,我们将讨论使用循环打包可视化处理分层数据。要使用 R 编程语言准备循环包装,我们将使用ggraph包并准备一个气泡来显示层次结构。
循环打包以可视化 R 中的层次结构数据
准备分层数据
在这里,我们将准备分层数据进行演示。为此,我们将使用耀斑数据集。
R
# 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)
R
vertices = flare$vertices
head(vertices)
R
# preparing the graph
mygraph <- graph_from_data_frame( edges,
vertices = vertices )
mygraph
R
# 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()
输出:
为层次结构创建另一个数据框
R
vertices = flare$vertices
head(vertices)
输出:
使用数据框准备图表:
R
# 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
可视化循环层次结构
在这里,我们将可视化具有层次结构的数据框。
R
# 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()
输出: