R: Stat for density ridgeline plots

stat_density_ridges {ggridges} | R Documentation

## Stat for density ridgeline plots

### Description

This stat is the default stat used by `geom_density_ridges`. It is very similar to `stat_density`, however there are a few differences. Most importantly, the density bandwidth is chosen across the entire dataset.

### Usage

    stat_density_ridges(mapping = NULL, data = NULL, geom = "density_ridges",
      position = "identity", na.rm = FALSE, show.legend = NA,
      inherit.aes = TRUE, bandwidth = NULL, from = NULL, to = NULL,
      jittered_points = FALSE, quantile_lines = FALSE, calc_ecdf = FALSE,
      quantiles = 4, ...)

### Arguments

`mapping` | Set of aesthetic mappings created by `aes()` or `aes_()`. If specified and `inherit.aes = TRUE` (the default), it is combined with the default mapping at the top level of the plot. You must supply `mapping` if there is no plot mapping.
`data` | The data to be displayed in this layer. There are three options: If `NULL`, the default, the data is inherited from the plot data as specified in the call to `ggplot()`. A `data.frame`, or other object, will override the plot data. A `function` will be called with a single argument, the plot data. The return value must be a `data.frame.`, and will be used as the layer data.
`geom` | The geometric object to use to display the data.
`position` | Position adjustment, either as a string, or the result of a call to a position adjustment function.
`na.rm` | If `FALSE`, the default, missing values are removed with a warning. If `TRUE`, missing values are silently removed.
`show.legend` | logical. Should this layer be included in the legends? `NA`, the default, includes if any aesthetics are mapped. `FALSE` never includes, and `TRUE` always includes.
`inherit.aes` | If `FALSE`, overrides the default aesthetics, rather than combining with them.
`bandwidth` | Bandwidth used for density calculation. If not provided, is estimated from the data.
`from, to` | The left and right-most points of the grid at which the density is to be estimated, as in `density()`. If not provided, there are estimated from the data range and the bandwidth.
`jittered_points` | If `TRUE`, carries the original point data over to the processed data frame, so that individual points can be drawn by the various ridgeline geoms. The specific position of these points is controlled by various position objects, e.g. `position_points_sina()` or `position_raincloud()`.
`quantile_lines` | If `TRUE`, enables the drawing of quantile lines.
`calc_ecdf` | If `TRUE`, `stat_density_ridges` calculates an empirical cumulative distribution function (ecdf) and returns a variable `ecdf` and a variable `quantile`. Both can be mapped onto aesthetics via `..ecdf..` and `..quantile..`, respectively.
`quantiles` | Sets the number of quantiles the data should be broken into. Used if either `calc_ecdf = TRUE` or `quantile_lines = TRUE`. If `quantiles` is an integer then the data will be cut into that many equal quantiles. If it is a vector of probabilities then the data will cut by them.
`...` | other arguments passed on to `layer()`. These are often aesthetics, used to set an aesthetic to a fixed value, like `color = "red"` or `size = 3`. They may also be parameters to the paired geom/stat.

### Examples

    library(ggplot2)

    # Examples of coloring by ecdf or quantiles
    library(viridis)
    ggplot(iris, aes(x=Sepal.Length, y=Species, fill=factor(..quantile..))) +
      stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE,
                          quantiles = 5) +
      scale_fill_viridis(discrete = TRUE, name = "Quintiles") + theme_ridges() +
      scale_y_discrete(expand = c(0.01, 0))

    ggplot(iris, aes(x=Sepal.Length, y=Species, fill=0.5 - abs(0.5-..ecdf..))) +
      stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
      scale_fill_viridis(name = "Tail probability", direction = -1) + theme_ridges() +
      scale_y_discrete(expand = c(0.01, 0))

    ggplot(iris, aes(x=Sepal.Length, y=Species, fill=factor(..quantile..))) +
      stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE,
                          quantiles = c(0.05, 0.95)) +
      scale_fill_manual(name = "Probability\nranges",
                        values = c("red", "grey80", "blue")) +
      theme_ridges() + scale_y_discrete(expand = c(0.01, 0))

[Package ggridges version 0.5.0 Index]