Title: | Ridgeline Plots in 'ggplot2' |
---|---|
Description: | Ridgeline plots provide a convenient way of visualizing changes in distributions over time or space. This package enables the creation of such plots in 'ggplot2'. |
Authors: | Claus O. Wilke [aut, cre] |
Maintainer: | Claus O. Wilke <[email protected]> |
License: | GPL-2 | file LICENSE |
Version: | 0.5.6.9000 |
Built: | 2024-11-03 03:04:57 UTC |
Source: | https://github.com/wilkelab/ggridges |
This dataset is equivalent to ais
from the DAAG
package.
Aus_athletes
Aus_athletes
An object of class data.frame
with 202 rows and 13 columns.
Telford, R.D. and Cunningham, R.B. 1991. Sex, sport and body-size dependency of hematology in highly trained athletes. Medicine and Science in Sports and Exercise 23: 788-794.
# none yet
# none yet
Data from Catalan regional elections for 949 municipalities, from 11 elections spanning the years 1980-2015. The data was obtained and processed from Idescat.cat by Marc Belzunces (Twitter: @marcbeldata).
Catalan_elections
Catalan_elections
A tibble with 20764 rows and 4 variables:
Municipality
Year
Option
The voter option; either "Indy" or "Unionist"
Percent
The percentage of the voters choosing the given option
This function is a drop-in replacement for ggplot2's geom_density()
. The only difference is that
the geom draws a ridgeline (line with filled area underneath) rather than a polygon.
geom_density_line( mapping = NULL, data = NULL, stat = "density", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_density_line( mapping = NULL, data = NULL, stat = "density", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
See geom_density()
.
library(ggplot2) ggplot(diamonds, aes(carat)) + geom_density_line() ggplot(diamonds, aes(carat)) + geom_density_line(adjust = 1/5) ggplot(diamonds, aes(carat)) + geom_density_line(adjust = 5) ggplot(diamonds, aes(depth, colour = cut)) + geom_density_line(alpha = 0.5) + xlim(55, 70) ggplot(diamonds, aes(depth, fill = cut, colour = cut)) + geom_density_line(alpha = 0.1) + xlim(55, 70)
library(ggplot2) ggplot(diamonds, aes(carat)) + geom_density_line() ggplot(diamonds, aes(carat)) + geom_density_line(adjust = 1/5) ggplot(diamonds, aes(carat)) + geom_density_line(adjust = 5) ggplot(diamonds, aes(depth, colour = cut)) + geom_density_line(alpha = 0.5) + xlim(55, 70) ggplot(diamonds, aes(depth, fill = cut, colour = cut)) + geom_density_line(alpha = 0.1) + xlim(55, 70)
geom_density_ridges
arranges multiple density plots in a staggered fashion, as in the cover of the famous Joy Division album Unknown Pleasures.
geom_density_ridges( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... ) geom_density_ridges2( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_density_ridges( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... ) geom_density_ridges2( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
panel_scaling |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
other arguments passed on to |
By default, this geom calculates densities from the point data mapped onto the x axis. If density calculation is
not wanted, use stat="identity"
or use geom_ridgeline
. The difference between geom_density_ridges
and geom_ridgeline
is that geom_density_ridges
will provide automatic scaling of the ridgelines (controlled by the scale
aesthetic), whereas
geom_ridgeline will plot the data as is. Note that when you set stat="identity"
, the height
aesthetic must
be provided.
Note that the default stat_density_ridges
makes joint density estimation across all datasets. This may not generate
the desired result when using faceted plots. As an alternative, you can set stat = "density"
to use stat_density
.
In this case, it is required to add the aesthetic mapping height = after_stat(density)
(see examples).
Required aesthetics are in bold.
x
y
weight
Optional case weights passed to stats::density
to calculate a weighted density estimate
group
Defines the grouping. Not needed if a categorical variable is mapped onto y
, but needed otherwise. Will typically be the same
variable as is mapped to y
.
height
The height of each ridgeline at the respective x value. Automatically calculated and
provided by stat_density_ridges
if the default stat is not changed.
scale
A scaling factor to scale the height of the ridgelines relative to the spacing between them.
A value of 1 indicates that the maximum point of any ridgeline touches the baseline right above, assuming
even spacing between baselines.
rel_min_height
Lines with heights below this cutoff will be removed. The cutoff is measured relative to the
overall maximum, so rel_min_height=0.01
would remove everything that is 1\
ridgelines. Default is 0, so nothing is removed.
alpha
colour
, fill
, group
, alpha
, linetype
, linewidth
, as in geom_ridgeline
.
point_shape
, point_colour
, point_size
, point_fill
, point_alpha
, point_stroke
, as in geom_ridgeline
.
library(ggplot2) # set the `rel_min_height` argument to remove tails ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(rel_min_height = 0.005) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # set the `scale` to determine how much overlap there is among the plots ggplot(diamonds, aes(x = price, y = cut)) + geom_density_ridges(scale = 4) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # the same figure with colors, and using the ggplot2 density stat ggplot(diamonds, aes(x = price, y = cut, fill = cut, height = after_stat(density))) + geom_density_ridges(scale = 4, stat = "density") + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + scale_fill_brewer(palette = 4) + theme_ridges() + theme(legend.position = "none") # use geom_density_ridges2() instead of geom_density_ridges() for solid polygons ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges2() + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges()
library(ggplot2) # set the `rel_min_height` argument to remove tails ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(rel_min_height = 0.005) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # set the `scale` to determine how much overlap there is among the plots ggplot(diamonds, aes(x = price, y = cut)) + geom_density_ridges(scale = 4) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # the same figure with colors, and using the ggplot2 density stat ggplot(diamonds, aes(x = price, y = cut, fill = cut, height = after_stat(density))) + geom_density_ridges(scale = 4, stat = "density") + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + scale_fill_brewer(palette = 4) + theme_ridges() + theme(legend.position = "none") # use geom_density_ridges2() instead of geom_density_ridges() for solid polygons ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges2() + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges()
Plots the sum of the y
and height
aesthetics versus x
, filling the area between y
and y + height
with a color.
Thus, the data mapped onto y and onto height must be in the same units.
If you want relative scaling of the heights, you can use geom_density_ridges
with stat = "identity"
.
geom_ridgeline( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_ridgeline( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
other arguments passed on to |
In addition to drawing ridgelines, this geom can also draw points if they are provided as part of the dataset.
The stat stat_density_ridges()
takes advantage of this option to generate ridgeline plots with overlaid
jittered points.
Required aesthetics are in bold.
x
y
height
Height of the ridgeline, measured from the respective y
value. Assumed to be positive, though this is not required.
group
Defines the grouping. Required when the dataset contains multiple distinct ridgelines. Will typically be the same
variable as is mapped to y
.
scale
A scaling factor to scale the height of the ridgelines.
A value of 1 indicates that the heights are taken as is. This aesthetic can be used to convert
height
units into y
units.
min_height
A height cutoff on the drawn ridgelines. All values that fall below this cutoff will be removed.
The main purpose of this cutoff is to remove long tails right at the baseline level, but other uses are possible.
The cutoff is applied before any height
scaling is applied via the scale
aesthetic. Default is 0, so negative values are removed.
colour
Color of the ridgeline
fill
Fill color of the area under the ridgeline
alpha
Transparency level of fill
. Not applied to color
. If you want transparent lines, you can set their
color as RGBA value, e.g. #FF0000A0 for partially transparent red.
group
Grouping, to draw multiple ridgelines from one dataset
linetype
Linetype of the ridgeline
linewidth
Line thickness
point_shape
, point_colour
, point_size
, point_fill
, point_alpha
, point_stroke
Aesthetics applied
to points drawn in addition to ridgelines.
library(ggplot2) d <- data.frame(x = rep(1:5, 3), y = c(rep(0, 5), rep(1, 5), rep(3, 5)), height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)) ggplot(d, aes(x, y, height = height, group = y)) + geom_ridgeline(fill="lightblue")
library(ggplot2) d <- data.frame(x = rep(1:5, 3), y = c(rep(0, 5), rep(1, 5), rep(3, 5)), height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)) ggplot(d, aes(x, y, height = height, group = y)) + geom_ridgeline(fill="lightblue")
The geoms geom_ridgeline_gradient
and geom_density_ridges_gradient
work just like geom_ridgeline
and geom_density_ridges
except
that the fill
aesthetic can vary along the x axis. Because filling with color gradients is fraught with issues,
these geoms should be considered experimental. Don't use them unless you really need to. Note that due to limitations
in R's graphics system, transparency (alpha
) has to be disabled for gradient fills.
geom_ridgeline_gradient( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, gradient_lwd = 0.5, show.legend = NA, inherit.aes = TRUE, ... ) geom_density_ridges_gradient( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = TRUE, gradient_lwd = 0.5, show.legend = NA, inherit.aes = TRUE, ... )
geom_ridgeline_gradient( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, gradient_lwd = 0.5, show.legend = NA, inherit.aes = TRUE, ... ) geom_density_ridges_gradient( mapping = NULL, data = NULL, stat = "density_ridges", position = "points_sina", panel_scaling = TRUE, na.rm = TRUE, gradient_lwd = 0.5, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
na.rm |
If |
gradient_lwd |
A parameter to needed to remove rendering artifacts inside the rendered gradients. Should ideally be 0, but often needs to be around 0.5 or higher. |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
other arguments passed on to |
panel_scaling |
Argument only to |
library(ggplot2) # Example for `geom_ridgeline_gradient()` d <- data.frame( x = rep(1:5, 3) + c(rep(0, 5), rep(0.3, 5), rep(0.6, 5)), y = c(rep(0, 5), rep(1, 5), rep(3, 5)), height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1) ) ggplot(d, aes(x, y, height = height, group = y, fill = factor(x+y))) + geom_ridgeline_gradient() + scale_fill_viridis_d(direction = -1) + theme(legend.position = 'none') # Example for `geom_density_ridges_gradient()` ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = stat(x))) + geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) + scale_x_continuous(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + scale_fill_viridis_c(name = "Temp. [F]", option = "C") + coord_cartesian(clip = "off") + labs(title = 'Temperatures in Lincoln NE in 2016') + theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank())
library(ggplot2) # Example for `geom_ridgeline_gradient()` d <- data.frame( x = rep(1:5, 3) + c(rep(0, 5), rep(0.3, 5), rep(0.6, 5)), y = c(rep(0, 5), rep(1, 5), rep(3, 5)), height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1) ) ggplot(d, aes(x, y, height = height, group = y, fill = factor(x+y))) + geom_ridgeline_gradient() + scale_fill_viridis_d(direction = -1) + theme(legend.position = 'none') # Example for `geom_density_ridges_gradient()` ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = stat(x))) + geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) + scale_x_continuous(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + scale_fill_viridis_c(name = "Temp. [F]", option = "C") + coord_cartesian(clip = "off") + labs(title = 'Temperatures in Lincoln NE in 2016') + theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank())
Plots the sum of the x
and width
aesthetics versus y
, filling the area between x
and x + width
with a color.
Just like geom_ridgeline()
, but with y and x replaced.
geom_vridgeline( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_vridgeline( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
other arguments passed on to |
Required aesthetics are in bold.
x
y
width
Width of the ridgeline, measured from the respective x
value. Assumed to be positive, though this is not required.
group
Defines the grouping. Required when the dataset contains multiple distinct ridgelines. Will typically be the same
variable as is mapped to x
.
scale
A scaling factor to scale the widths of the ridgelines.
A value of 1 indicates that the widths are taken as is. This aesthetic can be used to convert
width
units into x
units.
min_width
A width cutoff on the drawn ridgelines. All values that fall below this cutoff will be removed.
The main purpose of this cutoff is to remove long tails right at the baseline level, but other uses are possible.
The cutoff is applied before any width
scaling is applied via the scale
aesthetic. Default is 0, so negative values are removed.
color
Color of the ridgeline
fill
Fill color of the area under the ridgeline
alpha
Transparency level of fill
. Not applied to color
. If you want transparent lines, you can set their
color as RGBA value, e.g. #FF0000A0 for partially transparent red.
group
Grouping, to draw multiple ridgelines from one dataset
linetype
Linetype of the ridgeline
linewidth
Line thickness
library(ggplot2) d <- data.frame(y = rep(1:5, 3), x = c(rep(0, 5), rep(1, 5), rep(3, 5)), width = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)) ggplot(d, aes(x, y, width = width, group = x)) + geom_vridgeline(fill="lightblue") ggplot(iris, aes(x=Species, y=Sepal.Width, width = after_stat(density), fill=Species)) + geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 2)
library(ggplot2) d <- data.frame(y = rep(1:5, 3), x = c(rep(0, 5), rep(1, 5), rep(3, 5)), width = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)) ggplot(d, aes(x, y, width = width, group = x)) + geom_vridgeline(fill="lightblue") ggplot(iris, aes(x=Species, y=Sepal.Width, width = after_stat(density), fill=Species)) + geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 2)
A dataset containing weather information from Lincoln, Nebraska, from 2016. Originally downloaded from Weather Underground by Austin Wehrwein, http://austinwehrwein.com/. The variables are listed below. Most are self-explanatory. Max, mean, and min measurements are calculated relative to the specific day of measurement.
lincoln_weather
lincoln_weather
A tibble with 366 rows and 24 variables:
CST
Day of the measurement
Max Temperature [F]
Mean Temperature [F]
Min Temperature [F]
Max Dew Point [F]
Mean Dew Point [F]
Min Dewpoint [F]
Max Humidity
Mean Humidity
Min Humidity
Max Sea Level Pressure [In]
Mean Sea Level Pressure [In]
Min Sea Level Pressure [In]
Max Visibility [Miles]
Mean Visibility [Miles]
Min Visibility [Miles]
Max Wind Speed [MPH]
Mean Wind Speed[MPH]
Max Gust Speed [MPH]
Precipitation [In]
CloudCover
Events
Specific weather events, such as rain, snow, or fog
WindDir [Degrees]
Month
The month in which the measurement was taken
This is a position adjustment specifically for geom_density_ridges()
and related geoms. It
only jitters the points drawn by these geoms, if any. If no points are present, the plot
remains unchanged. The effect is similar to position_jitter()
: points are randomly shifted up and down
and/or left and right.
position_points_jitter( width = 0, height = 0.2, yoffset = 0, adjust_vlines = FALSE, seed = NULL )
position_points_jitter( width = 0, height = 0.2, yoffset = 0, adjust_vlines = FALSE, seed = NULL )
width |
Width for horizontal jittering. By default set to 0. |
height |
Height for vertical jittering, applied in both directions (up and down). By default 0.2. |
yoffset |
Vertical offset applied in addition to jittering. |
adjust_vlines |
If |
seed |
Random seed. If set to NULL, the current random number generator is used. If set to NA, a new random random seed is generated. If set to a number, this number is used as seed for jittering only. |
Other position adjustments for ridgeline plots: position_points_sina
, position_raincloud
library(ggplot2) # default jittered points ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "points_jitter", alpha = 0.7) # simulating a rug ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, point_shape = '|', alpha = 0.7, point_size = 2, position = position_points_jitter(width = 0.02, height = 0))
library(ggplot2) # default jittered points ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "points_jitter", alpha = 0.7) # simulating a rug ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, point_shape = '|', alpha = 0.7, point_size = 2, position = position_points_jitter(width = 0.02, height = 0))
This is a position adjustment specifically for geom_density_ridges()
and related geoms. It
only jitters the points drawn by these geoms, if any. If no points are present, the plot
remains unchanged. The effect is similar to a sina plot: Points are randomly distributed to fill
the entire shaded area representing the data density.
position_points_sina(rel_min = 0.02, rel_max = 0.98, seed = NULL)
position_points_sina(rel_min = 0.02, rel_max = 0.98, seed = NULL)
rel_min |
The relative minimum value at which a point can be placed. |
rel_max |
The relative maximum value at which a point can be placed. |
seed |
Other position adjustments for ridgeline plots: position_points_jitter
, position_raincloud
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "points_sina", alpha = 0.7)
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "points_sina", alpha = 0.7)
This is a position adjustment specifically for geom_density_ridges()
and related geoms. It
only jitters the points drawn by these geoms, if any. If no points are present, the plot
remains unchanged. The effect is similar to position_points_jitter()
, only that by default the
points lie all underneath the baseline of each individual ridgeline.
position_raincloud( width = 0, height = 0.4, ygap = 0.05, adjust_vlines = FALSE, seed = NULL )
position_raincloud( width = 0, height = 0.4, ygap = 0.05, adjust_vlines = FALSE, seed = NULL )
width |
Width for horizontal jittering. By default set to 0. |
height |
Total height of point cloud. By default 0.4. |
ygap |
Vertical gap between ridgeline baseline and point cloud. |
adjust_vlines |
If |
seed |
Random seed. See |
The idea for this position adjustment comes from Micah Allen's work on raincloud plots (Allen et al. 2021).
Allen, M., Poggiali, D., Whitaker, K., Marshall, T. R., van Langen, J., Kievit, R. A. (2021) Raincloud plots: a multi-platform tool for robust data visualization [version 2; peer review: 2 approved]. Wellcome Open Res 4:63.
Other position adjustments for ridgeline plots: position_points_jitter
, position_points_sina
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "raincloud", alpha = 0.7)
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(jittered_points = TRUE, position = "raincloud", alpha = 0.7)
The readability of ridgeline plots can often be improved by alternating between fill colors and
other aesthetics. The various cyclical scales make it easy to create plots with this feature,
simply map your grouping variable to the respective aesthetic (e.g., fill
) and then use
scale_fill_cyclical
to define the fill colors between you want to alternate. Note that the
cyclical scales do not draw legends by default, because the legends will usually be wrong
unless the labels are properly adjusted. To draw legends, set the guide
argument to "legend"
,
as shown in the examples.
scale_colour_cyclical(..., values) scale_fill_cyclical(..., values) scale_alpha_cyclical(..., values) scale_linetype_cyclical(..., values) scale_size_cyclical(..., values)
scale_colour_cyclical(..., values) scale_fill_cyclical(..., values) scale_alpha_cyclical(..., values) scale_linetype_cyclical(..., values) scale_size_cyclical(..., values)
... |
Common discrete scale parameters: |
values |
The aesthetic values that the scale should cycle through, e.g. colors if it is a scale for the color or fill aesthetic. |
library(ggplot2) # By default, scale_cyclical sets `guide = "none"`, i.e., no legend # is drawn ggplot(diamonds, aes(x = price, y = cut, fill = cut)) + geom_density_ridges(scale = 4) + scale_fill_cyclical(values = c("#3030D0", "#9090F0")) # However, legends can be turned on by setting `guide = "legend"` ggplot(diamonds, aes(x = price, y = cut, fill = cut)) + geom_density_ridges(scale = 4) + scale_fill_cyclical(values = c("#3030D0", "#9090F0"), guide = "legend", name = "Fill colors", labels = c("dark blue", "light blue")) # Cyclical scales are also available for the various other aesthetics ggplot(diamonds, aes(x = price, y = cut, fill = cut, color = cut, size = cut, alpha = cut, linetype = cut)) + geom_density_ridges(scale = 4, fill = "blue") + scale_fill_cyclical(values = c("blue", "green")) + scale_color_cyclical(values = c("black", "white")) + scale_size_cyclical(values = c(2, 1)) + scale_alpha_cyclical(values = c(0.4, 0.8)) + scale_linetype_cyclical(values = c(1, 2))
library(ggplot2) # By default, scale_cyclical sets `guide = "none"`, i.e., no legend # is drawn ggplot(diamonds, aes(x = price, y = cut, fill = cut)) + geom_density_ridges(scale = 4) + scale_fill_cyclical(values = c("#3030D0", "#9090F0")) # However, legends can be turned on by setting `guide = "legend"` ggplot(diamonds, aes(x = price, y = cut, fill = cut)) + geom_density_ridges(scale = 4) + scale_fill_cyclical(values = c("#3030D0", "#9090F0"), guide = "legend", name = "Fill colors", labels = c("dark blue", "light blue")) # Cyclical scales are also available for the various other aesthetics ggplot(diamonds, aes(x = price, y = cut, fill = cut, color = cut, size = cut, alpha = cut, linetype = cut)) + geom_density_ridges(scale = 4, fill = "blue") + scale_fill_cyclical(values = c("blue", "green")) + scale_color_cyclical(values = c("black", "white")) + scale_size_cyclical(values = c(2, 1)) + scale_alpha_cyclical(values = c(0.4, 0.8)) + scale_linetype_cyclical(values = c(1, 2))
These are various scales that can be applied to point aesthetics, such as
point_color
, point_fill
, point_size
. The individual scales all have the
same usage as existing standard ggplot2 scales, only the name differs.
See scale_vline_color_hue()
for specific scales for vline aesthetics
and scale_discrete_manual()
for a general discrete scale.
library(ggplot2) # default scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species)) + geom_density_ridges( aes( point_color = Species, point_fill = Species, point_shape = Species ), alpha = .4, jittered_points = TRUE ) + theme_ridges() # modified scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species)) + geom_density_ridges( aes( point_color = Species, point_fill = Species, point_shape = Species ), alpha = .4, point_alpha = 1, jittered_points = TRUE ) + scale_fill_hue(l = 50) + scale_point_color_hue(l = 20) + scale_point_fill_hue(l = 70) + scale_discrete_manual("point_shape", values = c(21, 22, 23)) + theme_ridges()
library(ggplot2) # default scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species)) + geom_density_ridges( aes( point_color = Species, point_fill = Species, point_shape = Species ), alpha = .4, jittered_points = TRUE ) + theme_ridges() # modified scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species)) + geom_density_ridges( aes( point_color = Species, point_fill = Species, point_shape = Species ), alpha = .4, point_alpha = 1, jittered_points = TRUE ) + scale_fill_hue(l = 50) + scale_point_color_hue(l = 20) + scale_point_fill_hue(l = 70) + scale_discrete_manual("point_shape", values = c(21, 22, 23)) + theme_ridges()
These are various scales that can be applied to vline aesthetics, such as
vline_color
, vline_width
, vline_linetype
. The individual scales all have the
same usage as existing standard ggplot2 scales, only the name differs.
See scale_point_color_hue()
for specific scales for point aesthetics
and scale_discrete_manual()
for a general discrete scale.
library(ggplot2) # default scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species, color = Species)) + geom_density_ridges( aes(vline_color = Species, vline_linetype = Species), alpha = .4, quantile_lines = TRUE ) + theme_ridges() # modified scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species, color = Species)) + geom_density_ridges( aes(vline_color = Species), alpha = .4, quantile_lines = TRUE ) + scale_fill_hue(l = 50) + scale_vline_color_hue(l = 30) + theme_ridges()
library(ggplot2) # default scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species, color = Species)) + geom_density_ridges( aes(vline_color = Species, vline_linetype = Species), alpha = .4, quantile_lines = TRUE ) + theme_ridges() # modified scales ggplot(iris, aes(x=Sepal.Length, y=Species, fill = Species, color = Species)) + geom_density_ridges( aes(vline_color = Species), alpha = .4, quantile_lines = TRUE ) + scale_fill_hue(l = 50) + scale_vline_color_hue(l = 30) + theme_ridges()
Works like stat_bin
except that the output is a ridgeline describing the histogram rather than
a set of counts.
stat_binline( mapping = NULL, data = NULL, geom = "density_ridges", position = "identity", ..., binwidth = NULL, bins = NULL, center = NULL, boundary = NULL, breaks = NULL, closed = c("right", "left"), pad = TRUE, draw_baseline = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
stat_binline( mapping = NULL, data = NULL, geom = "density_ridges", position = "identity", ..., binwidth = NULL, bins = NULL, center = NULL, boundary = NULL, breaks = NULL, closed = c("right", "left"), pad = TRUE, draw_baseline = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geom to use for drawing. |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
binwidth |
The width of the bins. Can be specified as a numeric value
or as a function that calculates width from unscaled x. Here, "unscaled x"
refers to the original x values in the data, before application of any
scale transformation. When specifying a function along with a grouping
structure, the function will be called once per group.
The default is to use the number of bins in The bin width of a date variable is the number of days in each time; the bin width of a time variable is the number of seconds. |
bins |
Number of bins. Overridden by |
center , boundary
|
bin position specifiers. Only one, |
breaks |
Alternatively, you can supply a numeric vector giving
the bin boundaries. Overrides |
closed |
One of |
pad |
If |
draw_baseline |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species, fill = Species)) + geom_density_ridges(stat = "binline", bins = 20, scale = 2.2) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + coord_cartesian(clip = "off") + theme_ridges() ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species, fill = Species)) + stat_binline(bins = 20, scale = 2.2, draw_baseline = FALSE) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + scale_fill_grey() + coord_cartesian(clip = "off") + theme_ridges() + theme(legend.position = 'none') library(ggplot2movies) ggplot(movies[movies$year>1989,], aes(x = length, y = year, fill = factor(year))) + stat_binline(scale = 1.9, bins = 40) + scale_x_continuous(limits = c(1, 180), expand = c(0, 0)) + scale_y_reverse(expand = c(0, 0)) + scale_fill_viridis_d(begin = 0.3, option = "B") + coord_cartesian(clip = "off") + labs(title = "Movie lengths 1990 - 2005") + theme_ridges() + theme(legend.position = "none") count_data <- data.frame( group = rep(letters[1:5], each = 10), mean = rep(1:5, each = 10) ) count_data$group <- factor(count_data$group, levels = letters[5:1]) count_data$count <- rpois(nrow(count_data), count_data$mean) ggplot(count_data, aes(x = count, y = group, group = group)) + geom_density_ridges2( stat = "binline", aes(fill = group), binwidth = 1, scale = 0.95 ) + geom_text( stat = "bin", aes(y = group + 0.9*stat(count/max(count)), label = ifelse(stat(count) > 0, stat(count), "")), vjust = 1.2, size = 3, color = "white", binwidth = 1 ) + scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13), expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + scale_fill_cyclical(values = c("#0000B0", "#7070D0")) + guides(y = "none") + coord_cartesian(clip = "off") + theme_ridges(grid = FALSE)
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species, fill = Species)) + geom_density_ridges(stat = "binline", bins = 20, scale = 2.2) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + coord_cartesian(clip = "off") + theme_ridges() ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species, fill = Species)) + stat_binline(bins = 20, scale = 2.2, draw_baseline = FALSE) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + scale_fill_grey() + coord_cartesian(clip = "off") + theme_ridges() + theme(legend.position = 'none') library(ggplot2movies) ggplot(movies[movies$year>1989,], aes(x = length, y = year, fill = factor(year))) + stat_binline(scale = 1.9, bins = 40) + scale_x_continuous(limits = c(1, 180), expand = c(0, 0)) + scale_y_reverse(expand = c(0, 0)) + scale_fill_viridis_d(begin = 0.3, option = "B") + coord_cartesian(clip = "off") + labs(title = "Movie lengths 1990 - 2005") + theme_ridges() + theme(legend.position = "none") count_data <- data.frame( group = rep(letters[1:5], each = 10), mean = rep(1:5, each = 10) ) count_data$group <- factor(count_data$group, levels = letters[5:1]) count_data$count <- rpois(nrow(count_data), count_data$mean) ggplot(count_data, aes(x = count, y = group, group = group)) + geom_density_ridges2( stat = "binline", aes(fill = group), binwidth = 1, scale = 0.95 ) + geom_text( stat = "bin", aes(y = group + 0.9*stat(count/max(count)), label = ifelse(stat(count) > 0, stat(count), "")), vjust = 1.2, size = 3, color = "white", binwidth = 1 ) + scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13), expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + scale_fill_cyclical(values = c("#0000B0", "#7070D0")) + guides(y = "none") + coord_cartesian(clip = "off") + theme_ridges(grid = FALSE)
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.
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, quantile_fun = quantile, n = 512, ... )
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, quantile_fun = quantile, n = 512, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
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 |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
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 |
jittered_points |
If |
quantile_lines |
If |
calc_ecdf |
If |
quantiles |
Sets the number of quantiles the data should be broken into. Used if either |
quantile_fun |
Function that calculates quantiles. The function needs to accept two parameters,
a vector |
n |
The number of equally spaced points at which the density is to be estimated. Should be a power of 2. Default is 512. |
... |
other arguments passed on to |
library(ggplot2) # Examples of coloring by ecdf or quantiles ggplot(iris, aes(x = Sepal.Length, y = Species, fill = factor(stat(quantile)))) + stat_density_ridges( geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = 5 ) + scale_fill_viridis_d(name = "Quintiles") + theme_ridges() ggplot(iris, aes( x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5-stat(ecdf)) )) + stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) + scale_fill_viridis_c(name = "Tail probability", direction = -1) + theme_ridges() ggplot(iris, aes( x = Sepal.Length, y = Species, fill = factor(stat(quantile)) )) + stat_density_ridges( geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = c(0.025, 0.975) ) + scale_fill_manual( name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"), labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]") ) + theme_ridges()
library(ggplot2) # Examples of coloring by ecdf or quantiles ggplot(iris, aes(x = Sepal.Length, y = Species, fill = factor(stat(quantile)))) + stat_density_ridges( geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = 5 ) + scale_fill_viridis_d(name = "Quintiles") + theme_ridges() ggplot(iris, aes( x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5-stat(ecdf)) )) + stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) + scale_fill_viridis_c(name = "Tail probability", direction = -1) + theme_ridges() ggplot(iris, aes( x = Sepal.Length, y = Species, fill = factor(stat(quantile)) )) + stat_density_ridges( geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = c(0.025, 0.975) ) + scale_fill_manual( name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"), labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]") ) + theme_ridges()
This theme has some special modifications that make ridgeline plots look better, such as properly aligned y axis labels. It can draw plots with and without background grids (see examples).
theme_ridges( font_size = 14, font_family = "", line_size = 0.5, grid = TRUE, center_axis_labels = FALSE )
theme_ridges( font_size = 14, font_family = "", line_size = 0.5, grid = TRUE, center_axis_labels = FALSE )
font_size |
Overall font size. Default is 14. |
font_family |
Default font family. |
line_size |
Default line size. |
grid |
If |
center_axis_labels |
If |
The theme.
library(ggplot2) # Example with background grid ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species)) + geom_density_ridges(rel_min_height = 0.005) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # Example without background grid ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species)) + geom_density_ridges() + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges(grid = FALSE)
library(ggplot2) # Example with background grid ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species)) + geom_density_ridges(rel_min_height = 0.005) + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges() # Example without background grid ggplot(iris, aes(x = Sepal.Length, y = Species, group = Species)) + geom_density_ridges() + scale_y_discrete(expand = c(0.01, 0)) + scale_x_continuous(expand = c(0.01, 0)) + theme_ridges(grid = FALSE)