trans_rarefy: Rarefaction and plotting.

trans_rarefyR Documentation

Rarefaction and plotting.

Description

Rarefaction based on the trans_norm class and visualization of the rarefaction curve based on the ggplot2.

Methods

Public methods


Method new()

Usage
trans_rarefy$new(
  dataset = NULL,
  alphadiv = "Shannon",
  depth = NULL,
  PD = FALSE,
  ...
)
Arguments
dataset

the object of microtable Class.

alphadiv

default "Shannon"; one or more alpha diversity measurements used for the rarefaction; see microtable$cal_alphadiv for all the available measurements. The measurement names are case-insensitive, e.g. "shannon" is same as "Shannon". Multiple measurements are supported and share the same rarefying process.

depth

default NULL; a numeric vector used for the rarefying. 0 represents using the original data without rarefying and the diversity values are all set to 0 as the starting point of the curve. If NULL, a sequence of 10 depths from 0 to the maximum of sample sums (i.e. max(dataset$sample_sums())) is generated automatically using a quadratic spacing round(max_depth * seq(0, 1, length.out = 10)^2), so the depths are denser in the low-depth region where the rarefaction curve rises steeply and sparser in the high-depth region where the curve becomes flat. Note that for each sample, only the depths not larger than its own sequencing depth are available, as the sample is removed at the larger depths by the norm function of trans_norm class, leading to an interrupted curve in the plot.

PD

default FALSE; whether add Faith's phylogenetic diversity (PD) to the alpha diversity. The calculation depends on cal_alphadiv function of microtable class, so the phylogenetic tree (phylo_tree in the dataset) is necessary for this option.

...

parameters passed to norm function of trans_norm class, such as method ("rarefy" or "SRS") and rngseed, except the sample.size parameter which is controlled by the depth parameter.

Returns

res_rarefy stored in the object, which is a data.frame with the columns SampleID (sample name), seqnum (the rarefying depth) and the alpha diversity measurements used (the column names are the standard measurement names). The measurements used are also stored in object$measure, and the rarefying depths in object$depth.

Examples
\dontrun{
data(dataset)
t1 <- trans_rarefy$new(dataset = dataset, depth = c(0, 10, 50, 400, 800))
# the depth is generated automatically when it is not provided
t1 <- trans_rarefy$new(dataset = dataset)
}

Method plot_rarefy()

Plotting the rarefied result.

Usage
trans_rarefy$plot_rarefy(
  color_values = RColorBrewer::brewer.pal(8, "Dark2"),
  color = "SampleID",
  measure = NULL,
  show_point = TRUE,
  point_size = 0.3,
  point_alpha = 0.6,
  add_fitting = FALSE,
  fitting_method = "lm",
  fitting_formula = y ~ log(x + 1),
  x_axis_title = "Sequence number",
  y_axis_title = NULL,
  show_legend = TRUE,
  show_samplename = FALSE,
  samplename_size = 3,
  samplename_color = "grey30",
  ...
)
Arguments
color_values

colors used for presentation.

color

default "SampleID"; color mapping in the plot.

measure

default NULL; the alpha diversity measurement(s) used as the y axis; see the alphadiv parameter in new function. If NULL, all the measures stored in the object are used. When multiple measures are used, the plot is faceted by the measures.

show_point

default TRUE; whether show the point.

point_size

default .3; point size value.

point_alpha

default .6; point alpha value.

add_fitting

default FALSE; whether add fitted line.

fitting_method

default "lm"; the method used by ggplot2::geom_smooth function. Only available when add_fitting = TRUE. The default "lm" avoids the warnings generated by the default "loess" method, as there are usually only a few depths for each sample.

fitting_formula

default y ~ log(x + 1); the formula used by ggplot2::geom_smooth function. Only available when add_fitting = TRUE. Note that x + 1 is used instead of x, as the depth 0 makes log(x) invalid.

x_axis_title

default "Sequence number"; x axis title.

y_axis_title

default NULL; default NULL represents the measure used; when multiple measures are used, default NULL represents "Value".

show_legend

default TRUE; whether show the legend in the plot.

show_samplename

default FALSE; whether show the sample name in the plot.

samplename_size

default 3; the sample name text size. Only available when show_samplename is TRUE.

samplename_color

default "grey30"; sample name text color. Only available when show_samplename is TRUE.

...

parameters pass to ggplot2::geom_line (when add_fitting = FALSE) or ggplot2::geom_smooth (when add_fitting = TRUE).

Returns

ggplot.

Examples
\dontrun{
t1$plot_rarefy(color = "Group")
}

Method print()

Print the trans_rarefy object.

Usage
trans_rarefy$print()

Method clone()

The objects of this class are cloneable with this method.

Usage
trans_rarefy$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `trans_rarefy$new`
## ------------------------------------------------

## Not run: 
data(dataset)
t1 <- trans_rarefy$new(dataset = dataset, depth = c(0, 10, 50, 400, 800))
# the depth is generated automatically when it is not provided
t1 <- trans_rarefy$new(dataset = dataset)

## End(Not run)

## ------------------------------------------------
## Method `trans_rarefy$plot_rarefy`
## ------------------------------------------------

## Not run: 
t1$plot_rarefy(color = "Group")

## End(Not run)

microeco documentation built on Oct. 20, 2026, 9:06 a.m.