在R中绘制一个家谱

问题描述 投票:1回答:1
d = data.frame(
   offspring = c("G2I1", "G2I2", "G2I3", "G3I1", "G3I2", "G3I3", "G3I4", "G4I1", "G4I2", "G4I3", "G4I4", "G5I1", "G5I2", "G5I3"  ),
   parent1   = c("G1I1", "G1I2", "G1I1", "G2I1", "G2I3", "G2I1", "G2I3", "G3I2", "G3I2", "G3I1", "G3I4", "G4I3", "G4I3", "G4I1" ),
   parent2   = c("G1I3", "G1I2", "G1I2", "G2I2", "G2I2", "G2I2", "G2I3", "G3I4", "G3I1", "G3I2", "G3I4", "G4I1", "G4I1", "G4I2" )
)

print(d)

        offspring parent1 parent2
    1       G2I1    G1I1    G1I3  # generation 2
    2       G2I2    G1I2    G1I2  # generation 2
    3       G2I3    G1I1    G1I2  # generation 2
    4       G3I1    G2I1    G2I2  # generation 3
    5       G3I2    G2I3    G2I2  # generation 3
    6       G3I3    G2I1    G2I2  # generation 3
    7       G3I4    G2I3    G2I3  # generation 3
    8       G4I1    G3I2    G3I4  # generation 4
    9       G4I2    G3I2    G3I1  # generation 4
    10      G4I3    G3I1    G3I2  # generation 4
    11      G4I4    G3I4    G3I4  # generation 4
    12      G5I1    G4I3    G4I1  # generation 5
    13      G5I2    G4I3    G4I1  # generation 5
    14      G5I3    G4I1    G4I2  # generation 5

数据表示

这些数据代表了一个家谱。每行表示一个后代及其两个父母。我称他们为parent1parent2,因为他们是雌雄同体。此外,他们可以克隆自己!世代不重叠,意味着一代n的后代的所有父母都出生在n-1一代。

让我们考虑一个例子。个人G3I4出生在第3代(G3),是这一代的个人索引4(I4;索引只是一个ID)。这个人是个人G4I1和个人G4I4的父母。事实上,G3I4G4I4的唯一父母,因为她克隆了自己。

我怎样才能在R中绘制这个家谱?

相关文章

How to plot family tree in R非常相关,但我没有将它应用于我的数据。第一个问题使用igraph,我不是很熟悉。但我没有看到任何好看的东西

d = tibble(
       offspring = c("G2I1", "G2I2", "G2I3", "G3I1", "G3I2", "G3I3", "G3I4", "G4I1", "G4I2", "G4I3", "G4I4", "G5I1", "G5I2", "G5I3"  ),
       parent1   = c("G1I1", "G1I2", "G1I1", "G2I1", "G2I3", "G2I1", "G2I3", "G3I2", "G3I2", "G3I1", "G3I4", "G4I3", "G4I3", "G4I1" ),
       parent2   = c("G1I3", "G1I2", "G1I2", "G2I2", "G2I2", "G2I2", "G2I3", "G3I4", "G3I1", "G3I2", "G3I4", "G4I1", "G4I1", "G4I2" )
    )

d2 = data.frame(from=c(d$parent1,d$parent2), to=rep(d$offspring,2))
g=graph_from_data_frame(d2)
co=layout.reingold.tilford(g, flip.y=T)
plot(g,layout=co)

enter image description here

但是一些没有留下任何后代的人在图表中丢失了。

第二个答案使用kinship2。根据我的理解,kinship2不能处理无性繁殖。

r plot graph igraph genealogy
1个回答
2
投票

我唯一看错的是G1中重叠的节点。有了更多信息,我很乐意根据需要调整输出。

library(igraph)
d = data.frame(
  offspring = c("G2I1", "G2I2", "G2I3", "G3I1", "G3I2", "G3I3", "G3I4", "G4I1", "G4I2", "G4I3", "G4I4", "G5I1", "G5I2", "G5I3"  ),
  parent1   = c("G1I1", "G1I2", "G1I1", "G2I1", "G2I3", "G2I1", "G2I3", "G3I2", "G3I2", "G3I1", "G3I4", "G4I3", "G4I3", "G4I1" ),
  parent2   = c("G1I3", "G1I2", "G1I2", "G2I2", "G2I2", "G2I2", "G2I3", "G3I4", "G3I1", "G3I2", "G3I4", "G4I1", "G4I1", "G4I2" ),
  stringsAsFactors = F
)

d2 = data.frame(from=c(d$parent1,d$parent2), to=rep(d$offspring,2))
g=graph_from_data_frame(d2)
#co=layout.reingold.tilford(g, flip.y=T)
co1 <- layout_as_tree(g, root = which(grepl("G1", V(g)$name)))
#plot(g,layout=co, edge.arrow.size=0.5)
plot(g,layout=co1, edge.arrow.size=0.25)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.