Animated ordinations

In some cases it is useful to look at longitudinal data animated in an ordination. If your dataset includes longitudinal data (time, position, pH, etc), you can draw an animated trace using the Animations tab. For more information look at this tutorial.

In [ ]:
import pandas as pd, numpy as np
from emperor import Emperor, nbinstall
from skbio import OrdinationResults


def load_mf(fn, index='#SampleID'):
    _df = pd.read_csv(fn, sep='\t', dtype=str, keep_default_na=False, na_values=[])
    _df.set_index(index, inplace=True)
    return _df

We are going to load data from Caporaso et al. 2011. This is a subset of the full dataset, if you wish to look at the full data you can find it in study 550 in Qiita (remember you need to be logged in to access the study).

In [ ]:
mf = load_mf('moving-pictures/sample-metadata.tsv')
res ='moving-pictures/ordination.txt')

Recall that in this study two subjects are sampled longitudinally over four different sites (gut, left palm, right palm and tongue). In order to look at an individual trace per body site and per subject, we need to combine the Subject and the BodySite columns.

In [ ]:
mf['animated_trace'] = mf.Subject + ' ' + mf.BodySite

If you want to share your notebook via GitHub use remote=True and make sure you share your notebook using nbviewer.

In [ ]:
viz = Emperor(res, mf, remote=False)

In the Gradient dropdown, select the DaysSinceExperimentStart category. This category determines the order to connect the samples in a trace. And in the Trajectory dropdown, select the animated_trace category. This category indicates which samples will constitute each trace. Lastly, to look at the animated traces, go to the Animations tab and click the play button.

As you can see in the animation, each body site for each subject is animated individually over time.

In [ ]: