#!/usr/bin/env python # coding: utf-8 # # Ridgeline plot # ## Preparation # In[1]: import pandas as pd from lets_plot import * from lets_plot.mapping import as_discrete LetsPlot.setup_html() # In[2]: iris_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv") print(iris_df.shape) iris_df.head() # In[3]: mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") print(mpg_df.shape) mpg_df.head() # ## Plots # ### Default plot # In[4]: ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges() # ### `min_height` parameter # In[5]: 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], }) # In[6]: ggplot(df) + \ geom_area_ridges(aes("x", "y", height="h"), stat='identity', color="black", fill="#3182bd", min_height=-.4) # ### `trim` and `tails_cutoff` parameters # In[7]: width, height = 400, 300 bunch = GGBunch() bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(trim=False, tails_cutoff=None) + \ ggtitle("Default: trim=False, tails_cutoff=None"), 0, 0, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(trim=False, tails_cutoff=0) + \ ggtitle("trim=False, tails_cutoff=0"), width, 0, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(trim=False, tails_cutoff=3) + \ ggtitle("trim=False, tails_cutoff=3"), 0, height, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(trim=True) + \ ggtitle("trim=True"), width, height, width, height) bunch.show() # ### `scale` parameter # In[8]: ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(scale=1.5) # ### Quantiles # In[9]: quantiles = [.05, .25, .5, .75, .95] width, height = 400, 300 bunch = GGBunch() bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(quantiles=quantiles, color='black'), 0, 0, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(quantiles=quantiles, quantile_lines=True, color='black'), width, 0, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(aes(fill="..quantile.."), quantiles=quantiles, color='black', show_legend=False), 0, height, width, height) bunch.add_plot(ggplot(iris_df, aes("sepal_length", "species")) + \ geom_area_ridges(aes(fill="..quantile.."), quantiles=quantiles, quantile_lines=True, color='black', show_legend=False), width, height, width, height) bunch.show() # ### Other # In[10]: 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..")) # In[11]: ggplot(mpg_df, aes("hwy", as_discrete("year"), fill="drv")) + \ geom_area_ridges(color="white", alpha=.5) + \ facet_grid(x="drv") + \ theme_bw() + flavor_darcula()