Generates a heatmap plot.

Heatmap(
  score,
  color_scheme = "BuRd",
  center_color = NULL,
  border_color = NULL,
  lab_fill = "score",
  angle = NULL,
  hjust = NULL,
  vjust = NULL,
  legend_position = "right",
  y_text_position = "right",
  feature_text_subset = NULL,
  segment.width = c(1, 2.5, 1),
  segment.size = 0.2,
  text.spacing = 2,
  text.size = 2.5,
  hide_axis_line = TRUE,
  plot.margin = margin(t = 5.5, r = 5.5, b = 5.5, l = 5.5),
  expand_limits_x = NULL,
  facet_col = NULL,
  facet_row = NULL,
  panel.spacing = unit(5, "pt"),
  strip.placement = "outside",
  ncol = NULL,
  nrow = NULL,
  ...
)

Arguments

score

A matrix for input, for instance, one generated using the CalcStats function.

color_scheme

Specifies the color gradient for the heatmap visualization. This parameter accepts multiple input formats to provide flexibility in defining color schemes:

- Predefined color schemes from the `viridis` package ("A" to "H").

- Named vector with keys "low", "mid", and "high" for three-point gradients. Example: `c(low = muted("blue"), mid = "white", high = muted("red"))`.

- Two-point gradient with keys "low" and "high". Example: `c(low = "lightblue", high = "red")`.

- RColorBrewer sequential palettes: "Blues", "BuGn", "BuPu", "GnBu", "Greens", "Greys", "Oranges", "OrRd", "PuBu", "PuBuGn", "PuRd", "Purples", "RdPu", "Reds", "YlGn", "YlGnBu", "YlOrBr", "YlOrRd".

- RColorBrewer diverging palettes: "BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYlBu", "RdYlGn", "Spectral".

- Custom diverging palettes: "GnYlRd", "BuYlRd", "GyRd", "BuRd", "PuOr".

- Append "-rev" to any RColorBrewer palette name to reverse the color order. Example: "RdBu-rev".

- Custom color gradient using a vector of colors.

center_color

Logical or NULL. Determines whether the color scale should be centered at zero. If TRUE, the color scale will be centered at zero, with the midpoint color representing zero. If FALSE, the color scale will span the full range of the data without centering. If NULL (default), it will automatically determine based on the color scheme: TRUE for diverging color palettes, FALSE for sequential palettes or custom color schemes. This is particularly useful for visualizing data with both positive and negative values, such as z-scores or log fold changes.

border_color

Color for the tile borders. Default: NULL.

lab_fill

Label for the color. Default: 'score'.

angle

Angle of x-axis labels. Default: NULL (auto-determined).

hjust

Horizontal justification of x-axis labels. Default: NULL (auto-determined).

vjust

Vertical justification of x-axis labels. Default: NULL (auto-determined).

legend_position

Position of the figure legend. Default: 'right'.

y_text_position

Position of the row name text. Default: 'right'

feature_text_subset

A subset of feature names to be shown. Useful when there are many features. Default: NULL.

segment.width

Adjust the width ratio of the line connecting heatmap rows and feature text. Only effective when `feature_text_subset` is set. Default: c(1, 2.5, 1).

segment.size

Thickness of the line connecting heatmap rows and feature text. Only effective when `feature_text_subset` is set. Default: 0.2.

text.spacing

Spacing between feature texts. Only effective when `feature_text_subset` is set. Default: 2.

text.size

Font size of the feature text. Only effective when `feature_text_subset` is set. Default: 2.5.

hide_axis_line

Whether to hide the axis line or not. Default: TRUE.

plot.margin

Adjusts the space around the plot to prevent text labels from being cut off. This is particularly useful when axis names are too long and may extend beyond the plot boundaries. The margin is defined for top (t), right (r), bottom (b), and left (l) edges of the plot area. To increase the left margin and provide more space for the first name on the x-axis, increase the `l` value. Default: `margin(t = 5.5, r = 5.5, b = 5.5, l = 5.5)` in grid units.

expand_limits_x

This parameter is softly deprecated in favor of using `plot.margin`. Previously used to increase the space on the left side of the plot by adjusting the x-axis limits. It is still available for backward compatibility but its use is not recommended. Default: NULL.

facet_col

Vector or factor to split the heatmap columns. Default: NULL.

facet_row

Vector or factor to split the heatmap rows. Default: NULL.

panel.spacing

Spacing between panels when rows or columns are split. Default: unit(5, "pt").

strip.placement

Placement of panel names when rows or columns are split. Default: 'outside'.

ncol

Number of columns when either `facet_col` or `facet_row` is not NULL, wrapping the heatmap. Default: NULL.

nrow

Number of rows when either `facet_col` or `facet_row` is not NULL, wrapping the heatmap. Default: NULL.

...

Additional parameters passed to the `theme` function of ggplot2.

Value

A ggplot object.

Details

For more detailed usage, see the examples provided.

Examples

# First, create a matrix using the CalcStats function.
genes <- VariableFeatures(pbmc)
toplot <- CalcStats(pbmc, features = genes, method = "zscore", order = "p", n = 5)

# Generate a basic heatmap.
Heatmap(toplot, lab_fill = "zscore")

# Modify the color theme to range from white to dark green.
Heatmap(toplot, lab_fill = "zscore", color_scheme = c("white", muted("green")))

# Use a color theme that transitions from dark blue to light yellow (centered at 0) to dark red.
Heatmap(toplot, lab_fill = "zscore", color_scheme = c(
  low = muted("blue"),
  mid = "lightyellow",
  high = muted("red"))
)

# Employ the viridis color theme, options include A, B, C, D, or E.
Heatmap(toplot, lab_fill = "zscore", color_scheme = "A")

# Adjust the left margin to ensure the x-axis labels fit within the plot boundaries
Heatmap(toplot, lab_fill = "zscore", plot.margin = margin(l = 30))

# Construct a dense matrix with data for 500 genes.
toplot2 <- CalcStats(pbmc, features = genes[1:500], method = "zscore", order = "p")

# Display only a subset of gene names.
Heatmap(toplot2, lab_fill = "zscore", feature_text_subset = genes[1:20], expand_limits_x = c(-0.5, 11))

# Divide the heatmap into rows based on gene groups.
gene_groups <- sample(c("group1", "group2", "group3"), nrow(toplot2), replace = TRUE)
Heatmap(toplot2, lab_fill = "zscore", facet_row = gene_groups) +
  theme(axis.text.y = element_blank())