{ "cells": [ { "cell_type": "markdown", "id": "599ab589-6b5d-4429-9a6b-8ba816a7b227", "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 `facetWrap()` and `xLabWidth`/`yLabWidth` in `facetGrid()`.\n", "\n", "The margins around the facet titles are controlled by the `margin` parameter in `elementText()`.
\n", "Horizontal and vertical justifications - using `hjust` and `vjust` parameter in `elementText()`." ] }, { "cell_type": "code", "execution_count": 1, "id": "c78e2aa3-1187-4203-b544-4e20989fcddb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "47d3905a-13b3-4c17-aa7a-1eaf46e57d7f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.7.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "83b526d2-33ab-445f-813d-8e9cc94edb03", "metadata": {}, "outputs": [], "source": [ "val df = dataFrameOf(\"animal_type\", \"animal\", \"weight\", \"diet\")(\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", ")" ] }, { "cell_type": "markdown", "id": "e0f1f949-c7ce-4e5f-af6e-ff49edbd0200", "metadata": {}, "source": [ "### Facet Labels" ] }, { "cell_type": "code", "execution_count": 4, "id": "f50bf5aa-601b-4aba-b2ad-ef26104cbc0f", "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(df.toMap()) { x = \"animal\"; y = \"weight\" } +\n", " geomBar(stat = Stat.identity) +\n", " themeBW() +\n", " theme(panelGridMinor = elementBlank())" ] }, { "cell_type": "markdown", "id": "3a94fa6c-2a6e-4e2c-9f6f-ebfe69f1798b", "metadata": {}, "source": [ "#### Default" ] }, { "cell_type": "code", "execution_count": 5, "id": "c30e5c0d-9e18-4d33-ae01-79c3eeb2c9fe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetWrap(facets = \"animal_type\", ncol = 2, scales = \"free\")" ] }, { "cell_type": "markdown", "id": "2eb94675-7816-4856-8ac1-4d085ae24a81", "metadata": {}, "source": [ "#### Wrap Labels using `labWidth`-parameters" ] }, { "cell_type": "code", "execution_count": 6, "id": "ae99612c-c1b0-43ac-8e53-da66d29184a4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetWrap(facets = \"animal_type\", ncol = 2, scales = \"free\", labWidth = 6)" ] }, { "cell_type": "code", "execution_count": 7, "id": "741e10bd-bd04-4ace-a409-04ba2c3a59bf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\", xLabWidth = 6)" ] }, { "cell_type": "markdown", "id": "bbb9d350-f3c3-4d18-9827-f93657b60f60", "metadata": {}, "source": [ "#### Line Breaks Using `\\n` in Facet Values" ] }, { "cell_type": "code", "execution_count": 8, "id": "cb7b453d-7da4-4706-86f7-d952e9c38607", "metadata": {}, "outputs": [], "source": [ "val df2 = df.update { animal_type }.with { it.replace(' ', '\\n') }\n", "\n", "val p2 = letsPlot(df2.toMap()) { x = \"animal\"; y = \"weight\" } +\n", " geomBar(stat = Stat.identity) +\n", " themeBW() +\n", " theme(panelGridMinor = elementBlank())" ] }, { "cell_type": "code", "execution_count": 9, "id": "c6db2315-cc50-4e69-93aa-90f09fc4da65", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "dbeea0ea-3d60-49a6-a0de-2d3ace1051d7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + facetWrap(facets = listOf(\"animal_type\", \"diet\"), ncol = 4, scales = \"free\")" ] }, { "cell_type": "markdown", "id": "6c8bfdd8-cc93-4831-8d64-3eecc13c794b", "metadata": {}, "source": [ "### Text Justifications" ] }, { "cell_type": "code", "execution_count": 11, "id": "4d359ce4-1901-478a-bdd9-379b40984027", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + facetWrap(facets = listOf(\"animal_type\", \"diet\"), ncol = 4, scales = \"free\") +\n", " theme(stripText = elementText(hjust = 1, vjust = 1))" ] }, { "cell_type": "markdown", "id": "7db2d0bc-bf84-4c7e-aaba-9a6c73ee9db6", "metadata": {}, "source": [ "### Margins Around the Text Element" ] }, { "cell_type": "code", "execution_count": 12, "id": "614f608a-aba4-4ecc-b961-81e715330ddf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\") +\n", " theme(stripText = elementText(margin = listOf(10, 30), hjust = 0, vjust = 1))" ] } ], "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.9.10" } }, "nbformat": 4, "nbformat_minor": 5 }