Skip to contents

This function generates a PCA plot for a `SummarizedExperiment` object using precomputed PCA results.

Usage

plot_pca(
  exp_data,
  color = NA,
  shape = NA,
  fname = "results/qc/plot_PCA.pdf",
  pcs = c(1, 2),
  res_name = "pca_res",
  id_name = "sample_id",
  point_size = 2,
  color_in = c("color", "fill")[1],
  out = c("plotly", "ggplot")[1]
)

Arguments

exp_data

A `SummarizedExperiment` object containing gene expression data. PCA results must be stored in `exp_data@metadata` under the name specified by `res_name`.

color

A character string specifying the column name in `colData(exp_data)` to use for coloring the points. Default is `NA`, meaning no coloring.

shape

A character string specifying the column name in `colData(exp_data)` to use for shaping the points. Default is `NA`, meaning no shaping.

fname

A character string specifying the file path to save the plot as a PDF. Default is `"results/qc/plot_PCA.pdf"`. Set to `NULL` to skip saving.

pcs

An integer vector of length 2 specifying the principal components to plot on the x and y axes. Default is `c(1, 2)`.

res_name

A character string specifying the name of the PCA results stored in `exp_data@metadata`. Default is `"pca_res"`.

id_name

A character string specifying the column name in `colData(exp_data)` containing sample identifiers. Default is `"sample_id"`.

point_size

A numeric value specifying the size of the points in the plot. Default is `2`.

color_in

A character string indicating whether the color aesthetic should be applied to `"color"` (border) or `"fill"` (interior). Default is `"color"`.

out

A character string indicating the output type: `"plotly"` (interactive Plotly plot) or `"ggplot"` (static ggplot). Default is `"plotly"`.

Value

A PCA plot, either as a `plotly` interactive object or a `ggplot` static object, depending on the `out` parameter.

Details

The function creates a scatter plot of the specified principal components (`pcs[1]` and `pcs[2]`). Users can optionally: - Color points based on a metadata column (`color`). - Shape points based on a metadata column (`shape`). - Save the static plot as a PDF if `fname` is not `NULL`.

If PCA results are not precomputed using `pca_gexp`, the function will throw an error. The proportion of variance explained by the principal components is displayed on the axis labels.