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.

Usage

rgi_timeline(
  data,
  subject = "host_subject_id",
  date = "collection_date",
  sample = "sample_id",
  site = NULL,
  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.

date

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

sample

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

site

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

color_var

What the point colours show:

"site" (the default)

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

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 (e.g. ifelse(genes$host_subject_id %in% cases, "case", "control")).

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 (a subject sampled at several sites then has a row in each site's panel), 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. Best for comparing the spacing of samples across subjects.

"study-day"

days since the first sample in data, across all subjects, so day 0 is the study's first contact and each subject starts on the day they entered. Shows enrollment and follow-up on one scale.

"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. "donations".

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 rgi_aggregate() with run metadata joined (one row per sample and resistance gene); it is reduced to one row per sample first. 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, mapped with RGI or not.

This is the same function as sylph2lab::sylph_timeline() and funprofiler2lab::funprofiler_timeline(), so a cohort is drawn the same way whichever of the three packages you use.

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.

See also

rgi_aggregate() to make data.

Examples

genes <- rgi_aggregate(rgi_example("results"),
                       metadata = rgi_example("runs"))
#> 1221 of 2469 gene detections were below `min_coverage` = 50% or `min_reads` = 0 and were left out.
rgi_timeline(genes, site = "isolation_source")


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


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


# calendar dates, one colour per subject
rgi_timeline(runs, sample = "run_accession", x_axis = "calendar-day")


# colour by a subject feature, given as a vector: sampled on > 1 day?
visits <- unique(runs[c("host_subject_id", "collection_date")])
repeat_sampled <- names(which(table(visits$host_subject_id) > 1))
rgi_timeline(
  runs, sample = "run_accession",
  color_var = ifelse(runs$host_subject_id %in% repeat_sampled,
                     "repeat", "single visit"),
  color_label = "Follow-up"
)


# one panel per site, coloured by site
rgi_timeline(genes, site = "isolation_source", facet_var = "site")