Skip to contents

Draws one row per subject and one point per sample, placed at the sample's collection day, so you can see at a glance how many samples each subject gave, and when. Each row is labelled with the subject and its number of samples. Points can be coloured by the sample's source (e.g. stool, groin swab or a near-patient environmental surface) or, for cultured isolates, by species, and their shape shows whether each sample is an isolate (triangle) or a metagenome (circle).

Usage

smash_timeline(
  data,
  subject = "host_subject_id",
  date = "collection_date",
  sample = "sample_id",
  site = NULL,
  sample_type = "sample_type",
  species = "species",
  color_var = "site",
  color_label = NULL,
  facet_var = NULL,
  x_axis = c("subject-day", "study-day", "calendar-day"),
  show_counts = TRUE,
  sample_noun = "samples",
  point_size = 3,
  palette = "turbo"
)

Arguments

data

A data frame with sample, subject and collection date columns.

subject

Column identifying the subject (e.g. patient or donor). Defaults to "host_subject_id", the BioSample attribute in sra2lab run tables (also used for isolates prepared with lv2sra).

date

Column with the collection date. Defaults to "collection_date".

sample

Column identifying the sample. Defaults to "sample_id", as made by smash_aggregate(); use "run_accession" for an sra2lab run table.

site

Optional column with the sampling site, the sample's source or another sample category (e.g. "sample_source" after smash_aggregate(), or "isolation_source" in a run table). With color_var = "site" (the default), points are coloured by site, with a legend; without site, each subject has its own colour.

sample_type

Column saying whether each sample is an "isolate" or a "metagenome", which sets the point shapes. Defaults to "sample_type", as made by smash_aggregate(); ignored if data has no such column. NULL draws every sample as a circle.

species

Column with each isolate's species, for color_var = "species" and facet_var = "species". Defaults to "species", as made by smash_aggregate() (where metagenomes have NA).

color_var

What the point colours show:

"site" (the default)

the site column, or the subject when site is not given.

"species"

the species column: each isolate's species, with metagenomes (and isolates of unknown species) in grey.

a column name

a subject feature: a column of data with one value per subject, such as treatment arm, outcome or age.

a vector

a subject feature given directly, one value per row of data.

A subject feature must have the same value in all of a subject's rows. Numeric features get a continuous colour scale, others a discrete one; missing values are grey.

color_label

Legend title for a subject feature. Defaults to the column name, tidied ("treatment_arm" becomes "Treatment arm"), or "Subject feature" for a vector.

facet_var

Optional variable to split the timeline into panels, one above the other in a single column, with one panel per value. It takes the same options as color_var: "site" for the site column or "species" for the species column (a subject then has a row in each panel it has samples in), a subject-level column of data, or a vector with one value per row of data. Each panel shows only its own subjects, and its height follows their number (with ggplot2 3.5.2 or later). Factor levels set the panel order; other values are sorted, with missing values last. NULL (the default) gives a single panel.

x_axis

What the x axis shows:

"subject-day" (the default)

days since each subject's first sample, so every subject starts at day 0.

"study-day"

days since the first sample in data, across all subjects, so each subject starts on the day they entered.

"calendar-day"

calendar dates.

Can be abbreviated, e.g. "study".

show_counts

If TRUE (the default), each subject's label includes its number of samples.

sample_noun

Word for samples in the labels, e.g. "cultures".

point_size

Size of the points.

palette

A viridis palette option ("turbo", "viridis", "magma", ...) for the point colours.

Value

A ggplot object. Add to or modify it as usual, and save it with ggplot2::ggsave().

Details

data is usually the output of smash_aggregate() with run metadata joined (one row per sketch); it is reduced to one row per sample first, so sketches of a sample at several k-mer sizes count once. Any data frame with one or more rows per sample works, for example the run table from sra2lab::get_bioproject_runs(), to show every sample in a BioProject, sketched or not.

Collection dates can be Dates or text in YYYY-MM-DD, YYYY/MM/DD or MM/DD/YYYY form. Samples whose date is missing or cannot be read (for example SRA's "missing" or a bare year) are left out, with a warning.

Isolates and metagenomes. A cohort can hold both: metagenomes of a subject's specimens, and the isolates cultured from them (or from their environment). With a sample_type column (as made by smash_aggregate()), isolates are drawn as triangles and metagenomes as circles. color_var = "species" colours isolates by species, to follow each organism through a subject's timeline, with metagenomes in grey; and facet_var = "species" gives each species its own panel.

See also

smash_aggregate() to make data.

Examples

sketches <- smash_aggregate(smash_example("signatures"),
                            metadata = smash_example("runs"))
#> The signatures hold sketches at more than one setting (k = 31, scaled = 10000, seed = 42, abund; k = 51, scaled = 10000, seed = 42, abund). Choose one with `k`, `scaled`, `seed` and `method` before comparing or plotting.
smash_timeline(sketches, site = "sample_source")


# every run in the BioProject, from the run table alone
runs <- read.csv(smash_example("runs"))
smash_timeline(runs, sample = "run_accession", site = "isolation_source")


# days since the cohort's first sample: shows when subjects entered
smash_timeline(runs, sample = "run_accession", x_axis = "study-day")


# isolates and metagenomes, coloured by isolate species (made-up isolates)
cohort <- data.frame(
  sample_id = paste0("S", 1:8),
  host_subject_id = rep(c("P1", "P2"), each = 4),
  collection_date = as.Date("2025-01-01") + c(0, 0, 7, 14, 3, 3, 10, 20),
  sample_type = c("metagenome", "isolate", "isolate", "metagenome",
                  "metagenome", "isolate", "isolate", "isolate"),
  species = c(NA, "Klebsiella pneumoniae", "Klebsiella pneumoniae", NA,
              NA, "Enterococcus faecium", "Klebsiella pneumoniae",
              "Enterococcus faecium")
)
smash_timeline(cohort, color_var = "species")