Heatmap.Rd
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,
...
)
A matrix for input, for instance, one generated using the CalcStats function.
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.
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.
Color for the tile borders. Default: NULL.
Label for the color. Default: 'score'.
Angle of x-axis labels. Default: NULL (auto-determined).
Horizontal justification of x-axis labels. Default: NULL (auto-determined).
Vertical justification of x-axis labels. Default: NULL (auto-determined).
Position of the figure legend. Default: 'right'.
Position of the row name text. Default: 'right'
A subset of feature names to be shown. Useful when there are many features. Default: NULL.
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).
Thickness of the line connecting heatmap rows and feature text. Only effective when `feature_text_subset` is set. Default: 0.2.
Spacing between feature texts. Only effective when `feature_text_subset` is set. Default: 2.
Font size of the feature text. Only effective when `feature_text_subset` is set. Default: 2.5.
Whether to hide the axis line or not. Default: TRUE.
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.
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.
Vector or factor to split the heatmap columns. Default: NULL.
Vector or factor to split the heatmap rows. Default: NULL.
Spacing between panels when rows or columns are split. Default: unit(5, "pt").
Placement of panel names when rows or columns are split. Default: 'outside'.
Number of columns when either `facet_col` or `facet_row` is not NULL, wrapping the heatmap. Default: NULL.
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.
A ggplot object.
For more detailed usage, see the examples provided.
# 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())