Skip to content

Instantly share code, notes, and snippets.

@rmaia
Created May 4, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmaia/dd98a1fc677a470cb0f66c00a1c8bb26 to your computer and use it in GitHub Desktop.
Save rmaia/dd98a1fc677a470cb0f66c00a1c8bb26 to your computer and use it in GitHub Desktop.
Plot a phylogeny with bars, as separate plots, combined
require(ape)
require(geiger)
# use Geospiza data for example
data(geospiza)
phylo <- drop.tip(geospiza$geospiza.tree, 'olivacea')
bardata <- setNames(geospiza$geospiza.data[,'beakD'], rownames(geospiza$geospiza.data))
# create dummy categorical variable, highlighting ground finches
statedata <- setNames(c(1,0,0,0,1,1,0,0,0,0,0,0,0), names(bardata)) +1
# create second dummy categorical variable, for illustrative purposes
madeupdata <- setNames(sample(c(0,1), size=Ntip(phylo), replace=TRUE), names(bardata)) + 1
# order data so bars align with tips
bar.ordered <- bardata[phylo$tip.label]
state.ordered <- statedata[phylo$tip.label]
madeup.ordered <- madeupdata[phylo$tip.label]
# give categorical data pretty colors
state.ordered <- factor(state.ordered, labels=c("#ADC0C4","#FF0000"))
madeup.ordered <- factor(madeup.ordered, labels=c("#ADC0C4","#FF0000"))
# specify a plotting area with two columns, one for the phylo + one for the bars
layout(matrix(c(1,1,1,1,2), nrow=1))
# you will need to play around with the margins
# to get the tips and bars to match
# plot phylogeny
par(mar=c(3,0,1,0))
plot(phylo, show.tip.label=F, edge.width=2)
# plot categorical variables (adjust size and positions as needed)
tiplabels(pch=15, col=as.character(state.ordered), cex=2, adj=0.51)
tiplabels(pch=15, col=as.character(madeup.ordered), cex=2, adj=0.525)
# plot bar data
par(mar=c(2,0,0,5))
barplot(bar.ordered, horiz=T, axisnames=F, xlim=c(0,2), col=as.character(state.ordered), border=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment