epi_plot_heatmap_triangle: Plot a triangle heatmap using ggplot2

View source: R/epi_plot_heatmap_triangle.R

epi_plot_heatmap_triangleR Documentation

Plot a triangle heatmap using ggplot2

Description

Plot correlations between numeric variables as a triangle heatmap using ggplot2. Prettier plot than a simple heatmap with only the lower triangle of a correlation matrix. Pass the correlation values from a melted (long) correlation matrix as input. Requires a data.frame with correlations and a data.frame with matching p-values. Column headers in both must be called Var1 and Var2. Use the output from epi_stats_corr_triangle() for example. cor_method is a string passed to the legend title It expects the name of the method used for correlation (eg 'Spearman')

Usage

epi_plot_heatmap_triangle(
  cormat_melted_triangle_r = NULL,
  cormat_melted_triangle_pval = NULL,
  cor_method = "Spearman",
  show_values = "pval"
)

Arguments

cormat_melted_triangle_r

a matrix object with correlation values Usually the output of episcout::epi_stats_corr_triangle().

cormat_melted_triangle_pval

a matrix object with correlation p-values Usually the output of episcout::epi_stats_corr_triangle().

cor_method

Correlation method used, will be printed in plot. Default is 'Spearman'

show_values

Values to show in plot, 'pval' or 'corr'. Default is 'pval'.

Value

Returns a heatmap as a ggplot2 object

Author(s)

Antonio Berlanga-Taylor <\url{https://github.com/AntonioJBT/episcout}>

See Also

epi_stats_corr, epi_stats_corr_triangle, epi_stats_corr_rename, epi_plot_heatmap

Examples


## Not run: 
n <- 100
df <- data.frame(var_id = rep(1:(n / 2), each = 2),
var_to_rep = rep(c("Pre", "Post"), n / 2),
x = rnorm(n),
y = rbinom(n, 1, 0.50),
z = rpois(n, 2)
)
df_corr <- df %>% select_if(~ epi_clean_cond_numeric(.))
df_corr <- df_corr[, -1] # exclude var_id
cormat_all <- epi_stats_corr(df_corr, method = 'pearson')
melted_triangles <- epi_stats_corr_triangle(cormat = cormat_all$cormat)
vars_list <- c('x', 'y', 'z')
var_labels <- c('numeric', 'binomial', 'poisson')
renamed_triangles <- epi_stats_corr_rename(melted_triangles$cormat_melted_triangle_r,
                                           melted_triangles$cormat_melted_triangle_pval,
                                           vars_list = vars_list,
                                           var_labels = var_labels
                                           )
library(ggplot2)
library(ggthemes)
epi_plot_heatmap(cormat_all$cormat_melted_r)
epi_plot_heatmap(renamed_triangles$cormat_melted_triangle_r)
epi_plot_heatmap(renamed_triangles$cormat_melted_triangle_pval)

epi_plot_heatmap_triangle(renamed_triangles$cormat_melted_triangle_r,
                          renamed_triangles$cormat_melted_triangle_pval,
                          show_values = 'pval'#'corr'
                          )
ggsave('epi_heatmap_triangle.svg',
       height = 12,
       width = 12,
       units = 'in'
       )


## End(Not run)

AntonioJBT/episcout documentation built on Dec. 1, 2024, 4:07 a.m.