在 R 中绘制家谱
Plot a genealogy in R
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
数据表示
这个数据代表一个家谱。每行表示一个后代及其两个 parent。我称他们为 parent1
和 parent2
因为他们是雌雄同体。而且,他们可以克隆自己!世代不重叠,这意味着 n
世代的所有 parent 后代都出生在 n-1
.
世代
让我们考虑一个例子。个体G3I4
出生于第3代(G3
),是本代个体索引4(I4
;索引只是一个ID)。此人是 parent 个人 G4I1
和个人 G4I4
。事实上,G3I4
是 G4I4
中唯一的 parent,因为她克隆了自己。
问题
如何在 R
中绘制此家谱?
相关post
post 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)
但是图中缺少一些不留下任何后代的个体。
第二个答案使用kinship2
。据我了解,kinship2
无法处理无性繁殖。
我唯一看错的是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)
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
数据表示
这个数据代表一个家谱。每行表示一个后代及其两个 parent。我称他们为 parent1
和 parent2
因为他们是雌雄同体。而且,他们可以克隆自己!世代不重叠,这意味着 n
世代的所有 parent 后代都出生在 n-1
.
让我们考虑一个例子。个体G3I4
出生于第3代(G3
),是本代个体索引4(I4
;索引只是一个ID)。此人是 parent 个人 G4I1
和个人 G4I4
。事实上,G3I4
是 G4I4
中唯一的 parent,因为她克隆了自己。
问题
如何在 R
中绘制此家谱?
相关post
post 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)
但是图中缺少一些不留下任何后代的个体。
第二个答案使用kinship2
。据我了解,kinship2
无法处理无性繁殖。
我唯一看错的是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)