{ "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": [ "| \n", " | animal_type | \n", "animal | \n", "weight | \n", "diet | \n", "
|---|---|---|---|---|
| 0 | \n", "pet | \n", "cat | \n", "5 | \n", "carnivore | \n", "
| 1 | \n", "pet | \n", "dog | \n", "10 | \n", "carnivore | \n", "
| 2 | \n", "pet | \n", "rabbit | \n", "2 | \n", "herbivore | \n", "
| 3 | \n", "pet | \n", "hamster | \n", "1 | \n", "herbivore | \n", "
| 4 | \n", "farm\\nanimal | \n", "cow | \n", "500 | \n", "herbivore | \n", "
| 5 | \n", "farm\\nanimal | \n", "pig | \n", "100 | \n", "carnivore | \n", "
| 6 | \n", "farm\\nanimal | \n", "horse | \n", "700 | \n", "herbivore | \n", "