我有一个标准data.Frame,其中包含一些分类列和一个数字列。它代表一个嵌套的实验设计(但并不重要),如下所示:
Zip.zip
我想用此代码(左图!)进行相同的图: https://www.r-graph-gallery.com/314-custom-circle-packing-with-several-levels/
我不知道如何将数据框转换为“ igraph”数据。框并继续执行建议绘制的代码(我没有 from 和 to < / em>列)。给定我的示例数据,我想要的输出看起来像右边的图(圆圈大小代表set.seed(1234)
data = data.frame(toplevel=c("A","A","A","A", "B", "B", "B"),
second = c("A1", "A1", "A2", "A2", "B1", "B1", "B2"),
experiments = paste0("exp_00", 1:7),
values = runif(7, 1,100))
#### toplevel second experiments values
#### 1 A A1 exp_001 12.25664
#### 2 A A1 exp_002 62.60764
#### 3 A A2 exp_003 61.31820
#### 4 A A2 exp_004 62.71456
#### 5 B B1 exp_005 86.23062
#### ...
列)。我尝试使用values
谢谢 编辑:到目前为止,我尝试过的代码(我只有部分图形..?)
graph_from_data_frame
答案 0 :(得分:1)
我认为您当前的尝试肯定是正确的。一个可能有用的技巧是添加一个额外的“根”节点,两个顶级节点都连接到该节点。目前,由于您的顶级节点之间根本没有任何连接,因此ggraph只绘制其中的一组:
edges = rbind.data.frame(
data[,1:2] %>% setNames(c("from", "to")),
data[, 2:3] %>% setNames(c("from", "to")),
data.frame(from = c("root", "root"), to = c("A", "B")))
vertices = bind_rows(
data.frame(name = "root", values = sum(data$values)),
data %>% group_by(toplevel) %>% summarize(values=sum(values)) %>% select(name=toplevel, values),
data %>% group_by(second) %>% summarize(values=sum(values)) %>% select(name=second, values),
data %>% select(name=experiments, values),
)
mygraph=graph_from_data_frame(edges, directed = TRUE, vertices = vertices)
ggraph(mygraph, layout = 'circlepack',weight="values") +
geom_node_circle(aes(fill = depth)) +
geom_node_label(aes(label = name)) +
theme_void()