plot_correlations: plot_correlations

View source: R/plot_correlations.R

plot_correlationsR Documentation

plot_correlations

Description

Function computes simple Pearson correlations between variables of a data frame/matrix along with a heatmap plot.

The data frame/matrix should have observations along the rows without NA values and the columns of variables are assumed to be numeric. The ggplot2 plot object takes the form of a color coded heatmap of the upper triangular portion of a symmetric correlation matrix.

Usage

plot_correlations(
  x = NULL,
  create_plot = TRUE,
  cluster_corr = FALSE,
  tile_border_color = "white",
  label_sz = 11,
  label_color = "black",
  label_fontface = "plain",
  label_alpha = 1,
  title = NULL,
  subtitle = NULL,
  center_titles = FALSE,
  axis_text_size = 11,
  rot_x_tic_angle = 40,
  scale_limits = c(-1, 1),
  scale_colors = c("blue", "green"),
  mid_scale_color = "white",
  legend_title = ggplot2::waiver(),
  show_legend = TRUE
)

Arguments

x

A numeric data frame or matrix with variable columns and their observational values along the rows.

create_plot

A logical that if TRUE will create a heatmap plot of the correlations.

cluster_corr

A logical that if TRUE will cluster the correlations across the heatmap scales.

tile_border_color

A numeric that sets the heatmap tile's border color.

label_sz

A numeric that sets the size of the label.

label_color

A string that sets the label's color.

label_fontface

A string that sets the label's font face. Acceptable values are "plain", "bold", "italic", "bold.italic".

label_alpha

A numeric that sets the label's alpha value.

title

A string that sets the plot title.

subtitle

A string that sets the plot subtitle.

center_titles

A logical which if TRUE centers both the 'title' and 'subtitle'.

axis_text_size

A numeric that sets the font size along the axis'. Default is 11.

rot_x_tic_angle

A numeric that sets the angle of the x axis tic labels.

scale_limits

A numeric vector of two elements giving the min/max values for the correlation scale. The default is c(-1.0, 1.0)

scale_colors

A character vector of two elements giving the low/high colors that are interpolated across 'scale_limits'.

mid_scale_color

A character string that sets the color for the mid value of 'scale_limits'.

legend_title

A character string that sets the legend's title.

show_legend

A logical that controls the appearance of the legend.

Value

A named list with a data frame of correlations between the variables and a ggplot2 plot object.

Examples

library(ggplot2)
library(data.table)
library(RplotterPkg)
library(RregressPkg)

midwest_dt <- data.table::as.data.table(ggplot2::midwest)  |>
_[, .(popdensity, percwhite, percblack, popadults, perchsd,
      percollege, percprof, percbelowpoverty, percchildbelowpovert,
      percadultpoverty, percelderlypoverty, inmetro)
]

midwest_corr_lst <- RregressPkg::plot_correlations(
  x = midwest_dt,
  title = "Correlations of Midwest Demographic Variables",
  subtitle = "Source: ggplot2::midwest",
  label_sz = 6
)
a_plot <- midwest_corr_lst$corr_plot


deandevl/RregressPkg documentation built on Feb. 5, 2025, 12:11 p.m.