{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Annotated Barchart\n", "\n", "Use the `labels` parameter to create an annotated Barchart." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "DataFrame: rowsCount = 3, columnsCount = 12
\n", " \n", " \n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpg = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "val mpgData = mpg.toMap()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure Layer Labels Using API Similar to the Tooltip Configuration API" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " listOf(\n", " letsPlot(mpgData) + geomBar(labels = layerLabels().line(\"@..count..\")) { x = \"trans\" },\n", " letsPlot(mpgData) + geomBar(\n", " stat = Stat.sum(), size = 0,\n", " labels = layerLabels().line(\"@..proppct..\")\n", " ) { x = \"trans\"; y = \"..n..\" } + coordFlip()\n", " ), \n", " widths = listOf(1, 1.2)\n", ") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Stacked Bars" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) { x = \"class\"; fill = \"drv\" } +\n", " geomBar(labels = layerLabels().line(\"@..proppct..\").format(\"..proppct..\", \"{d} %\")) +\n", " ggsize(700, 400)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) { x = \"class\"; fill = \"drv\" } +\n", " geomBar(labels = layerLabels().line(\"@..proppct..\").format(\"..proppct..\", \"{d} %\"),\n", " position = positionFill()) +\n", " coordFlip() +\n", " ggsize(700, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Narrow Bars\n", "Too thin for labels to fit in. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) { x = \"class\"; fill = \"drv\" } +\n", " geomBar(labels = layerLabels().line(\"@..proppct..\").format(\"..proppct..\", \"{d} %\"),\n", " position = positionDodge()) +\n", " ggsize(700, 400)" ] } ], "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": 4 }