R Code: My Ancestors’ Longevity

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

Load relevant R packages


Read in Ancestor Longevity Data

I created a tab-delimited .txt file with one line for each ancestor. The columns used:

  • Gen: a label to distinguish each generation (I used a different number for each generation)
  • Age: age at death
  • Expect: life expectancy based on that person’s year of birth

The code below also adds a column “id” as an index to plot individuals in the order I had them sorted.

long <- read.delim("AncestorLongevity.txt", header=TRUE)
long$id <- as.numeric(rownames(long))

Draw Plot

The first and last lines below can be un-commented-out to save the plot as a pdf.

#pdf("AncestorLongevityPlot.pdf", width=6, height=4)
ggplot(long, aes(x = id)) +
  geom_col(aes(y = Age, fill=as.factor(Gen))) + scale_fill_manual(values=c("lightgoldenrod2","mediumseagreen","lightslateblue","darkorchid1"), name="Generation", labels=c("grandparents","1st great grandparents","2nd great grandparents","3rd great grandparents")) +
  geom_point(aes(y = Expect, color="expect")) +
  scale_color_manual(values=c("expect"="black"), name=element_blank(), labels="average life expectancy\n based on year of birth") +
ggtitle("Longevity of Ancestors") +
xlab("Individual") +
ylab("Age at Death") +
  theme_classic() +
  theme(plot.title = element_text(face="bold", hjust=0.5)) + 


One thought on “R Code: My Ancestors’ Longevity

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: