cor_plot: Plot correlations as a heatmap with significance stars and...

View source: R/cor_plot.R

cor_plotR Documentation

Plot correlations as a heatmap with significance stars and confidence intervals

Description

Plots correlations between multiple variables as a heatmap. This function fulfills a similar need to psych::cor.plot() but allows for different values on the x and y axis. It also simplifies a lot of stuff that is annoying about the function in the psych package.

Usage

cor_plot(
  data,
  x,
  y = NULL,
  use = "complete.obs",
  method = "pearson",
  digits = 3,
  digit_size = 4,
  significance = T,
  sign_levels = list(`.001` = "***", `.01` = "**", `.05` = "*", `.1` = "."),
  return_significant_only = F,
  CI = T,
  CI_conf_level = 0.95,
  plot_CI = T,
  colorblind = F,
  title = NULL,
  label_rotation = 0,
  return_plot = F
)

Arguments

data

The dataframe from which the data will be sampled

x

Independent variables, provided as a vector containing variable names as strings, e.g. c("iv1", "iv2", "iv3").

y

Dependent variables, optional. Also provided as a vector. If no y values are specified, the function uses the x values for both the x and y axis.

use

Which values to use for the correlation. Can be any option for the 'use' command of the 'stats::cor' function, i.e. "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs". Defaults to "complete.obs".

method

Which correlation method to use. Can be any option for the 'method' command of the 'stats::cor' function, i.e. "pearson" "kendall", or "spearman". Defaults to "pearson".

digits

How many digits should be shown in the plot. Defaults to 3.

digit_size

How big the digits within each tile should be. Defaults to 4, but will probably need to be adjusted depending on the number of variables

significance

Whether significance of correlation should be computed. Significance will be included both in the table as well as in the plot. Right now, computation of significance is done using the t-distribution, but not accounting for multiple comparisons. Will be implemented in the future as an optional parameter.

sign_levels

A list consisting of the significance levels as names and significance signs (e.g. "***") as values. Names must be provided as strings and can have a leading zero (but don't have to), i.e. can be in form of e.g. ".001" or "0.001". Significance signs must also be provided as strings. Defaults to .001 = three stars, .01 = two stars, .05 = one star

return_significant_only

Whether the returned values (table, not the plot) should only contain the correlations that are significant. Defaults to FALSE. If set to TRUE, will return any correlation with a p value smaller than the largest supplied significance level. For example, if sign_levels is not modified, any correlation with p < .1 will be returned. Only works if significance is set to TRUE.

CI

Whether the CI for each correlation should be reported in the table. Defaults to TRUE.

CI_conf_level

Confidence level used for the CI. Defaults to 0.95.

plot_CI

Whether the CI should be included below the correlation within the plot.

colorblind

Whether the colors in the plot should be modified to a colorblind palette. Defaults to FALSE. Uses colors from the palette colorblind_1 in this package if TRUE.

title

Title that should be added to the plot. If not specified, title will be omitted.

label_rotation

How many degrees the label on the x axis (corresponds to the values specified as 'y' if y != NULL) should be rotated.

return_plot

Whether the plot should be returned. If TRUE, return value will be a list containing the table and the plot, the plot will not be printed. # Can e.g. be used to pass the plot to plotly, which is helpful for large tables.

Value

Returns the correlation table and plots it as a heatmap. If return_plot is TRUE, also returns the ggplot object as a list element together with the correlation table. Can be called using print(object$cor_plot)


daaawit/ABOHiwi documentation built on March 23, 2022, 3:27 p.m.