How to plot family tree in R
r
Solution
As noted in the comments, you should try `igraph`. Here is a quick start:
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)
There aren't any names, and the arrows are going in the wrong direction, but you should be able to go from there.
Problem
I've been searching around how to plot a family tree but couldn't find something i could reproduce. I've been looking in Hadley's book about ggplot but the same thing. I want to plot a family tree having as a source a dataframe similar to this: ``` familyTree <- data.frame( id = 1:6, cnp = c("11", NA, "22", NA, NA, "33"), last_name = c("B", "B", "B", NA, NA, "M"), last_name_alyas = rep(c(NA, "M"), c(5L, 1L)), middle_name = rep(c("C", NA), c(1L, 5L)), first_name = c("Me", "P", "A", NA, NA, "S"), first_name_alyas = rep(c(NA, "F"), c(5L, 1L)), 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 = rep(c(NA, "stroke"), c(5L, 1L)), 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 = rep(NA_character_, 6L), comments = rep(NA_character_, 6L) ) ``` Is there any way I can plot a family tree with ggplot? If not, how can i plot it using another package. The primary key is 'id' and you connect to other members of the family using "id_father" and "id_mother".