Skip to contents

Visualize the number of observations per sample.

Usage

rare_stacked(
  biom,
  rline = TRUE,
  counts = TRUE,
  labels = TRUE,
  y.transform = "log10",
  ...
)

Arguments

biom

An rbiom object, or any value accepted by as_rbiom().

rline

Where to draw a horizontal line on the plot, intended to show a particular rarefaction depth. Set to TRUE to show an auto-selected rarefaction depth, FALSE to not show a line, or an integer for a custom position. Default: TRUE.

counts

Display the number of samples and reads remaining after rarefying to rline reads per sample. Default: TRUE.

labels

Show sample names under each bar. Default: TRUE.

y.transform

Y-axis transformation. Options are "log10" or "none". Default: "log10". Use xaxis.transform or yaxis.transform to pass custom values directly to ggplot2's scale_* functions.

...

Additional parameters to pass along to ggplot2 functions. Prefix a parameter name with r. to ensure it gets passed to (and only to) geom_hline. For instance, r.color = "black" ensures only the horizontal rarefaction line has its color set to "black".

Value

A ggplot2 plot. The computed data points and ggplot command are available as $data and $code, respectively.

Examples

    library(rbiom)

    rare_stacked(hmp50)


    rare_stacked(hmp50, rline = 500, r.linewidth = 2, r.linetype = "twodash")


    fig <- rare_stacked(hmp50, counts = FALSE)
    fig$code
#> # Generated by rbiom 4.0.0
#> library(rbiom)
#> library(ggplot2)
#> 
#> biom <- hmp50
#> 
#> # Reads per sample, from fewest to most.
#> ss <- sort(sample_sums(biom))
#> rline <- min(ss[ss >= sum(ss) * 0.1 / length(ss)])   # auto-selected rarefaction depth
#> 
#> # Each sample has an 'Excluded' bar (reads above rline) and a 'Retained' bar.
#> data <- tibble::tibble(
#>   .x     = factor(rep(names(ss), 2), levels = names(ss)),
#>   .group = factor(rep(c("Excluded", "Retained"), each = length(ss))),
#>   .xmin  = rep(seq_along(ss) - 0.4, 2),
#>   .xmax  = rep(seq_along(ss) + 0.4, 2),
#>   .ymin  = as.vector(c(ifelse(ss  < rline, 1, rline), ifelse(ss < rline, 0, 1))),
#>   .ymax  = as.vector(c(ifelse(ss == rline, 0, ss),   ifelse(ss < rline, 0, rline))) )
#> data <- data[data$.ymin > 0 & data$.ymax > 0, ]
#> 
#> ggplot(data) +
#>   geom_point(
#>     mapping = aes(x = .x), 
#>     alpha   = 0, 
#>     y       = 1 ) +
#>   geom_rect(
#>     mapping = aes(xmin = .xmin, xmax = .xmax, ymin = .ymin, ymax = .ymax, fill = .group), 
#>     color   = NA ) +
#>   geom_hline(
#>     yintercept = 1183, 
#>     color      = "#D55E00", 
#>     linetype   = "dashed" ) +
#>   labs(
#>     fill = "Reads", 
#>     x    = NULL, 
#>     y    = "Sequencing Depth\n(log10 scale)" ) +
#>   scale_fill_manual(values = c('Excluded' = "#BBBBBB", 'Retained' = "#0072B2")) +
#>   scale_x_discrete() +
#>   scale_y_continuous(
#>     breaks       = 10^(0:5), 
#>     minor_breaks = as.vector(2:9 %o% 10^(0:4)), 
#>     labels       = scales::label_number(scale_cut = scales::cut_si("")), 
#>     expand       = c(0, 0), 
#>     transform    = "log10" ) +
#>   theme_rbiom() +
#>   theme(axis.text.x = element_text(hjust = 0, vjust = 0.3, angle = -90))