如何在R中绘制家谱

时间:2012-07-01 07:42:51

标签: r

我一直在寻找如何绘制一个家谱,但找不到我可以复制的东西。我一直在寻找哈德利关于ggplot的书,但同样的事情。

我想绘制一个家族树,其中包含与此类似的数据帧:

dput(head(familyTree))
structure(
  list(
    id = 1:6, 
    cnp = c("11", NA, "22", NA, NA, "33"), 
    last_name = c("B", "B", "B", NA, NA, "M"), 
    last_name_alyas = c(NA, NA, NA, NA, NA, "M"), 
    middle_name = c("C", NA, NA, NA, NA, NA), 
    first_name = c("Me", "P", "A", NA, NA, "S"), 
    first_name_alyas = c(NA, NA, NA, NA, NA, "F"), 
    maiden_name = c(NA, NA, "M", NA, NA, NA), 
    id_father = c(2L, 4L, 6L, NA, NA, 8L), 
    id_mother = c(3L, 5L, 7L, NA, NA, 9L), 
    birth_date = c("1986-01-01", "1963-01-01", "1964-01-01", NA, NA, "1936-01-01"), 
    birth_place = c("City", "Village", "Village", NA, NA, "Village"), 
    death_date = c("0000-00-00", NA, NA, NA, NA, "2007-12-23"), 
    death_reason = c(NA, NA, NA, NA, NA, "stroke"), 
    nr_brothers = c(NA, 1L, NA, NA, NA, NA), 
brothers_names = c(NA, "M", NA, NA, NA, NA), 
    nr_sisters = c(1L, NA, 1L, NA, NA, 2L), 
    sisters_names = c("A", NA, "E", NA, NA, NA), 
    school = c(NA, "", "", NA, NA, ""), 
    occupation = c(NA, "", "", NA, NA, ""), 
    diseases = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), 
    comments = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_)
  ), 
  .Names = c("id", "cnp", "last_name", "last_name_alyas", "middle_name", "first_name", "first_name_alyas", "maiden_name", "id_father", "id_mother", "birth_date", "birth_place", "death_date", "death_reason", "nr_brothers", "brothers_names", "nr_sisters", "sisters_names", "school", "occupation", "diseases", "comments"), 
  row.names = c(NA, 6L), 
  class = "data.frame"
)

有什么办法可以用ggplot绘制一个家谱吗?如果没有,我怎么能用另一个包来绘制它。

主键是'id',您使用“id_father”和“id_mother”连接到该家庭的其他成员。

2 个答案:

答案 0 :(得分:10)

如评论中所述,您应该尝试igraph。这是一个快速入门:

require(igraph)
mothers=familyTree[,c('id','id_mother','first_name', 'last_name')]
fathers=familyTree[,c('id','id_father','first_name', 'last_name')]
mothers$name=paste(mothers$first_name,mothers$last_name)
fathers$name=paste(fathers$first_name,fathers$last_name)
names(mothers)=c('parent','id','first_name','last_name','name')
names(fathers)=c('parent','id','first_name','last_name','name')
links=rbind(mothers,fathers)
links=links[!is.na(links$id),]
g=graph.data.frame(links)
co=layout.reingold.tilford(g, flip.y=F)
plot(g,layout=co)

enter image description here

没有任何名字,箭头方向错误,但你应该可以从那里开始。

答案 1 :(得分:4)

您是否尝试过kinship2套餐?

library(kinship2)
df <- data.frame(id = c(1,2,3,4,5,6), sex = c(1,2,1,2,2,2), dadid = c(0,0,0,0,1,3), momid = c(0,0,0,0,2,4), famid = 1)
relation1 <- matrix(c(2,3,4,1), nrow = 1)
foo <- pedigree(id = df$id, dadid = df$dadid, momid = df$momid, sex = df$sex, relation = relation1, famid = df$famid)
ped <- foo['1']
plot(ped)

您可以看到结果图:

enter image description here