{ "cells": [ { "cell_type": "markdown", "id": "bca9d6e0", "metadata": {}, "source": [ "# Multi-line Labels in Facets\n", "\n", "\n", "The 'newline' character (`\\n`) now works as a 'line break' in the facet titles.
\n", "Automatic line breaking is performed according to the text length limit specified with parameters `labwidth` in `facet_wrap()` and `x_labwidth`/`y_labwidth` in `facet_grid()`.\n", "\n", "The margins around the facet titles are controlled by the `margin` parameter in `element_text()`.
\n", "Horizontal and vertical justifications - using `hjust` and `vjust` parameter in `element_text()`." ] }, { "cell_type": "code", "execution_count": 1, "id": "7a35b17a", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "d9dc5b7b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "87276233", "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame.from_records([\n", " (\"pet\", \"cat\", 5, \"carnivore\"),\n", " (\"pet\", \"dog\", 10, \"carnivore\"),\n", " (\"pet\", \"rabbit\", 2, \"herbivore\"),\n", " (\"pet\", \"hamster\", 1, \"herbivore\"),\n", "\n", " (\"farm animal\", \"cow\", 500, \"herbivore\"),\n", " (\"farm animal\", \"pig\", 100, \"carnivore\"),\n", " (\"farm animal\", \"horse\", 700, \"herbivore\"),\n", "])\n", "data.columns = (\"animal_type\", \"animal\", \"weight\", \"diet\")" ] }, { "cell_type": "markdown", "id": "c90e6bbc", "metadata": {}, "source": [ "### Facet Labels" ] }, { "cell_type": "code", "execution_count": 4, "id": "cb015bd8", "metadata": {}, "outputs": [], "source": [ "p = ggplot(data, aes(x=\"animal\", y=\"weight\")) + \\\n", " geom_bar(stat=\"identity\") + \\\n", " theme_bw() + \\\n", " theme(panel_grid_minor=element_blank())" ] }, { "cell_type": "markdown", "id": "d0d696e1", "metadata": {}, "source": [ "#### Default" ] }, { "cell_type": "code", "execution_count": 5, "id": "ad819c31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_wrap(facets=\"animal_type\", ncol=2, scales=\"free\")" ] }, { "cell_type": "markdown", "id": "992af030", "metadata": {}, "source": [ "#### Wrap Labels using `labwidth`-parameters" ] }, { "cell_type": "code", "execution_count": 6, "id": "889c7ca6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_wrap(facets=\"animal_type\", ncol=2, scales=\"free\", labwidth=6)" ] }, { "cell_type": "code", "execution_count": 7, "id": "eda24884", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free\", x_labwidth=6)" ] }, { "cell_type": "markdown", "id": "e5fd2900", "metadata": {}, "source": [ "#### Line Breaks Using `\\n` in Facet Values" ] }, { "cell_type": "code", "execution_count": 8, "id": "d08fb52b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
animal_typeanimalweightdiet
0petcat5carnivore
1petdog10carnivore
2petrabbit2herbivore
3pethamster1herbivore
4farm\\nanimalcow500herbivore
5farm\\nanimalpig100carnivore
6farm\\nanimalhorse700herbivore
\n", "
" ], "text/plain": [ " animal_type animal weight diet\n", "0 pet cat 5 carnivore\n", "1 pet dog 10 carnivore\n", "2 pet rabbit 2 herbivore\n", "3 pet hamster 1 herbivore\n", "4 farm\\nanimal cow 500 herbivore\n", "5 farm\\nanimal pig 100 carnivore\n", "6 farm\\nanimal horse 700 herbivore" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[\"animal_type\"] = data[\"animal_type\"].str.replace(' ', '\\n')\n", "data" ] }, { "cell_type": "code", "execution_count": 9, "id": "9f62f8fb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "47d12537", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=4, scales=\"free\")" ] }, { "cell_type": "markdown", "id": "9fc96ba4", "metadata": {}, "source": [ "### Text Justifications" ] }, { "cell_type": "code", "execution_count": 11, "id": "47db58e9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=4, scales=\"free\") + \\\n", " theme(strip_text=element_text(hjust=1, vjust=1))" ] }, { "cell_type": "markdown", "id": "bede7550", "metadata": {}, "source": [ "### Margins Around the Text Element" ] }, { "cell_type": "code", "execution_count": 12, "id": "eb488984", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free\") + \\\n", " theme(strip_text=element_text(margin=[10, 30], hjust=0, vjust=1))" ] } ], "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.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }