1

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

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

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()

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

数据框更像这样

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

1 回答 1

1

circlepack 布局对具有一个根且没有循环的分层/树状结构进行建模。要将您的模型建模df为 circlepack 布局,您必须考虑abgroup列中都是根。如果我们在 df 中添加一个根,并且两者都有a并且b是该根的孩子,我们可以将其可视化为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()

于 2017-05-02T16:05:53.460 回答