Download notebook (.ipynb)

Ridgeline plot#

Preparation#

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
iris_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv")
print(iris_df.shape)
iris_df.head()
(150, 5)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(mpg_df.shape)
mpg_df.head()
(234, 12)
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact

Plots#

Default plot#

ggplot(iris_df, aes("sepal_length", "species")) + \
    geom_area_ridges()

min_height parameter#

df = pd.DataFrame({
    "x": [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7],
    "y": [-.4, -.4, -.4, -.4, -.4, -.4, -.4, -.8, -.8, -.8, -.8, -.8, -.8, -.8],
    "h": [.4, -.2, .6, -.8, .3, .1, .7, .1, .3, .1, -.6, -.1, -.3, -.1],
})
ggplot(df) + \
    geom_area_ridges(aes("x", "y", height="h"), stat='identity', color="black", fill="#3182bd", min_height=-.4)

trim and tails_cutoff parameters#

gggrid([
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(trim=False, tails_cutoff=None) + \
        ggtitle("Default: trim=False, tails_cutoff=None"),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(trim=False, tails_cutoff=0) + \
        ggtitle("trim=False, tails_cutoff=0"),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(trim=False, tails_cutoff=3) + \
        ggtitle("trim=False, tails_cutoff=3"),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(trim=True) + \
        ggtitle("trim=True")
], ncol=2) + ggsize(800, 600)

scale parameter#

ggplot(iris_df, aes("sepal_length", "species")) + \
    geom_area_ridges(scale=1.5)

Quantiles#

quantiles = [.05, .25, .5, .75, .95]

gggrid([
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(quantiles=quantiles, color='black'),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(quantiles=quantiles, quantile_lines=True, color='black'),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(aes(fill="..quantile.."),
                         quantiles=quantiles, color='black', show_legend=False),
    ggplot(iris_df, aes("sepal_length", "species")) + \
        geom_area_ridges(aes(fill="..quantile.."),
                         quantiles=quantiles, quantile_lines=True,
                         color='black', show_legend=False),
], ncol=2) + ggsize(800, 600)

Other#

ggplot(iris_df, aes("sepal_length", "species")) + \
    geom_area_ridges(kernel="triangular", adjust=.8, quantile_lines=True, trim='all', \
                     color="#993404", fill="#fe9929", tooltips=layer_tooltips().line("height|@..height..")\
                    .format("@..density..", ".2f").line("density|@..density..")\
                    .line("quantile|@..quantile.."))
ggplot(mpg_df, aes("hwy", as_discrete("year"), fill="drv")) + \
    geom_area_ridges(color="white", alpha=.5) + \
    scale_y_discrete(format="d") + \
    facet_grid(x="drv") + \
    theme_bw() + flavor_darcula()