Ridge plot type

Description

Type function for producing ridge plots (also known as joy plots), which display density distributions for multiple groups with vertical offsets. This function uses tinyplot scaffolding, which enables added functionality such as grouping and faceting.

The line color is controlled by the col argument in the tinyplot() call. The fill color is controlled by the bg argument in the tinyplot() call.

Usage

type_ridge(
  scale = 1.5,
  gradient = FALSE,
  breaks = NULL,
  probs = NULL,
  bw = "nrd0",
  kernel = "gaussian",
  ...,
  raster = FALSE,
  col = NULL,
  alpha = NULL
)

Arguments

scale Numeric. Controls the scaling factor of each plot. Values greater than 1 means that plots overlap.
gradient Logical or character. Should a color gradient be used to shade the area under the density? If a character specification is used, then it can either be of length 1 and specify the palette to be used with gradient = TRUE corresponding to gradient = “viridis”. If a character vector of length greater than 1 is used, then it should specifify the colors in the palette, e.g., gradient = hcl.colors(512).
breaks Numeric. If a color gradient is used for shading, the breaks between the colors can be modified. The default is to use equidistant breaks spanning the range of the x variable.
probs Numeric. Instead of specifying the same breaks on the x-axis for all groups, it is possible to specify group-specific quantiles at the specified probs. The quantiles are computed based on the density (rather than the raw original variable). Only one of breaks or probs must be specified.
bw, kernel, Arguments passed to density.
raster Logical. Should the ridges and color gradient, if relevant, be drawn via a coercion to rasterImage? Note that this will result in potential smoothness artifacts at high resolutions. Defaults to FALSE, in which case the ridges and color gradient will be passed in vectorised fashion to polygon.
col Character string denoting the outline (border) color for all of the ridge densities. Note that a singular value is expected; if multiple colors are provided then only the first will be used. This argument is mostly useful for the aesthetic effect of drawing a common outline color in combination with gradient fills. See Examples.
alpha Numeric in the range [0,1] for adjusting the alpha transparency of the density fills. In most cases, will default to a value of 1, i.e. fully opaque. But for some by grouped plots (excepting the special cases where by==y or by==x), will default to 0.6.

Examples

library("tinyplot")

## default ridge plot
tinyplot(Species ~ Sepal.Width, data = iris, type = "ridge")

## use the same bandwidth for all densities
tinyplot(Species ~ Sepal.Width, data = iris,
  type = type_ridge(bw = bw.nrd0(iris$Sepal.Width)))

## customized ridge plot without overlap of densities
tinyplot(Species ~ Sepal.Width, data = iris,
  type = type_ridge(scale = 1),
  bg = "light blue", col = "black")

## by grouping is also supported. two special cases of interest:

# 1) by == y (color by y groups)
tinyplot(Species ~ Sepal.Width | Species, data = iris, type = "ridge")

# 2) by == x (gradient coloring along x)
tinyplot(Species ~ Sepal.Width | Sepal.Width, data = iris, type = "ridge")

# aside: pass explicit `type_ridge(col = <col>)` arg to set a common border
# color
tinyplot(Species ~ Sepal.Width | Sepal.Width, data = iris,
  type = type_ridge(col = "white"))

## gradient coloring along the x-axis can also be invoked manually without
## a legend (the following lines are all equivalent)
tinyplot(Species ~ Sepal.Width, data = iris, type = type_ridge(gradient = TRUE))

# tinyplot(Species ~ Sepal.Width, data = iris, type = type_ridge(gradient = "viridis"))
# tinyplot(Species ~ Sepal.Width, data = iris, type = type_ridge(gradient = hcl.colors(512)))

## highlighting only the center 50% of the density (between 25% and 75% quantile)
tinyplot(Species ~ Sepal.Width, data = iris, col = "white", type = type_ridge(
  gradient = hcl.colors(3, "Dark Mint")[c(2, 1, 2)], probs = c(0.25, 0.75)))

## highlighting the probability distribution by the color gradient
tinyplot(Species ~ Sepal.Width, data = iris, type = type_ridge(
  gradient = hcl.colors(250, "Dark Mint")[c(250:1, 1:250)], probs = 0:500/500))

## with faceting and color gradient
airq = transform(airquality, Late = ifelse(Day > 15, "Late", "Early"))
tinyplot(Month ~ Ozone, facet = ~ Late, data = airq,
  type = type_ridge(gradient = TRUE),
  grid = TRUE, axes = "t", col = "white")