如何在R中的ggraph库中使用圆包布局 [英] how to use circle pack layout in ggraph library in r

查看:63
本文介绍了如何在R中的ggraph库中使用圆包布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建ggraph circlepack布局需要什么数据格式?似乎需要层次结构.

what data format is necessary to create ggraph circlepack layout? It seems to require a hierarchy.

我通常尝试了顶点和节点,但显然不起作用.

I tried normally vertices and nodes, which apparently doesn't work.

library(ggraph)
library(igraph)
edges=data.frame(from=c('a','b','c'), to= c('c','a','d'))
vertices=data.frame(nodes=c('a','b','c','d'), weight=c(1,2,3,4))
graph <- graph_from_data_frame(edges, vertices = vertices)
ggraph(graph, 'circlepack', weight = 'size') + 
geom_node_circle(size = 0.25, n = 50) + 
coord_fixed()

然后我尝试了一个也不起作用的树状图对象.如果要在实心圆中显示几个带有子项目的组,应如何构建图形对象?

I then tried a dendrogram object which doesn't work either. If i want to show several groups with sub-items in packed circle, how shall i build the graph object?

数据框更像这样

df <- data.frame(group=c("a","a","b","b","b"),    subitem=c("x","y","z,"u","v"), size=c(6,2,3,2,5))

推荐答案

circlepack布局对具有一个根且没有循环的分层/树状结构进行建模.要将您的 df 建模为circlepack布局,您必须考虑 group 列中的 a b 都是根源.如果我们向df添加根,并且 a b 都是该根的子代,我们可以将其可视化为 circlepack :

A circlepack layout models a hierarchical/tree-like structure with one root and no cycles. To model your df as a circlepack layout, you have to consider that a and b in the group column are both roots. If we add a root to the df, and have both a and b be children of that root, we can visualize it as a circlepack:


library(ggraph)
library(igraph)
library(dplyr)


df <- data.frame(group=c("root", "root", "a","a","b","b","b"),    
                 subitem=c("a", "b", "x","y","z","u","v"), 
                 size=c(0, 0, 6,2,3,2,5))

# create a dataframe with the vertices' attributes

vertices <- df %>% 
  distinct(subitem, size) %>% 
  add_row(subitem = "root", size = 0)

graph <- graph_from_data_frame(df, vertices = vertices)

ggraph(graph, layout = "circlepack", weight = 'size') + 
  geom_node_circle(aes(fill =depth)) +
# adding geom_text to see which circle is which node 
  geom_text(aes(x = x, y = y, label = paste(name, "size=", size))) +
  coord_fixed()

这篇关于如何在R中的ggraph库中使用圆包布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆