{ "cells": [ { "cell_type": "markdown", "id": "ac09fa5f", "metadata": {}, "source": [ "# Ridgeline plot" ] }, { "cell_type": "markdown", "id": "69c79ad3", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": 1, "id": "32f79dd9", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:56:59.070125Z", "iopub.status.busy": "2024-08-23T10:56:59.068923Z", "iopub.status.idle": "2024-08-23T10:57:01.762788Z", "shell.execute_reply": "2024-08-23T10:57:01.762517Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "91f59d82", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:01.764714Z", "iopub.status.busy": "2024-08-23T10:57:01.764473Z", "iopub.status.idle": "2024-08-23T10:57:01.801981Z", "shell.execute_reply": "2024-08-23T10:57:01.801612Z" } }, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.8.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.4.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "9a29719b", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:01.803723Z", "iopub.status.busy": "2024-08-23T10:57:01.803483Z", "iopub.status.idle": "2024-08-23T10:57:02.533537Z", "shell.execute_reply": "2024-08-23T10:57:02.533708Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":5,\"columns\":[\"sepal_length\",\"sepal_width\",\"petal_length\",\"petal_width\",\"species\"],\"kotlin_dataframe\":[{\"sepal_length\":5.1,\"sepal_width\":3.5,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.9,\"sepal_width\":3.0,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.7,\"sepal_width\":3.2,\"petal_length\":1.3,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.6,\"sepal_width\":3.1,\"petal_length\":1.5,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":5.0,\"sepal_width\":3.6,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 5

\n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
5.1000003.5000001.4000000.200000setosa
4.9000003.0000001.4000000.200000setosa
4.7000003.2000001.3000000.200000setosa
4.6000003.1000001.5000000.200000setosa
5.0000003.6000001.4000000.200000setosa
\n", " \n", " \n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val iris = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/iris.csv\")\n", "iris.head()\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "c465a300", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:02.535719Z", "iopub.status.busy": "2024-08-23T10:57:02.535475Z", "iopub.status.idle": "2024-08-23T10:57:02.562172Z", "shell.execute_reply": "2024-08-23T10:57:02.561907Z" } }, "outputs": [], "source": [ "val irisData = iris.toMap()" ] }, { "cell_type": "code", "execution_count": 5, "id": "e334ffdf", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:02.563995Z", "iopub.status.busy": "2024-08-23T10:57:02.563734Z", "iopub.status.idle": "2024-08-23T10:57:02.822687Z", "shell.execute_reply": "2024-08-23T10:57:02.822858Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", "
untitledmanufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.80000019994auto(l5)f1829pcompact
2audia41.80000019994manual(m5)f2129pcompact
3audia42.00000020084manual(m6)f2031pcompact
4audia42.00000020084auto(av)f2130pcompact
5audia42.80000019996auto(l5)f1626pcompact
\n", " \n", " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpg = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpg.head()\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "0f557034", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:02.824402Z", "iopub.status.busy": "2024-08-23T10:57:02.824173Z", "iopub.status.idle": "2024-08-23T10:57:02.847252Z", "shell.execute_reply": "2024-08-23T10:57:02.846933Z" } }, "outputs": [], "source": [ "val mpgData = mpg.toMap()" ] }, { "cell_type": "markdown", "id": "228e6443", "metadata": {}, "source": [ "## Plots" ] }, { "cell_type": "markdown", "id": "ad661de0", "metadata": {}, "source": [ "### Default plot" ] }, { "cell_type": "code", "execution_count": 7, "id": "d176e379", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:02.848760Z", "iopub.status.busy": "2024-08-23T10:57:02.848535Z", "iopub.status.idle": "2024-08-23T10:57:03.009107Z", "shell.execute_reply": "2024-08-23T10:57:03.009208Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(irisData) { x = \"sepal_length\"; y = \"species\" }\n", "\n", "p + geomAreaRidges()" ] }, { "cell_type": "markdown", "id": "68a21781", "metadata": {}, "source": [ "### `min_height` parameter" ] }, { "cell_type": "code", "execution_count": 8, "id": "06d918b3", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.010553Z", "iopub.status.busy": "2024-08-23T10:57:03.010401Z", "iopub.status.idle": "2024-08-23T10:57:03.076866Z", "shell.execute_reply": "2024-08-23T10:57:03.076658Z" } }, "outputs": [], "source": [ "val data = mapOf(\n", " \"x\" to listOf(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7),\n", " \"y\" to listOf(-.4, -.4, -.4, -.4, -.4, -.4, -.4, -.8, -.8, -.8, -.8, -.8, -.8, -.8),\n", " \"h\" to listOf(.4, -.2, .6, -.8, .3, .1, .7, .1, .3, .1, -.6, -.1, -.3, -.1)\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "ab2f2c40", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.078147Z", "iopub.status.busy": "2024-08-23T10:57:03.077999Z", "iopub.status.idle": "2024-08-23T10:57:03.136688Z", "shell.execute_reply": "2024-08-23T10:57:03.136794Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(data) +\n", " geomAreaRidges(stat = Stat.identity, color = \"black\", fill = \"#3182bd\", minHeight = -.4) {\n", " x = \"x\"; y = \"y\"; height = \"h\"\n", " }" ] }, { "cell_type": "markdown", "id": "973dbb61", "metadata": {}, "source": [ "### `trim` and `tailsCutoff` parameters" ] }, { "cell_type": "code", "execution_count": 10, "id": "75fd196c", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.138055Z", "iopub.status.busy": "2024-08-23T10:57:03.137863Z", "iopub.status.idle": "2024-08-23T10:57:03.256859Z", "shell.execute_reply": "2024-08-23T10:57:03.256630Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " p + geomAreaRidges(trim = false, tailsCutoff = null) + ggtitle(\"Default: trim=false, tailsCutoff=null\"),\n", " p + geomAreaRidges(trim = false, tailsCutoff = 0) + ggtitle(\"trim=false, tailsCutoff=0\"),\n", " p + geomAreaRidges(trim = false, tailsCutoff = 3) + ggtitle(\"trim=false, tailsCutoff=3\"),\n", " p + geomAreaRidges(trim = true) + ggtitle(\"trim=true\")\n", " ),\n", " ncol = 2,\n", " cellWidth = 400,\n", " cellHeight = 300\n", ")" ] }, { "cell_type": "markdown", "id": "81b0b390", "metadata": {}, "source": [ "### `scale` parameter" ] }, { "cell_type": "code", "execution_count": 11, "id": "923aa6f7", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.258747Z", "iopub.status.busy": "2024-08-23T10:57:03.258489Z", "iopub.status.idle": "2024-08-23T10:57:03.312413Z", "shell.execute_reply": "2024-08-23T10:57:03.312197Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomAreaRidges(scale = 1.5)" ] }, { "cell_type": "markdown", "id": "620232bf", "metadata": {}, "source": [ "### Quantiles" ] }, { "cell_type": "code", "execution_count": 12, "id": "682ad559", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.314408Z", "iopub.status.busy": "2024-08-23T10:57:03.314083Z", "iopub.status.idle": "2024-08-23T10:57:03.431160Z", "shell.execute_reply": "2024-08-23T10:57:03.430926Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val quantiles = listOf(.05, .25, .5, .75, .95)\n", "\n", "gggrid(\n", " plots = listOf(\n", " p + geomAreaRidges(quantiles = quantiles, color = \"black\"),\n", " p + geomAreaRidges(quantiles = quantiles, quantileLines = true, color = \"black\"),\n", " p + geomAreaRidges(quantiles = quantiles, color = \"black\", showLegend = false) { fill = \"..quantile..\" },\n", " p + geomAreaRidges(quantiles = quantiles, quantileLines = true, color = \"black\", showLegend = false) {\n", " fill = \"..quantile..\" \n", " }\n", " ),\n", " ncol = 2,\n", " cellWidth = 400,\n", " cellHeight = 300\n", ")" ] }, { "cell_type": "markdown", "id": "2bd6e69e", "metadata": {}, "source": [ "### Other" ] }, { "cell_type": "code", "execution_count": 13, "id": "acd7dc92", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.433066Z", "iopub.status.busy": "2024-08-23T10:57:03.432794Z", "iopub.status.idle": "2024-08-23T10:57:03.499399Z", "shell.execute_reply": "2024-08-23T10:57:03.499572Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomAreaRidges(kernel = \"triangular\", adjust = .8, quantileLines = true,\n", " color = \"#993404\", fill = \"#fe9929\", \n", " tooltips = layerTooltips()\n", " .line(\"height|@..height..\")\n", " .format(\"@..density..\", \".2f\")\n", " .line(\"density|@..density..\")\n", " .line(\"quantile|@..quantile..\"))" ] }, { "cell_type": "code", "execution_count": 14, "id": "e71f15fb", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:57:03.501518Z", "iopub.status.busy": "2024-08-23T10:57:03.501254Z", "iopub.status.idle": "2024-08-23T10:57:03.585310Z", "shell.execute_reply": "2024-08-23T10:57:03.585092Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpgData) { x = \"hwy\"; y = asDiscrete(\"year\"); fill = \"drv\" } +\n", " geomAreaRidges(color = \"white\", alpha = .5) +\n", " scaleFillBrewer(type = \"qual\", palette = \"Set1\") +\n", " facetGrid(x = \"drv\") +\n", " themeBW() + \n", " flavorDarcula()" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }