Skip to contents

Creates a knee plot (also called a barcode rank plot) that displays the relationship between cell rank and read depth on a log-log scale. This visualization is essential for single-cell RNA-seq quality control as it helps identify the transition between high-quality cells and background droplets, allowing users to determine appropriate filtering thresholds.

Arguments

values

(data.frame; numeric vector; MarchRDataset; MarchRProject) Input data containing read counts. Can be:

  • A data frame with read counts in column readDepth (obtained from get_matrix_qc())

  • A numeric vector of read counts per cell

  • A MarchRDataset object (will extract QC metrics automatically)

  • A MarchRProject object (will extract QC metrics from specified dataset/assay)

return_data

(logical) If TRUE, return the processed data used for plotting instead of the actual plot. Useful for custom visualizations or further analysis. Default is FALSE.

apply_styling

(logical) If FALSE, return a basic ggplot without MarchR styling applied. Set to TRUE (default) for publication-ready plots with consistent theming.

assay_name

(character) For MarchRDataset/MarchRProject: Name of the matrix/assay to use for generating the knee plot. Uses smart selection if not specified.

dataset_name

(character) For MarchRProject only: Name of the dataset containing the matrix. Uses smart selection if not specified.

...

Additional arguments passed to BPCells::plot_read_count_knee().

Value

A ggplot2 object showing the knee plot, or a data frame if return_data = TRUE. The plot shows:

  • X-axis: Cell rank (log scale) - cells ordered by decreasing read count

  • Y-axis: Read depth per cell (log scale) - total UMI/read counts

  • The "knee" point where the curve transitions from steep to shallow slope

Details

The knee plot is a fundamental QC visualization in single-cell analysis that helps distinguish between:

  1. High-quality cells: Show high read counts and form the steep part of the curve

  2. Low-quality cells/doublets: May have intermediate read counts

  3. Empty droplets/background: Have low read counts and form the shallow tail

The "knee" represents the inflection point where the slope changes dramatically, typically indicating the transition from cells to empty droplets. Cells to the left of the knee are usually considered high-quality, while those to the right may be filtered out.

This function wraps BPCells::plot_read_count_knee() and integrates seamlessly with the MarchR QC workflow. It can be used directly after running qc_matrix() to visualize the effect of filtering thresholds.

See also

qc_matrix for running matrix quality control get_matrix_qc for extracting QC metrics run_matrix_qc for the underlying QC function

Examples

# Create a demo project with unfiltered data
project <- MarchRProject()
demo_mat <- BPCells::get_demo_mat(subset = FALSE, filter_qc = FALSE)
project <- add_matrix(project, demo_mat, dataset_name = "demo", mat_name = "RNA")

# Run QC to generate metrics
project_qc <- qc_matrix(project,
                        dataset_name = "demo",
                        assay_name = "RNA",
                        read_depth = 1000)

# Plot knee curve directly from project
plot_knee(project_qc, dataset_name = "demo", assay_name = "RNA")


# Extract QC metrics first, then plot
qc_metrics <- get_matrix_qc(project_qc, dataset_name = "demo", assay_name = "RNA")
plot_knee(qc_metrics)


# Plot with custom styling disabled
plot_knee(qc_metrics, apply_styling = FALSE)


# Return data for custom analysis
knee_data <- plot_knee(qc_metrics, return_data = TRUE)
head(knee_data)
#> $data
#> # A tibble: 3,609 × 2
#>    ranks reads
#>    <dbl> <dbl>
#>  1     1 29839
#>  2     2 28228
#>  3     3 28075
#>  4     4 24507
#>  5     5 19141
#>  6     6 18932
#>  7     7 17306
#>  8     8 17066
#>  9     9 15412
#> 10    10 15276
#> # ℹ 3,599 more rows
#> 
#> $cells_passing
#> [1] NA
#> 

# Use with numeric vector directly
read_counts <- qc_metrics$readDepth
plot_knee(read_counts)