This function generates comprehensive heatmap visualizations for MEA data analysis, including individual grouping variable heatmaps, combined interaction heatmaps, and variable correlation matrices. It provides flexible scaling, clustering, and customization options with automatic quality filtering and missing data handling.
create_mea_heatmaps_enhanced(
data = NULL,
processing_result = NULL,
config = NULL,
value_column = "Normalized_Value",
variable_column = "Variable",
grouping_columns = c("Treatment", "Genotype"),
sample_id_columns = c("Well"),
timepoint_column = "Timepoint",
scale_method = "z_score",
aggregation_method = "mean",
missing_value_handling = "remove",
cluster_method = "euclidean",
cluster_rows = TRUE,
cluster_cols = TRUE,
create_individual_heatmaps = TRUE,
create_combined_heatmap = TRUE,
create_variable_correlation = TRUE,
output_dir = NULL,
save_plots = FALSE,
plot_format = "png",
plot_width = 10,
plot_height = 8,
dpi = 300,
fontsize = 10,
angle_col = 45,
show_rownames = TRUE,
show_colnames = TRUE,
return_data = TRUE,
verbose = TRUE,
quality_threshold = 0.8,
min_observations = 3
)A list containing:
Named list of heatmap objects for each grouping variable
Heatmap object for grouping variable interactions (if applicable)
List with correlation heatmap and correlation matrix
List containing processing information and parameters used
Each heatmap object contains: heatmap (pheatmap object), scaled_data (processed matrix), raw_data (aggregated input data), annotation (row annotations), annotation_colors (color schemes), and scaling_info (scaling parameters).
A data frame containing MEA measurement data. If NULL, must provide processing_result.
A list object from MEA data processing containing normalized_data or raw_data components. Takes precedence over the data parameter if provided.
Configuration list from MEA processing. If NULL and processing_result is provided, will attempt to use config from processing_result$config_used.
Character string specifying the column containing measurement values (default: "Normalized_Value").
Character string specifying the column containing variable names (default: "Variable").
Character vector of column names to use for grouping (default: c("Treatment", "Genotype")). Function will auto-detect which columns are available.
Character vector of columns identifying individual samples (default: c("Well")).
Character string specifying the timepoint column (default: "Timepoint").
Character string specifying scaling method. Options: "z_score" (default), "min_max", "robust", "none".
Character string specifying how to aggregate multiple measurements. Options: "mean" (default), "median", "sum".
Character string specifying how to handle missing values. Options: "remove" (default), "impute_mean", "impute_zero".
Character string specifying clustering distance method. Options: "euclidean" (default), "correlation", "manhattan".
Logical indicating whether to cluster rows (default: TRUE).
Logical indicating whether to cluster columns (default: TRUE).
Logical indicating whether to create separate heatmaps for each grouping variable (default: TRUE).
Logical indicating whether to create interaction heatmap when multiple grouping variables are present (default: TRUE).
Logical indicating whether to create variable correlation heatmap (default: TRUE).
Character string specifying output directory (default: NULL, no files saved)
Logical indicating whether to save plots to disk (default: FALSE)
Character string specifying file format for saved plots (default: "png").
Numeric value specifying plot width in inches (default: 10).
Numeric value specifying plot height in inches (default: 8).
Numeric value specifying resolution for saved plots (default: 300).
Numeric value specifying font size for heatmap labels (default: 10).
Numeric value specifying angle for column labels in degrees (default: 45).
Logical indicating whether to show row names (default: TRUE).
Logical indicating whether to show column names (default: TRUE).
Logical indicating whether to return processed data matrices (default: TRUE).
Logical indicating whether to print progress messages (default: TRUE).
Numeric value between 0-1 specifying minimum data completeness per variable (default: 0.8).
Numeric value specifying minimum observations required per group (default: 3).
The function performs several key operations:
Quality filtering: Removes variables with insufficient data completeness
Missing value handling: Multiple strategies for dealing with NA values
Data aggregation: Combines multiple measurements per group using specified method
Scaling: Applies normalization methods appropriate for heatmap visualization
Clustering: Hierarchical clustering of rows and/or columns using specified distance metrics
Visualization: Creates publication-ready heatmaps with proper color schemes and annotations
For scaling methods:
z_score: Centers data around mean with unit variance (best for comparing relative changes)
min_max: Scales to 0-1 range (best for absolute comparisons)
robust: Uses median and MAD for outlier-resistant scaling
none: No scaling applied
The function automatically adjusts plot dimensions based on data size and uses optimized color palettes appropriate for the scaling method chosen (diverging palettes for z_score/robust, sequential palettes for min_max).