create_density_ridge_plot: create_density_ridge_plot

View source: R/create_density_ridge_plot.R

create_density_ridge_plotR Documentation

create_density_ridge_plot

Description

Function creates ggplot2 based plots stacked vertically (also known as ridge or raincloud plots). The function's density arguments mirror most of the arguments available from density for the Kernel Density Estimation (KDE). See the density help page for more information.

Usage

create_density_ridge_plot(
  df = NULL,
  variables = NULL,
  plot_heights = 3.5,
  plot_widths = 24,
  bw = "nrd0",
  adjust = 1,
  kernel = "gaussian",
  n = 512,
  na.rm = TRUE,
  title = NULL,
  title_fontsz = 14,
  x_title = NULL,
  rot_x_tic_angle = 0,
  density_linewdth = 1,
  density_color = "black",
  density_fill = "gray",
  density_alpha = 0.4,
  x_limits = NULL,
  x_major_breaks = waiver(),
  x_minor_breaks = waiver(),
  x_labels = waiver(),
  y_limits = NULL,
  y_major_breaks = waiver(),
  y_minor_breaks = waiver(),
  y_show_axis = FALSE,
  axis_text_size = 11
)

Arguments

df

The source data frame from which the densities are plotted.

variables

A required string vector that names the x axis variables from 'df' for plotting their densities.

plot_heights

A numeric that sets the plot height in centimeters for each variable in 'variables'. The default is 3.5 centimeters in height for each plot.

plot_widths

A numeric that sets the plot width in centimeters for each variable in 'variables'. The default is 24 centimeters in width for each plot.

bw

A string or numeric that sets the smoothing bandwidth to be used with the KDE function.

adjust

A numeric that adjusts 'bw' since the actual bandwidth is computed as adjust*bw.

kernel

A string that set the type of Kernel Density Estimation (KDE). Acceptable values are "gaussian", "rectangular", "triangular", "epanechnikov", "biweight", "cosine" or "optcosine".

n

The number of equally spaced points at which the density is to be estimated. This should be a power of two.

na.rm

A logical which if TRUE, missing values are removed from 'df'. If FALSE any missing values cause an error.

title

A string that sets the overall title.

title_fontsz

A numeric that sets the title's font size. The default is 14.

x_title

A string that sets the x axis title. If NULL (the default) then the x axis title does not appear.

rot_x_tic_angle

A numeric that sets the angle of rotation for the x tic labels. When x tic labels are long, a value of 40 for this argument usually works well.

density_linewdth

A numeric that sets the density line width.

density_color

A string that sets the color for the density line.

density_fill

A string that sets the color for the density fill.

density_alpha

A numeric that sets the alpha value for 'density_fill'.

x_limits

A numeric 2 element vector or function that sets the minimum and maximum for the x axis. Use NA to refer to the existing minimum and maximum.

x_major_breaks

A numeric vector or function that sets the major tic locations along the x axis.

x_minor_breaks

A numeric vector or function that sets the minor tic locations along the x axis.

x_labels

A character vector or function giving x axis tic labels. Must be the same length as 'x_major_breaks'.

y_limits

A numeric 2 element vector or function that sets the minimum and maximum for the y axis. Use NA to refer to the existing minimum and maximum.

y_major_breaks

A numeric vector or function that sets the major tic locations along the y axis.

y_minor_breaks

A numeric vector or function that sets the minor tic locations along the y axis.

y_show_axis

A logical which if TRUE will display the y-axis density for each variable.

axis_text_size

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

Value

A ggplot class object.

References

Micah Allen,Davide Poggiali,Kirstie Whitaker,Tom Rhys Marshall, Jordy van Langen,Rogier A. Kievit (2021) Raincloud plots: a multi-platform tool for robust data visualization: version 2

Joachim Schork (2021) Ridgeline Plots in R (3 Examples)

Examples

library(ggplot2)
library(data.table)
library(grid)
library(gtable)
library(ggplotify)
library(RplotterPkg)

RplotterPkg::create_density_ridge_plot(
  df = RplotterPkg::midwest,
  bw = "sj",
  variables = c("HS_Diploma", "College_Edu", "Prof_Deg", "White", "Black", "Asian"),
  title = "Percent Distribution Among Midwest Counties",
  x_limits = c(0, 100),
  x_major_breaks = seq(0, 100, 10),
  density_fill = "blue",
  density_alpha = 0.5,
  plot_heights = 2.5,
  plot_widths = 18
)


deandevl/RplotterPkg documentation built on March 1, 2025, 11:17 a.m.