如何使用圆包布局GGRAPH库中的R(how to use circle pack layout i

2019-09-27 05:24发布

什么样的数据格式是必要创建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))

Answer 1:

甲circlepack布局模型的分层/树状结构具有一个根和没有循环。 到您的模型df作为circlepack布局,你要考虑的是abgroup列都是根。 如果再加根的DF,并同时拥有ab是根的孩子,我们可以想像它作为一个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()



文章来源: how to use circle pack layout in ggraph library in r
标签: r ggplot2 ggraph