{ "cells": [ { "cell_type": "markdown", "id": "f162df66-e14a-4bd5-83d0-4364521c9cee", "metadata": {}, "source": [ "# Layer Labels (Annotations) in `geom_crossbar`" ] }, { "cell_type": "code", "execution_count": 1, "id": "bd9d5917-f099-423e-9184-70dc95e2c703", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "7c946601-4186-464b-a449-ef5a737b3ef8", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'vertical_category': ['Without midline', 'Above midline', 'Below midline', 'Above bar', 'Below bar'],\n", " 'horisontal_category': ['Without midline', 'Right of midline', 'Left of midline', 'Right of bar', 'Left of bar'],\n", " 'ymin': [0, 5, 5, 0, 20],\n", " 'ymax': [20, 20, 20, 2, 22],\n", " 'y': [None, 6, 19, 1, 21],\n", " 'value': [20, 15, 15, 2, 2]\n", "}" ] }, { "cell_type": "markdown", "id": "fb64f03d-9f79-4e91-8677-0a1c761a6b72", "metadata": {}, "source": [ "### Automatic Text Positioning" ] }, { "cell_type": "code", "execution_count": 3, "id": "2cd062c4-cb02-43db-9b34-81ca0627962c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + \\\n", " geom_crossbar(\n", " aes(x='vertical_category', y='y', ymin='ymin', ymax='ymax'), \n", " labels=layer_labels([\"value\"]).size(20))" ] }, { "cell_type": "code", "execution_count": 4, "id": "956e27a2-93b2-4724-b671-32c60bb1e370", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + coord_flip() + \\\n", " geom_crossbar(\n", " aes(x='horisontal_category', y='y', ymin='ymin', ymax='ymax'), \n", " labels=layer_labels([\"value\"]).size(20))" ] }, { "cell_type": "markdown", "id": "0b1c219f-cb1d-40de-baa7-1beb6da4c945", "metadata": {}, "source": [ "### Long Labels Handling\n", "Lond text strings that don't fit in the bar are drawn with a background." ] }, { "cell_type": "code", "execution_count": 5, "id": "51b49632-ac1d-4c87-ab8c-2a3eec2ac8bd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + \\\n", " geom_crossbar(\n", " aes(x='vertical_category', y='y', ymin='ymin', ymax='ymax'), \n", " fill='lightblue',\n", " labels=layer_labels([\"vertical_category\"]).size(20))" ] }, { "cell_type": "markdown", "id": "f913e97f-9198-48d0-92fc-9032017c2402", "metadata": {}, "source": [ "### Inheriting the Geometry Colors" ] }, { "cell_type": "code", "execution_count": 6, "id": "a6ded5b4-2e6b-4eb3-b343-9b03aed67302", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + \\\n", " geom_crossbar(\n", " aes(x='vertical_category', y='y', ymin='ymin', ymax='ymax', color=\"vertical_category\"), \n", " show_legend=False,\n", " labels=layer_labels([\"value\"])\n", " .size(20)\n", " .inherit_color() # <--- Enable using the geometry color in labels.\n", " ) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }