### Install ggcorrheatmap from GitHub Source: https://leod123.github.io/ggcorrheatmap/index.html Install the development version of the ggcorrheatmap package from GitHub using devtools. ```r # install.packages("devtools") devtools::install_github("leod123/ggcorrheatmap") ``` -------------------------------- ### Install ggcorrheatmap from CRAN Source: https://leod123.github.io/ggcorrheatmap/index.html Install the stable version of the ggcorrheatmap package from CRAN. ```r install.packages("ggcorrheatmap") ``` -------------------------------- ### Basic gghm_tidy Example Source: https://leod123.github.io/ggcorrheatmap/reference/gghm_tidy.html Demonstrates the fundamental usage of gghm_tidy with a data frame in long format, specifying columns for rows, columns, and values. ```R set.seed(123) hm_in <- data.frame(row = rep(letters[1:10], each = 5), col = rep(LETTERS[1:5], 10), val = rnorm(50)) gghm_tidy(hm_in, row, col, val) ``` -------------------------------- ### Controlling Annotation Colors Source: https://leod123.github.io/ggcorrheatmap/articles/annotation.html Illustrates how to control annotation colors with multiple brewer or viridis palettes. This example uses an 8x8 matrix and 12 annotations per row/column. ```R # Make 8x8 matrix as some colour scales have a maximum of 8 possible colours mat_in <- matrix(1:64, nrow = 8, ncol = 8, byrow = TRUE) # Works without rownames or '.names' column as they are by default set to the same numbers here row_annot <- as.data.frame(replicate(12, letters[1:8])) col_annot <- as.data.frame(replicate(12, 1:8)) colnames(row_annot) <- colnames(col_annot) <- 1:12 gghm(mat_in, annot_rows_df = row_annot, annot_cols_df = col_annot, # Hide the legends legend_order = NA) ``` -------------------------------- ### ggcorrhm with Ladderize Dendrogram Ordering Source: https://leod123.github.io/ggcorrheatmap/articles/clustering.html Shows how to use `ladderize` for dendrogram ordering. This example highlights a potential warning when row and column orders differ, which can scramble the diagonal in square matrices. ```R # Only the rows are reordered, causing the matrix to become asymmetric # If the layout were triangular (or mixed), the warning would be slightly different # and the same plot as below would be produced (forced full layout). ggcorrhm(mtcars, cluster_rows = TRUE, cluster_cols = TRUE, dend_rows_extend = list(ladderize = NULL)) #> Warning: The clustering has ordered rows and columns differently. The diagonal may be #> scrambled due to the unequal row and column orders. ``` -------------------------------- ### Apply Different Binnings to Heatmap Triangles Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Customize binning for different heatmap triangles even when a single `col_scale` is provided. This example applies 4 bins to the top-right triangle and 8 bins to the bottom-left. ```r # Only one scale supplied, but it is split into two by # specifying two different binnings gghm(plt_dat, layout = c("tr", "bl"), mode = c("hm", "hm"), col_scale = "G", bins = c(4L, 8L)) ``` -------------------------------- ### Customizing Size Scales Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Applies different scales for element sizes using 'size_scale'. This example shows a binned size scale with absolute value transformation and a continuous size scale, each with specific ranges and legend configurations. ```R ggcorrhm(mtcars, layout = c("tr", "bl"), mode = c(23, 23), size_scale = list( # Can make a binned size scale scale_size_binned(n.breaks = 6, range = c(1, 5), # Absolute value transform but legend loses meaning transform = scales::trans_new("abs", abs, abs), name = "scale_size_binned", guide = guide_bins(order = 1)), scale_size_continuous(range = c(5, 10), name = "scale_size_continuous") ), border_lwd = 1, legend_order = NA) ``` -------------------------------- ### Heatmap with Custom Cell Labels and Formatting Source: https://leod123.github.io/ggcorrheatmap/articles/heatmap.html Displays custom labels within heatmap cells, controlling their size, color, and number of decimal places. This example demonstrates fine-tuning the appearance of cell labels. ```r plt1 <- gghm(plot_data, cell_labels = TRUE, col_scale = fill_scl, cell_label_size = 5, cell_label_col = "#63B8FF", cell_label_digits = 4) + labs(title = "size, colours, decimals") ``` -------------------------------- ### Arrange Legends with patchwork Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Demonstrates arranging legends using the patchwork package. It shows how to create a custom layout by combining a plot with multiple legends, including an example of horizontal legend orientation. ```R # Arrange in a grid (patchwork) # lgd_grid <- wrap_plots(lgd) # # # Combine with patchwork # wrap_plots(pl, lgd_grid, widths = c(3, 1)) ``` ```R # Try a different layout with patchwork. Turn the legends horizontal lgd <- get_legend(p + theme(legend.direction = "horizontal", legend.title = element_text(vjust = 0.8)))[["grobs"]][1:4] # Append the plot to the legend list to give to patchwork::wrap_plots wrap_plots(append(list(pl), lgd), design = " ##BBB## AAAAAAA AAAAAAA AAAAAAA AAAAAAA CCDDEEE ") ``` -------------------------------- ### Customize Scale Arguments Triangle-Wise Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Apply scale-related arguments like `limits`, `bins`, `high`, `mid`, `low`, and `midpoint` independently to each triangle using lists. This example demonstrates custom limits, bin counts, and color gradients for the bottom-right triangle. ```r # Make correlation data to plot (100x100) plt_dat <- sapply(seq(-1, 0, length.out = 100), function(x) { seq(x, x + 1, length.out = 100) }) # Using high, mid, low which only work if the default ggcorrhm scale is used ggcorrhm(plt_dat, cor_in = TRUE, show_names_diag = FALSE, border_col = 0, layout = c("tl", "br"), mode = c("hm", "hm"), # Some arguments can be NULL to use the default # If using NULL, use a list as it disappears in atomic vectors limits = list(NULL, c(-0.75, 0.75)), bins = list(NULL, 5L), high = list("pink", NULL), mid = list(NULL, NULL), low = c("cyan", "purple"), # midpoint cannot take NULL midpoint = c(-0.25, 0)) ``` -------------------------------- ### Split Diagonal in Heatmap Layouts Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Illustrates using 'split_diag = TRUE' to divide the diagonal of heatmap triangles. This example also shows how to customize diagonal label parameters and combine plots with different layouts. ```R plt1 <- ggcorrhm(mtcars, layout = "br", mode = "hm", split_diag = TRUE, names_diag_params = list(angle = -45, hjust = 1.2)) plt2 <- ggcorrhm(mtcars, layout = c("tr", "bl"), mode = c("hm", "hm"), col_scale = c("A", "G_rev"), split_diag = TRUE, # Move the names for visibility show_names_diag = FALSE, show_names_rows = TRUE, show_names_cols = TRUE) plt1 + plt2 ``` -------------------------------- ### Displaying P-values in Specific Heatmap Triangles Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Illustrates how to selectively display p-values and adjust cell label sizes for different triangles in a mixed layout. The first example applies p-values to the entire heatmap, while the second applies them only to the upper triangle. ```R plt1 <- ggcorrhm(mtcars, layout = c("tl", "br"), p_values = TRUE, cell_label_size = c(4, 2)) plt2 <- ggcorrhm(mtcars, layout = c("tl", "br"), p_values = c(FALSE, TRUE), cell_label_size = 2) plt1 + plt2 ``` -------------------------------- ### Faceting with Layout Options Source: https://leod123.github.io/ggcorrheatmap/articles/facets.html Demonstrates faceting with different layout options ('bl' for bottom-left, 'br' for bottom-right). Row counting is from the bottom when the y-axis is flipped. ```R # Two from the top plt1 <- ggcorrhm(mtcars, split_rows = 2, split_cols = 2, layout = "bl") # Two from the bottom plt2 <- ggcorrhm(mtcars, split_rows = 2, split_cols = 2, layout = "br") plt1 + plt2 ``` -------------------------------- ### Create custom labels with adjusted p-values Source: https://leod123.github.io/ggcorrheatmap/articles/tidy_input.html Mutate the data to create custom labels for the heatmap. This example shows how to display adjusted p-values in the top right and unadjusted in the bottom left, only if they are less than 0.05. ```r plt_dat %>% # Make custom labels with adjusted p-values in the # top right and unadjusted in the bottom left # but only display if < 0.05 mutate(labs = case_when(layout == "tr" & p_adj < 0.05 ~ round(p_adj, 2), layout == "bl" & p_val < 0.05 ~ round(p_val, 2), TRUE ~ NA)) %>% ggcorrhm_tidy(row, col, value, labels = labs, cor_in = TRUE) ``` -------------------------------- ### Comparing Full and Mixed Layouts Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Compares the default full layout ('f') with a mixed layout ('tl', 'br') that achieves a similar top-left to bottom-right diagonal. This highlights how mixed layouts can replicate or modify default arrangements. ```R plt1 <- ggcorrhm(mtcars, layout = "f") plt2 <- ggcorrhm(mtcars, layout = c("tl", "br"), mode = c("hm", "hm")) plt1 + plt2 ``` -------------------------------- ### Load Libraries Source: https://leod123.github.io/ggcorrheatmap/articles/facets.html Load the necessary libraries for ggcorrheatmap, ggplot2, and patchwork. ```r library(ggcorrheatmap) library(ggplot2) library(patchwork) ``` -------------------------------- ### Load Libraries Source: https://leod123.github.io/ggcorrheatmap/articles/annotation.html Loads necessary libraries for ggcorrheatmap, dplyr, ggplot2, and patchwork. ```R library(ggcorrheatmap) library(dplyr) library(ggplot2) library(patchwork) ``` -------------------------------- ### Highlighting Non-Significant P-values with ggplot2 Source: https://leod123.github.io/ggcorrheatmap/articles/correlation.html Customize a correlation heatmap by highlighting non-significant p-values using ggplot2. This example retrieves plot data, filters for p-values above a threshold, and marks them with a specific shape and size. ```R p <- ggcorrhm(mtcars, p_values = TRUE, p_thresholds = NULL, p_adjust = "bonferroni", return_data = TRUE, layout = "tl", include_diag = FALSE) ggcorrhm(mtcars, layout = "tl", include_diag = FALSE) + geom_point(data = p$plot_data %>% # Mark cells above a threshold filter(p_adj >= 0.05, as.character(row) != as.character(col)), mapping = aes(x = col, y = row), shape = 4, size = 8) ``` -------------------------------- ### Customize Continuous and Binned Scales with Scale Objects Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Illustrates customizing breaks in continuous and binned scales using scale objects like scale_fill_gradient2 and scale_fill_steps2. ```R plt_dat_cor <- sapply(seq(-1, 0, length.out = 10), function(x) { seq(x, x + 1, length.out = 10) }) plt1 <- ggcorrhm(plt_dat_cor, cor_in = TRUE) plt2 <- ggcorrhm(plt_dat_cor, cor_in = TRUE, layout = c("tr", "bl"), mode = c("hm", "hm"), col_scale = list( scale_fill_gradient2( # Mimic the default correlation scale high = "sienna2", mid = "white", low = "skyblue2", # If not binned this only changes the legend but not the colours breaks = seq(-1, 1, length.out = 11) ), # Binned scales can be made with e.g. # scale_fill_fermenter, scale_fill_viridis_b, scale_fill_steps etc scale_fill_steps2( high = "sienna2", mid = "white", low = "skyblue2", # Specify exact break points for bins breaks = c(-1, -0.75, -0.25, 0, 0.25, 0.75, 1) ) )) plt1 + plt2 ``` -------------------------------- ### Apply Single Scale to Mixed Layout Heatmap Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html When using a mixed layout with different modes (e.g., heatmap and text), a single value for `col_scale` applies the same scale to all heatmap components. This example uses 'A' for the scale. ```r plt_dat <- sapply(1:10, function(x) seq(x, x + 9)) gghm(plt_dat, layout = c("tl", "br"), mode = c("hm", "text"), col_scale = "A") ``` -------------------------------- ### Clustering in Mixed Layouts Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Shows how clustering can be applied to mixed layout heatmaps. Clustering is only possible if both rows and columns are clustered, as demonstrated with the 'cluster_rows' argument. ```r ggcorrhm(mtcars, layout = c("tr", "bl"), cluster_rows = TRUE) #> Warning: Cannot cluster only one dimension for triangular layouts, clustering both rows #> and columns instead. ``` -------------------------------- ### Apply Different Scales to Heatmap Triangles Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Use a vector or list for `col_scale` to apply distinct color scales to different triangles in a mixed layout heatmap. This example applies 'magma' to the top-left and 'RdYlGn' to the bottom-right. ```r plt_dat <- sapply(1:10, function(x) seq(x, x + 9)) gghm(plt_dat, layout = c("tl", "br"), mode = c("hm", "hm"), # For Viridis options, both the short and long versions work col_scale = c("magma", "RdYlGn")) ``` -------------------------------- ### Basic Mixed Layouts Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Creates basic mixed layouts by specifying different modes for the upper and lower triangles of the heatmap. The 'layout' argument takes a vector of two strings, and 'mode' can specify different display types for each triangle. ```r plt1 <- ggcorrhm(mtcars, layout = c("tl", "br")) plt2 <- ggcorrhm(mtcars, layout = c("tl", "br"), mode = c("21", "23")) plt1 + plt2 ``` -------------------------------- ### Custom P-value Marking with ggplot2 Source: https://leod123.github.io/ggcorrheatmap/articles/correlation.html Generate custom p-value markings by returning plot data from ggcorrhm and manipulating it with ggplot2 functions. This example customizes border line types, widths, and uses geom_point for significant p-values. ```R p <- ggcorrhm(mtcars, p_values = TRUE, p_thresholds = NULL, p_adjust = "bonferroni", return_data = TRUE, layout = "br", include_diag = FALSE) p$plot_data <- mutate(p$plot_data, p_lty = case_when(p_adj < 0.05 ~ 1, TRUE ~ 3), p_lwd = case_when(p_adj < 0.05 ~ 1, TRUE ~ 0.5), p_size = -log10(p_adj)) ggcorrhm(mtcars, border_lty = p$plot_data$p_lty, border_lwd = p$plot_data$p_lwd, layout = "br", include_diag = FALSE) + geom_point(aes(x = col, y = row, size = p_size), data = p$plot_data %>% filter(p_adj < 0.05, as.character(row) != as.character(col)), shape = "*", size = 8) + scale_size(range = c(4, 8)) + labs(size = "-log10(p_adj)") ``` -------------------------------- ### Load Required Libraries Source: https://leod123.github.io/ggcorrheatmap/articles/correlation.html Load the necessary libraries for creating correlation heatmaps and data manipulation. ```R library(ggcorrheatmap) library(ggplot2) library(dplyr) library(patchwork) ``` -------------------------------- ### Correlate two matrices using cor_long Source: https://leod123.github.io/ggcorrheatmap/reference/cor_long.html This example illustrates how to use cor_long to calculate correlations between two different data frames. Both data frames must be provided along with their respective row, column, and value specifications. ```R # Correlation between two matrices cor_in2 <- data.frame(rows = rep(letters[1:10], each = 10), cols = rep(letters[1:10], 10), values = rnorm(100)) corr2 <- cor_long(cor_in, row, col, val, cor_in2, rows, cols, values) ``` -------------------------------- ### Customizing Column Annotations Source: https://leod123.github.io/ggcorrheatmap/articles/annotation.html Demonstrates how to apply custom color scales and themes to column annotations based on data types. Use `annot_cols_df` for annotation data and `annot_cols_col` for styling. ```r col_annot <- as.data.frame(replicate(8, 1:8)) %>% rename_with(function(x) {paste(c("custom", "default"), rep(1:4, each = 2), sep = "")}) %>% mutate(across(1:4, as.character)) col_annot$.names <- colnames(mtcars)[1:8] col_annot_fill <- list(custom1 = "Oranges", custom2 = scale_fill_hue(guide = "none"), custom3 = "mako", custom4 = scale_fill_gradient(guide = "none")) ggcorrhm(mtcars[, 1:8], annot_cols_df = col_annot, annot_cols_col = col_annot_fill, legend_order = NA) ``` -------------------------------- ### Correlate long format data to long format matrix Source: https://leod123.github.io/ggcorrheatmap/reference/cor_long.html This example shows how to obtain a long-format output from the cor_long function by specifying `out_format = "long"`. This is useful for subsequent plotting or processing in a tidy format. ```R # Long format output corr_long <- cor_long(cor_in, row, col, val, out_format = "long") ``` -------------------------------- ### Load necessary libraries Source: https://leod123.github.io/ggcorrheatmap/articles/clustering.html Load the required libraries for ggcorrheatmap, data manipulation, dendrograms, and plot arrangement. ```r library(ggcorrheatmap) library(dplyr) library(dendextend) library(patchwork) ``` -------------------------------- ### Using NULL for Default Colour Scale Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Demonstrates using NULL for one of the colour scales, which reverts to the default scale customizable with 'high', 'mid', and 'low' arguments. The other scale is a custom distiller scale. ```R # NULL uses the default that changes with high, mid, low etc ggcorrhm(mtcars, layout = c("tl", "br"), mode = c("hm", "hm"), col_scale = list( NULL, # Scale object will not be affected by the default scale arguments scale_fill_distiller(palette = "PRGn", limits = c(-1, 1)) ), bins = 5L, high = "pink", mid = "beige", low = "lightblue") ``` -------------------------------- ### Custom Scales with ggplot2 Objects Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Applies custom ggplot2 scale objects to specific legends within `gghm`, overriding default behavior and `legend_order`. This example customizes color scales for triangles and annotations, and hides one annotation legend. ```R gghm(cor(mtcars), layout = c("tl", "br"), mode = c(19, 21), col_name = c("1", "3"), size_name = "2", col_scale = list( # Keep default scale for first triangle NULL, # Pass a scale object for second triangle scale_fill_viridis_c(name = "scale object 1", option = "G", guide = guide_colourbar(order = 1)) ), annot_rows_df = annot_rows, annot_cols_df = annot_cols, annot_rows_col = list( # Scale object for second row annotation "5" = scale_fill_distiller(name = "scale object 2", palette = "Purples") ), annot_cols_col = list( # Scale object for first column annotation "6" = scale_fill_brewer(name = "scale object 3", palette = "RdBu", # Hide the legend guide = "none") ), # (Try to) Remove all legends legend_order = NA) + theme(legend.direction = "horizontal") ``` -------------------------------- ### Faceting with Tidy Data in gghm_tidy Source: https://leod123.github.io/ggcorrheatmap/articles/tidy_input.html Demonstrates using facet_rows and facet_cols arguments in gghm_tidy to create faceted heatmaps based on annotation columns. It also shows an alternative using split_rows and split_cols. ```r plt1 <- mtcars_long %>% # Use the annotations as facets left_join(row_key, by = "cars") %>% left_join(col_key, by = "vars") %>% # Change facet order for rows mutate(b = factor(b, levels = letters[1:3])) %>% gghm_tidy(cars, vars, vals, facet_rows = b, facet_cols = d) plt2 <- gghm_tidy(mtcars_long, cars, vars, vals, split_rows = 16, split_cols = 6) plt1 + plt2 ``` -------------------------------- ### ggcorrhm with Row and Column Clustering Source: https://leod123.github.io/ggcorrheatmap/reference/ggcorrhm.html Demonstrates enabling clustering for both rows and columns in the heatmap. ```R ggcorrhm(mtcars, layout = "tl", cluster_rows = TRUE, cluster_cols = TRUE) ``` -------------------------------- ### Load Required Libraries Source: https://leod123.github.io/ggcorrheatmap/articles/tidy_input.html Load the necessary libraries for data manipulation and ggcorrheatmap functionality. ```R library(ggcorrheatmap) library(dplyr) library(tidyr) # For converting between long and wide formats library(tibble) library(patchwork) ``` -------------------------------- ### Correlate long format data to wide format matrix Source: https://leod123.github.io/ggcorrheatmap/reference/cor_long.html This example demonstrates the default usage of cor_long to create a wide-format correlation matrix from a long-format data frame. Ensure the input data frame is properly structured with columns for rows, columns, and values. ```R set.seed(123) cor_in <- data.frame(row = rep(letters[1:10], each = 5), col = rep(LETTERS[1:5], 10), val = rnorm(50)) # Wide format output (default) corr_wide <- cor_long(cor_in, row, col, val) ``` -------------------------------- ### Customizing Heatmap Triangles with Vectors and Lists Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Demonstrates using vectors and lists to control border linewidth, line style, and colors for different triangles in a mixed layout heatmap. It also shows how to conditionally display and color cell labels based on triangle. ```R colr <- c("#70369D", "#59369D", "#4A44AB", "#486ED8", "#5B9892", "#79C314", "#C6DB28", "#FBDD2B", "#FEB30A", "#F56B08", "#E81416") ggcorrhm(mtcars, layout = c("bl", "tr"), mode = c("none", "hm"), # One value for the whole heatmap (border linewidth) border_lwd = 1.5, # Length two vector, each value being used for the whole corresponding triangle border_lty = c(1, 3), # A list with values for each triangle border_col = list( # The first triangle is 66 values (if the diagonal is drawn), second 55 # If not known, can return the plotting data to see how many cells are in each triangle # Since the matrix must be square the numbers can also be calculated # Total: ncol*ncol, triangle without diagonal: (total - ncol) / 2, with diagonal: without + ncol # First triangle, provide all values, moving down each column from the left rev(unlist(sapply(seq_along(colr), function(i) rep(colr[i], i)))), # Second triangle, recycle one value "white" ), # Also draw cell labels on the first triangle and set the same colours as the cells cell_labels = c(TRUE, FALSE), cell_label_col = list( # Provide the same values as for the cell border colours, but minus one as the diagonal # names are written instead of cell labels and are controlled differently rev(unlist(sapply(seq_along(colr), function(i) rep(colr[i], i - 1)))), 1 # Anything as there are no labels ), names_diag_param = list(colour = rev(colr))) ``` -------------------------------- ### gghm_tidy with Annotation and Clustering Source: https://leod123.github.io/ggcorrheatmap/reference/gghm_tidy.html Shows how to add row and column annotations and enable clustering for both rows and columns using gghm_tidy. Column annotations can be specified using tidy selection. ```R set.seed(123) hm_in <- data.frame(row = rep(letters[1:10], each = 5), col = rep(LETTERS[1:5], 10), val = rnorm(50)) # Add annotation by giving names of columns in the data hm_in$row_annot1 <- rep(1:10, each = 5) hm_in$row_annot2 <- rep(10:1, each = 5) hm_in$col_annot <- rep(letters[1:5], 10) # Columns are given using 'tidy' selection # so they can be unquoted, quoted, from variables (with !! notation) or indices gghm_tidy(hm_in, row, col, val, annot_rows = c(row_annot1, row_annot2), annot_cols = col_annot, cluster_rows = TRUE, cluster_cols = TRUE) ``` -------------------------------- ### Basic Row and Column Annotation Source: https://leod123.github.io/ggcorrheatmap/articles/annotation.html Demonstrates adding row and column annotations using data frames. Ensure row names or a '.names' column match the heatmap data. ```R row_annot <- data.frame(.names = colnames(mtcars), a = rnorm(ncol(mtcars))) col_annot <- data.frame(b = sample(letters[1:3], ncol(mtcars), TRUE)) rownames(col_annot) <- colnames(mtcars) ggcorrhm(mtcars, annot_rows_df = row_annot, annot_cols_df = col_annot) ``` -------------------------------- ### Create Mixed Layouts and Scales with ggcorrhm Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Generates a simple mixed layout with mixed scales and a more complex version with various scale arguments like bins and limits. ```R # Make plot data between -1 and 1 (like correlation data) plt_dat_cor <- sapply(seq(-1, 0, length.out = 100), function(x) { seq(x, x + 1, length.out = 100) }) # Simple mixed layout with mixed scales plt1 <- ggcorrhm(plt_dat_cor, cor_in = TRUE, show_names_diag = FALSE, border_col = 0, layout = c("tl", "br"), mode = c("hm", "hm"), col_scale = c("Spectral", "H")) # With many scale arguments applied plt2 <- ggcorrhm(plt_dat_cor, cor_in = TRUE, show_names_diag = FALSE, border_col = 0, layout = c("tl", "br"), mode = c("hm", "hm"), col_scale = c("Spectral", "H"), # Provide a list as a mix of integer and double would be coerced if atomic vector # Integer vs double, results in 11 and 10 bins bins = list(11L, 11), # NULL to use default limits (-1 to 1 in ggcorrhm) limits = list(c(-.5, .5), NULL)) plt1 + plt2 ``` -------------------------------- ### ggcorrhm with Mixed Layouts Source: https://leod123.github.io/ggcorrheatmap/reference/ggcorrhm.html Demonstrates using a mixed layout for different parts of the heatmap, specified as a character vector. ```R ggcorrhm(mtcars, layout = c("tl", "br")) ``` -------------------------------- ### ggcorrheatmap with Dendrogram Extension Arguments Source: https://leod123.github.io/ggcorrheatmap/reference/gghm.html Shows how to use `dend_*_extend` arguments to extend dendrograms, including setting branch line types and highlighting branches. ```r # Using the dend_*_extend arguments gghm(scale(hm_in), cluster_rows = TRUE, dend_rows_extend = list("set" = list("branches_lty", c(1, 2, 3)), # Empty list element (or NULL) if no arguments to be given "highlight_branches_col" = list())) ``` -------------------------------- ### Basic ggcorrheatmap Usage Source: https://leod123.github.io/ggcorrheatmap/reference/gghm.html Demonstrates the most basic usage of the ggcorrheatmap function with a subset of the mtcars dataset. ```r library(ggplot2) # Use part of the mtcars data (for visibility) hm_in <- mtcars[1:15, ] # Basic usage gghm(hm_in) ``` -------------------------------- ### Creating a Tidy Correlation Heatmap with Annotations Source: https://leod123.github.io/ggcorrheatmap/articles/tidy_input.html This snippet shows how to prepare data with row and column annotations and then generate a heatmap using gghm_tidy. Ensure annotations are unique to avoid conflicts. ```r set.seed(123) row_key <- data.frame(cars = rownames(mtcars), a = 1:nrow(mtcars), b = sample(letters[1:3], nrow(mtcars), TRUE)) col_key <- data.frame(vars = colnames(mtcars), c = 1:ncol(mtcars), d = sample(LETTERS[1:2], ncol(mtcars), TRUE)) mtcars_long %>% # Add the row and column annotations left_join(row_key, by = "cars") %>% left_join(col_key, by = "vars") %>% # Add labels mutate(labs = 1:nrow(.)) %>% gghm_tidy(cars, vars, vals, labels = labs, annot_rows = c(a, b), annot_col = c(c, d), # gghm arguments legend_order = 1, cell_label_col = "white", cell_label_digits = 0, col_scale = "A", scale_data = "col", cell_label_size = 2, cluster_rows = TRUE, cluster_cols = TRUE) ``` -------------------------------- ### Reverse Color Scales for Heatmaps and Annotations Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Demonstrates reversing the color scale for both the main heatmap and row annotations using 'rev_' prefix for the heatmap and '_rev' suffix for the annotation scale character. Annotation size and distance are adjusted for better visibility. ```r row_annot <- data.frame("Default" = 1:nrow(volcano), "Reversed" = 1:nrow(volcano)) row_annot_colr <- list("Default" = "A", "Reversed" = "A_rev") gghm(volcano, show_names_rows = FALSE, show_names_cols = FALSE, border_col = NA, col_scale = "rev_A", col_name = "Reversed", annot_rows_df = row_annot, annot_rows_col = row_annot_colr, # Make the annotation larger for visibility annot_size = 3, annot_dist = 1.5) + ``` -------------------------------- ### Creating Facet Membership Vectors Source: https://leod123.github.io/ggcorrheatmap/articles/facets.html Generates ordered and unordered facet membership vectors for rows and columns using `sample()`. This is useful for creating custom faceting based on arbitrary groupings. ```R set.seed(123) row_facets <- sample(c("AAA", "BB", "C"), 32, TRUE) col_facets <- sample(c(123, 456), 11, TRUE) plt1 <- gghm(mtcars, legend_order = NA, split_rows = row_facets, split_cols = col_facets) ``` -------------------------------- ### Return Data with Layout Information Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Illustrates how to retrieve the underlying data used for plotting, including a 'layout' column that indicates which triangle each cell belongs to when using mixed layouts. This is enabled by setting 'return_data = TRUE'. ```r head(ggcorrhm(mtcars, layout = c("tl", "br"), return_data = TRUE)$plot_data) #> row col value layout #> 1 mpg mpg 1.0000000 tl #> 2 cyl mpg -0.8521620 tl #> 3 disp mpg -0.8475514 tl #> 4 hp mpg -0.7761684 tl #> 5 drat mpg 0.6811719 tl #> 6 wt mpg -0.8676594 tl ``` -------------------------------- ### ggcorrheatmap with Symmetric Matrix Layout Source: https://leod123.github.io/ggcorrheatmap/reference/gghm.html Shows how to use ggcorrheatmap with a symmetric correlation matrix and specifies a 'tl' (top-left) layout. ```r # Different layout (using a symmetric matrix) gghm(cor(mtcars), layout = "tl") ``` -------------------------------- ### Mixed Layouts for Annotations and Dendrograms Source: https://leod123.github.io/ggcorrheatmap/articles/mixed.html Demonstrates using mixed layouts ('hm', 'none') to place annotations and dendrograms on the empty sides of triangular layouts. Cell borders can be selectively hidden in specific triangles. ```R annot <- data.frame(.names = colnames(mtcars), a = 1:11) ggcorrhm(mtcars, layout = c("tl", "br"), mode = c("hm", "none"), annot_rows_df = annot, annot_cols_df = annot, cluster_rows = TRUE, cluster_cols = TRUE, # Hide cell borders only in the 'none' triangle border_lwd = c(0.1, 0)) ``` -------------------------------- ### Heatmap with Clustering, Scaling, and Annotations Source: https://leod123.github.io/ggcorrheatmap/articles/heatmap.html Creates a heatmap with row clustering enabled, column data scaled, and row annotations displayed on the top. This combines several customization options for a more informative visualization. ```r gghm(mtcars[1:15, ], cluster_rows = TRUE, scale_data = "col", annot_rows_df = row_annot, annot_rows_names_side = "top") ``` -------------------------------- ### ggcorrhm with Row and Column Annotations Source: https://leod123.github.io/ggcorrheatmap/reference/ggcorrhm.html Combines row and column annotations with clustering and a full layout. Note how specific annotation columns can be selected. ```R ggcorrhm(mtcars, layout = "full", cluster_rows = TRUE, cluster_cols = TRUE, annot_rows_df = annot[, -3], annot_cols_df = annot[, -2]) ``` -------------------------------- ### Create Basic Heatmap with Row Annotations Source: https://leod123.github.io/ggcorrheatmap/articles/heatmap.html Generates a basic heatmap using the first 15 rows of the mtcars dataset and adds row annotations. The annotation labels are placed on the top. ```r row_annot <- data.frame(.names = rownames(mtcars)[1:15], a = sample(letters[1:4], 15, TRUE)) gghm(mtcars[1:15, ], annot_rows_df = row_annot, annot_rows_names_side = "top") ``` -------------------------------- ### ggcorrhm with Different Layout Source: https://leod123.github.io/ggcorrheatmap/reference/ggcorrhm.html Illustrates changing the layout of the correlation heatmap using the 'layout' argument. ```R ggcorrhm(mtcars, layout = "br") ``` -------------------------------- ### Basic row and column clustering Source: https://leod123.github.io/ggcorrheatmap/articles/clustering.html Enable clustering for rows and columns by setting `cluster_rows` and `cluster_cols` to TRUE. Distance measure and clustering method can be specified. ```r ggcorrhm(mtcars, cluster_rows = TRUE, cluster_cols = TRUE) ``` -------------------------------- ### ggcorrheatmap with Gaps for Splitting Source: https://leod123.github.io/ggcorrheatmap/reference/gghm.html Illustrates adding gaps to split rows and columns in the heatmap, useful for visualizing distinct groups. ```r # With gaps gghm(mtcars, split_rows = c(10, 20), split_cols = 6) ``` -------------------------------- ### ggcorrhm with Two Matrices Source: https://leod123.github.io/ggcorrheatmap/reference/ggcorrhm.html Shows how to use ggcorrhm with two different data matrices. ```R ggcorrhm(iris[1:32, -5], mtcars) ``` -------------------------------- ### Generate General Heatmap Source: https://leod123.github.io/ggcorrheatmap/index.html Create a general heatmap with clustered rows and columns. Data is scaled by column. ```r gghm(mtcars, scale_data = "col", cluster_rows = TRUE, cluster_cols = TRUE) ``` -------------------------------- ### Handle Out-of-Bounds Values with Color Scales Source: https://leod123.github.io/ggcorrheatmap/articles/legends.html Demonstrates using scale_fill_distiller with limits and na.value to handle out-of-bounds values, and the 'squish' strategy for out-of-bounds values in binned scales. ```R plt2 <- gghm(plt_dat, layout = c("tr", "bl"), mode = c("hm", "hm"), col_scale = list( # scale_fill_distiller to use Brewer palettes with continuous data # The default direction in distiller is -1, opposite of gghm scale_fill_distiller( palette = "RdYlGn", # Values outside the limits are replaced with NAs limits = c(7, 13), # Can then change the colour using the na.value argument # (corresponding to na_col in gghm) na.value = "beige" ), scale_fill_distiller( palette = "RdYlGn", limits = c(7, 13), # Change oob strategy 'oob' argument # scales::squish assigns the extreme colours to values outside the range oob = scales::squish ) ), cell_labels = TRUE) plt1 + plt2 ``` -------------------------------- ### Generate Mixed Layout Correlation Heatmap Source: https://leod123.github.io/ggcorrheatmap/index.html Create a mixed layout correlation heatmap displaying correlation values and p-values in different triangles. Uses a simplified layout specification. ```r # With correlation values and p-values ggcorrhm(mtcars, layout = c("topright", "bottomleft"), cell_labels = c(FALSE, TRUE), p_values = c(FALSE, TRUE)) ``` -------------------------------- ### Apply Triangular Layouts to Square Heatmaps Source: https://leod123.github.io/ggcorrheatmap/articles/correlation.html Generate triangular correlation heatmaps for square matrices using the 'layout' argument. Options include 'topleft', 'topright', 'bottomleft', and 'bottomright'. ```R plt1 <- ggcorrhm(mtcars, layout = "topleft") ```