R/heatmap_functions.R
plot_pubmatrix_heatmap.RdThis function creates a heatmap displaying overlap percentages derived from a PubMatrix result matrix, with Euclidean distance clustering for rows and columns.
plot_pubmatrix_heatmap(
matrix,
title = "PubMatrix Co-occurrence Heatmap",
cluster_rows = TRUE,
cluster_cols = TRUE,
show_numbers = TRUE,
color_palette = NULL,
filename = NULL,
width = 10,
height = 8,
cellwidth = NA,
cellheight = NA,
scale_font = TRUE
)A data frame or matrix from PubMatrix results containing publication co-occurrence counts
Character string for the heatmap title. Default is "PubMatrix Co-occurrence Heatmap"
Logical value determining if rows should be clustered using Euclidean distance. Default is TRUE
Logical value determining if columns should be clustered using Euclidean distance. Default is TRUE
Logical value determining if overlap percentage values should be displayed in cells. Default is TRUE
Color palette for the heatmap. Default uses a red gradient color scale
Optional filename to save the heatmap. If NULL, displays the plot
Width of saved plot in inches. Default is 10
Height of saved plot in inches. Default is 8
Optional numeric cell width for pheatmap (in pixels). Default `NA` lets pheatmap auto-size.
Optional numeric cell height for pheatmap (in pixels). Default `NA` lets pheatmap auto-size.
Logical value determining if font size should scale with cell size. Default is TRUE
A pheatmap object (invisible)
The function displays overlap percentages in heatmap cells and uses Euclidean distance for clustering rows and columns. Overlap percentages are computed from the observed co-occurrence counts using `intersection / union * 100`, where the union is derived from row and column totals. NA values in the input matrix are converted to 0 before calculation to ensure stability.
# Create a small test matrix
test_matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
rownames(test_matrix) <- c("Gene1", "Gene2")
colnames(test_matrix) <- c("GeneA", "GeneB")
# Create heatmap using the helper
plot_pubmatrix_heatmap(test_matrix, title = "Test Heatmap")
# Equivalent using pheatmap directly:
# Compute overlap matrix as the function does (here trivial because counts are raw)
overlap_matrix <- test_matrix
pheatmap::pheatmap(
overlap_matrix,
main = "Test Heatmap (pheatmap)",
color = colorRampPalette(c("#fee5d9", "#cb181d"))(100),
display_numbers = TRUE,
fontsize = 16,
fontsize_number = 14,
border_color = "lightgray",
show_rownames = TRUE,
show_colnames = TRUE
)