Maptree-层级结构数据展示的绝佳尝试
1
Maptree
边文件格式要求 节点文件即为全部节点即可。
2
通过一个简单的实例我们来进行一个基本了解
# 载入包
library(ggraph)
library(igraph)
library(tidyverse)
# 提取边文件
edges <- flare$edges
# 提取节点文件
vertices <- flare$vertices
# 构建igraph对象
mygraph <- graph_from_data_frame( edges, vertices=vertices )
# 出图
ggraph(mygraph, layout = 'circlepack') +
geom_node_circle() +
theme_void()
3
布局转化
ggraph(mygraph, layout='dendrogram', circular=TRUE) +
geom_edge_diagonal() +
theme_void() +
theme(legend.position="none")
4
类似树图的展示
ggraph(mygraph, layout='dendrogram', circular=FALSE) +
geom_edge_diagonal() +
theme_void() +
theme(legend.position="none")
5
将圈树转化为水平展示
ggraph(mygraph, 'treemap', weight = 'size') +
geom_node_tile(aes(fill = depth), size = 0.25) +
theme_void() +
theme(legend.position="none")
ggraph(mygraph, 'partition', circular = TRUE) +
geom_node_arc_bar(aes(fill = depth), size = 0.25) +
theme_void() +
theme(legend.position="none")
6
使用igeaph网络布局展示
ggraph(mygraph) +
geom_edge_link() +
geom_node_point() +
theme_void() +
theme(legend.position="none")
7
配色和标签设置
# 载入包
library(tidyverse)
library(viridis)
# 同样导入边和节点数据
edges=flare$edges
vertices = flare$vertices
str(edges)
head(edges)
##构造网络对象
mygraph <- graph_from_data_frame( edges, vertices=vertices )
# Hide the first level (right)
ggraph(mygraph, layout = 'circlepack', weight="size") +
geom_node_circle(aes(fill = as.factor(depth), color = as.factor(depth) )) +
scale_fill_manual(values=c("0" = "white", "1" = viridis(4)[1], "2" = viridis(4)[2], "3" = viridis(4)[3], "4"=viridis(4)[4])) +
scale_color_manual( values=c("0" = "white", "1" = "black", "2" = "black", "3" = "black", "4"="black") ) +
theme_void() +
theme(legend.position="FALSE")
# Second one: hide 2 first levels
更改配色我们就很轻易得到了下面的图形:
ggraph(mygraph, layout = 'circlepack', weight="size") +
geom_node_circle(aes(fill = as.factor(depth), color = as.factor(depth) )) +
scale_fill_manual(values=c("0" = "white", "1" = "white", "2" = magma(4)[2], "3" = magma(4)[3], "4"=magma(4)[4])) +
scale_color_manual( values=c("0" = "white", "1" = "white", "2" = "black", "3" = "black", "4"="black") ) +
theme_void() +
theme(legend.position="FALSE")
8
添加标签
# 重现导入边和节点数据,方便重复
edges <- flare$edges %>%
filter(to %in% from) %>%
droplevels()
vertices <- flare$vertices %>%
filter(name %in% c(edges$from, edges$to)) %>%
droplevels()
vertices$size <- runif(nrow(vertices))
# 构造对象
mygraph <- graph_from_data_frame( edges, vertices=vertices )
ggraph(mygraph, layout = 'circlepack', weight="size" ) +
geom_node_circle(aes(fill = depth)) +
geom_node_text( aes(label=shortName, filter=leaf, fill=depth, size=size)) +
theme_void() +
theme(legend.position="FALSE") +
scale_fill_viridis()