Q_plot: Plot a Q matrix using ggplot2

View source: R/fstruct_functions.R

Q_plotR Documentation

Plot a Q matrix using ggplot2

Description

This function enables graphical visualization of a Q matrix, the default output of population structure inference software programs such as STRUCTURE and ADMIXTURE. In the output plot, each vertical bar represents a single individual's ancestry; the height of each color in the bar corresponds to the individual membership coefficients given by the Q matrix. Because this function produces a ggplot object, its output can be modified using standard ggplot2 syntax. For a more comprehensive population structure visualization program, see the program distruct.

Usage

Q_plot(Q, K = ncol(Q), arrange)

Arguments

Q

A dataframe, matrix, or array representing a Q matrix. Each row represents an individual, and the last K columns contain individual membership coefficients. The first few columns may contain information not relevant to this plot; their inclusion is optional. When restricted to the last K columns, the rows of this matrix must sum to approximately 1.

K

The number of ancestral clusters in the Q matrix. Each individual must have K membership coefficients.

arrange

Optional variable controlling horizontal ordering of individuals. If arrange = TRUE, individuals are ordered by the clusters of greatest mean membership. K values of 11 or fewer.

Value

A ggplot object describing a bar plot of membership coefficients from the Q matrix.

Examples

Q_plot(
  # Make an example matrix of membership coefficients.
  # Each row is an individual. Rows sum to 1.
  Q = matrix(c(
    .4, .2, .4,
    .5, .3, .2,
    .5, .4, .1,
    .6, .1, .3,
    .6, .3, .1
  ),
  nrow = 5,
  byrow = TRUE
  ),
 K = 3, # How many ancestry coefficients per individual?
 arrange = TRUE
) +
  # Below are example, optional modifications to the default plot
  ggplot2::ggtitle("Population A") +
  ggplot2::scale_fill_brewer("Blues") +
  ggplot2::scale_color_brewer("Blues") +
  ggplot2::xlab("Individuals")
  # Note that both scale_fill and scale_color are needed to change the color of the bars.

MaikeMorrison/FSTruct documentation built on Aug. 26, 2023, 7:01 a.m.