jaspHistogram: Histograms and Density plots for JASP

View source: R/jaspHistogram.R

jaspHistogramR Documentation

Histograms and Density plots for JASP

Description

A plot histogram with three components. (1) A histogram, which can be tweaks with the same settings as hist. (2) A density line, which can optionally be shaded. (3) rugs underneath the figure. Each of these components can be enabled (or disabled) individually. Colors are taken from graphOptions("palette").

Usage

jaspHistogram(
  x,
  xName,
  groupingVariable = NULL,
  groupingVariableName,
  histogram = TRUE,
  histogramPosition = "dodge",
  binWidthType = c("doane", "fd", "scott", "sturges", "manual"),
  numberOfBins = NA,
  rugs = FALSE,
  rugsColor = FALSE,
  density = FALSE,
  densityColor = FALSE,
  densityShade = FALSE,
  densityShadeAlpha = 0.6,
  densityLineWidth = 1,
  hideYAxisLabels = density
)

Arguments

x

numeric, the data to show a histogram for

xName

string, the title on the x-axis

groupingVariable

factor, show histograms and/ or densities for individual groups?

groupingVariableName

character, legend name of the grouping variable.

histogram

logical, should a histogram be shown?

histogramPosition

character, see argument position in geom_histogram.

binWidthType

string, type of bindwidth, matches the string values that can be passed to hist.

numberOfBins

if binWidthType is set to "manual", this value determines the number of bins.

rugs

logical, should rugs be shown on the x-axis?

rugsColor

logical, should the rugs be colored?

density

logical, should a density be superimposed on the plot?

densityColor

logical, should the density be colored?

densityShade

logical, should the area underneath the density be shaded?

densityShadeAlpha

numeric in [0, 1], transparancy for the shaded density.

densityLineWidth

positive number, the line width of the superimposed density.

hideYAxisLabels

logical, should the y-axis line be hidden? Defaults to showDensity.

Examples

set.seed(1234)

# some random data
x <- rnorm(1e3) + rnorm(1e3, 1, .1) - rgamma(1e3, 10, 5)

# individual plot components can be enabled or disabled
jaspHistogram(x, "random data", histogram = TRUE,  density = FALSE, rugs = FALSE) +
  ggplot2::ggtitle("default options")
jaspHistogram(x, "random data", histogram = TRUE,  density = TRUE,  rugs = FALSE)
jaspHistogram(x, "random data", histogram = TRUE,  density = TRUE,  rugs = TRUE)
jaspHistogram(x, "random data", histogram = TRUE,  density = FALSE, rugs = TRUE)
jaspHistogram(x, "random data", histogram = FALSE, density = FALSE, rugs = TRUE)
jaspHistogram(x, "random data", histogram = FALSE, density = FALSE, rugs = FALSE) +
  ggplot2::ggtitle("this is a bit boring though")

# histogram style can be tweaked
jaspHistogram(x, binWidthType = "sturges") + ggplot2::ggtitle("breaks: sturges")
jaspHistogram(x, binWidthType = "manual", numberOfBins = 250) +
  ggplot2::ggtitle("breaks: manual")

# split by group
groupingVariable <- gl(3, 333, length = 1e3)
groupingVariableName <- "group name"

jaspHistogram(x, "random data", groupingVariable, groupingVariableName)
jaspHistogram(x, "random data", groupingVariable, groupingVariableName,
              histogramPosition = "identity") # stacked
jaspHistogram(x, "random data", groupingVariable, groupingVariableName, density = TRUE,
              rugs = TRUE, rugsColor = TRUE)
jaspHistogram(x, "random data", groupingVariable, groupingVariableName, density = TRUE,
              rugs = TRUE, hideYAxisLabels = FALSE)
jaspHistogram(x, "random data", groupingVariable, groupingVariableName, density = TRUE,
              densityColor = TRUE, hideYAxisLabels = FALSE, densityLineWidth = 2)

# Can also be used to make fancy density plots
data("mtcars")
graphOptions(palette = "viridis")
jaspHistogram(mtcars$drat, "drat", factor(mtcars$cyl), "cyl",
              histogram = FALSE, hideYAxisLabels = FALSE,
              density = TRUE, densityColor = TRUE, densityShade = TRUE, densityShadeAlpha = 0.45
)
graphOptions(palette = "colorblind")

jasp-stats/jaspGraphs documentation built on April 20, 2024, 4:13 p.m.