{ "cells": [ { "cell_type": "markdown", "id": "bd6be7c5", "metadata": {}, "source": [ "# Lollipop Plot\n", "\n", "A lollipop plot displays each element of a dataset as a segment and a circle. It is usually combined with the `count` stat, and is especially useful when you have several bars of the same height." ] }, { "cell_type": "markdown", "id": "c5f3557a", "metadata": {}, "source": [ "1. [Parameters `size`, `stroke` and `linewidth`](#stroke)\n", "\n", "2. [Parameter `fatten`](#fatten)\n", "\n", "3. [Horizontal Sticks](#direction)\n", "\n", "4. [Sloped Baseline](#slope)\n", "\n", "5. [Parameter `stat`](#stat)\n", "\n", "6. [Lollipops in Marginal Layer](#ggmarginal)\n", "\n", "7. [Lollipops and a Regression Line](#slope_and_intercept)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "ba071491", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "06477cad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "66de6718", "metadata": {}, "outputs": [ { "data": { "text/html": [ "DataFrame: rowsCount = 3, columnsCount = 12
\n", " \n", " \n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "df.head(3)" ] }, { "cell_type": "code", "execution_count": 12, "id": "66fac357", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " listOf(\n", " letsPlot(df.toMap()) { x = \"class\" } + geomLollipop(stat = Stat.count()) + ggtitle(\"stat='count'\"),\n", " letsPlot(df.toMap()) { x = \"hwy\" } + geomLollipop(stat = Stat.bin()) + ggtitle(\"stat='bin'\"),\n", " letsPlot(df.toMap()) { x = \"hwy\" } + geomLollipop(stat = Stat.density(n=30)) + ggtitle(\"stat='density'\")\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "aa9cd80d", "metadata": {}, "source": [ "\n", "\n", "#### 6. Lollipops in Marginal Layer" ] }, { "cell_type": "code", "execution_count": 13, "id": "d17028b4", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(df.toMap()) { x = \"hwy\"; y = \"cty\" } +\n", " geomBin2D(binWidth = 1 to 1) +\n", " ggmarginal(\"r\", size=.2,\n", " layer = geomLollipop(stat = Stat.count(), orientation = \"y\", size = 1) { color = \"..count..\" } )" ] }, { "cell_type": "markdown", "id": "7f91b9f7", "metadata": {}, "source": [ "\n", "\n", "#### 7. Lollipops and a Regression Line" ] }, { "cell_type": "code", "execution_count": 14, "id": "5ce3b9ac", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val xs = df[\"hwy\"].toList().map { (it as Number).toDouble() }\n", "val ys = df[\"cty\"].toList().map { (it as Number).toDouble() }\n", "\n", "fun linearModel(x: Double) = LinearRegression(xs, ys, 0.95).evalX(x).y\n", "\n", "val intercept = linearModel(0.0)\n", "val slope = linearModel(1.0) - intercept\n", "\n", "letsPlot(df.toMap()) { x = \"hwy\"; y = \"cty\" } +\n", " geomSmooth(level = 0.99) + \n", " geomLollipop(slope = slope, intercept = intercept,\n", " size = 1.2, shape = 21, color = \"black\", fill = \"magenta\") +\n", " coordFixed()" ] } ], "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.8.20" } }, "nbformat": 4, "nbformat_minor": 5 }