ggMarginal_Hist2D: Add marginal histograms to ggplot2 2d histogram plots

View source: R/ggMarginal_Hist2D.R

ggMarginal_Hist2DR Documentation

Add marginal histograms to ggplot2 2d histogram plots

Description

Create a ggplot2 2d histogram plot with marginal histogram plots.

Usage

ggMarginal_Hist2D(
  data,
  x,
  y,
  size = 5,
  after_stat_var = c("ncount", "count", "density", "ndensity"),
  hist_scale = c("arithmetic", "log10", "4th-root"),
  bins = 30,
  binwidths = NULL,
  xlab = NULL,
  ylab = NULL,
  xparams = list(limits = NULL, breaks = waiver(), labels = waiver(), name = waiver(),
    oob = scales::squish),
  yparams = list(limits = NULL, breaks = waiver(), labels = waiver(), name = waiver(),
    oob = scales::squish),
  fill_scale = ggplot2::scale_fill_viridis_c(option = "magma", name = ""),
  legend.position = c(0.99, 0.99),
  legend.justification = c(1, 1),
  ...,
  groupColour = FALSE,
  groupFill = FALSE,
  showIntermediates = FALSE,
  testing = FALSE
)

Arguments

data

The data.frame to use for creating the plots.

x

The name of the variable along the x axis.

y

The name of the variable along the y axis.

size

Integer describing the relative size of the marginal plots compared to the main plot. A size of 5 means that the main plot is 5x wider and 5x taller than the marginal plots.

hist_scale

- scale for histograms; options = ("arithmetic","log10","4th-root")

bins

- number of bins to use (scalar or 2-element vector); default=30

binwidths

- bin widths to use (scalar or 2-element vector); default=NULL

xlab

- x-axis title; default=NULL (i.e., use ggplot2 default)

ylab

- y-axis title; default=NULL (i.e., use ggplot2 default)

xparams

- list of [ggplot2::scale_x_continuous()] parameters; default = list(limits=NULL,breaks=waiver(),labels=waiver(),oob=scales::squish)

yparams

- list of [ggplot2::scale_y_continuous()] parameters; default = list(limits=NULL,breaks=waiver(),labels=waiver(),oob=scales::squish)

fill_scale

- 2d histogram fill scale; default=ggplot2::scale_fill_viridis_c(option="magma",name="")

legend.position

- legend position; default=c(0.99,0.99)

legend.justification

- legend justification; default=c(1,1)

...

Extra parameters to pass to the marginal plots. Any parameter that [ggplot2::geom_histogram()] accepts can be used. For example, colour = "red" can be used for any marginal plot type,

groupColour

If TRUE, the colour (or outline) of the marginal plots will be grouped according to the variable mapped to colour in the scatter plot. The variable mapped to colour in the scatter plot must be a character or factor variable. See examples below.

groupFill

If TRUE, the fill of the marginal plots will be grouped according to the variable mapped to colour in the scatter plot. The variable mapped to colour in the scatter plot must be a character or factor variable.

testing

- flag (T/F) to include

stat_var

- name of statistical variable to plot; options=("count","ncount","density","ndensity")

Value

If testing is false, a ggGTbl object that can be printed using S3 dispatch with print. If testing is TRUE, a list with elements

  • ggGTbl - an object of class ggGTbl.

  • built - a list with elements p2d, px,py corresponding to ggplot2 objects "built" with [ggplot2::ggplot_build()]

Note

The grid, gtable, and rlang packages are required for this function.

This function and related code are based on the [ggExtra::ggMarginal()] code developed by Dean Attali ("daattali@gmail.com").

Examples

library(ggplot2)
ggGTbl = ggMarginal_Hist2D(data=mtcars,x=wt,y=mpg,
                          hist_scale="arithmetic",
                          binwidths=c(0.5,7),
                          xparams=list(limits=c(0, 3),breaks=seq(0, 7,0.5),oob=scales::squish),
                          yparams=list(limits=c(0,40),breaks=seq(0,40,2),oob=scales::squish),
                          legend.position=c(0.01,0.99),
                          legend.justification=c(0,1),
                          showIntermediates=FALSE);
print(ggGTbl,newpage=TRUE);
gglst = ggMarginal_Hist2D(data=mtcars,x=wt,y=mpg,
                          hist_scale="arithmetic",
                          binwidths=c(0.5,7),
                          xparams=list(limits=c(0, 3),breaks=seq(0, 7,0.5),oob=scales::squish),
                          yparams=list(limits=c(0,40),breaks=seq(0,40,2),oob=scales::squish),
                          legend.position=c(0.01,0.99),
                          legend.justification=c(0,1),
                          showIntermediates=FALSE,
                          testing=TRUE);
print(gglst$ggGTbl,newpage=TRUE);


wStockhausen/wtsPlots documentation built on July 19, 2023, 8:15 p.m.