{ "cells": [ { "cell_type": "markdown", "id": "3b3c31f8-7f05-4b50-be2d-cfd85781c62b", "metadata": {}, "source": [ "# Text Geoms\n", "\n", "Text geoms are useful for labeling plots. They can be used by themselves or in combination with other geoms. \n", "\n", "1. [`geom_text()`/`geom_label()`](#1.-geom_text()/geom_label())\n", "\n", "2. [The `label_format` Parameter](#2.-The-label_format-Parameter)\n", "\n", "3. [Support of Multiple Lines](#3.-Support-of-Multiple-Lines)\n", "\n", " 3.1. [Change Lineheight](#3.1.-Change-Lineheight)\n", "\n", "4. [Rotation and Alignment](#4.-Rotation-and-Alignment)\n", "\n", " 4.1. [Adjust Position by Nudging a Given Offset](#4.1.-Adjust-Position-by-Nudging-a-Given-Offset)\n", "\n", " 4.2. [Move Text - Use `position_nudge`](#4.2.-Move-Text---Use-position_nudge)\n", "\n", " 4.3. [Move Text - Use `nudge_y` Parameter](#4.3.-Move-Text---Use-nudge_y-Parameter)\n", "\n", " 4.4. [Justification: `inward` and `outward`](#4.4.-Justification:-inward-and-outward)\n", "\n", "5. [GeoDataFrame in `geom_text()`/`geom_label()`](#5.-GeoDataFrame-in-geom_text()/geom_label())\n", "\n", "6. [Text on Livemap](#6.-Text-on-Livemap)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "victorian-aberdeen", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:10.478600Z", "iopub.status.busy": "2024-04-26T12:07:10.478600Z", "iopub.status.idle": "2024-04-26T12:07:11.506807Z", "shell.execute_reply": "2024-04-26T12:07:11.506095Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] } ], "source": [ "from itertools import product\n", "\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot import tilesets\n", "from lets_plot.geo_data import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "id": "61246a0a-c42d-42f7-a0d7-0f68909ee627", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.506807Z", "iopub.status.busy": "2024-04-26T12:07:11.506807Z", "iopub.status.idle": "2024-04-26T12:07:11.521934Z", "shell.execute_reply": "2024-04-26T12:07:11.521934Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "0b1cec35-d9b0-47a2-a9f1-5866950ca56f", "metadata": {}, "source": [ "# 1. `geom_text()`/`geom_label()`\n", "\n", "- `geom_text()` adds a text directly to the plot.\n", "- `geom_label()` adds a text directly to the plot with a rectangle behind the text, making it easier to read." ] }, { "cell_type": "code", "execution_count": 3, "id": "differential-tsunami", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.521934Z", "iopub.status.busy": "2024-04-26T12:07:11.521934Z", "iopub.status.idle": "2024-04-26T12:07:11.553371Z", "shell.execute_reply": "2024-04-26T12:07:11.553371Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "label_plot = ggplot() + geom_label(x=0, y=0, label='Lorem ipsum', size=14)\n", "text_plot = ggplot() + geom_text(x=0, y=0, label='Lorem ipsum')\n", "\n", "gggrid([label_plot, text_plot]) + ggsize(600, 300)" ] }, { "cell_type": "markdown", "id": "f1aff276-d0e7-4303-b5e4-8bfe6131861b", "metadata": {}, "source": [ "Change additional parameters." ] }, { "cell_type": "code", "execution_count": 4, "id": "atmospheric-digit", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.557065Z", "iopub.status.busy": "2024-04-26T12:07:11.557065Z", "iopub.status.idle": "2024-04-26T12:07:11.570157Z", "shell.execute_reply": "2024-04-26T12:07:11.569241Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_label(x=0, y=0, label='Lorem ipsum', size=.9, size_unit='y', \\\n", " fill='#edf8e9', color='#238b45', fontface='bold', \\\n", " label_padding=1.0, label_r=0.5, label_size=2.0) + \\\n", " ggsize(500, 200)" ] }, { "cell_type": "markdown", "id": "2945f7d3-72bb-4ab8-bb11-608842d8677f", "metadata": {}, "source": [ "Use different fonts for labels or text." ] }, { "cell_type": "code", "execution_count": 5, "id": "plain-banana", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.570157Z", "iopub.status.busy": "2024-04-26T12:07:11.570157Z", "iopub.status.idle": "2024-04-26T12:07:11.586561Z", "shell.execute_reply": "2024-04-26T12:07:11.585302Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "families = [\n", " 'Arial',\n", " 'Calibri', \n", " 'Garamond',\n", " 'Geneva',\n", " 'Georgia',\n", " 'Helvetica',\n", " 'Lucida Grande',\n", " 'Rockwell',\n", " 'Times New Roman',\n", " 'Verdana',\n", " 'sans-serif',\n", " 'serif',\n", " 'monospace',\n", "]\n", "ggplot() + \\\n", " geom_label(aes(y=list(range(len(families))), \\\n", " label=families, family=families), \\\n", " size=10, label_padding=0, label_r=0)" ] }, { "cell_type": "markdown", "id": "5dc10115-0ed8-4289-bac4-3b0321cb0548", "metadata": {}, "source": [ "Add aesthetic parameters." ] }, { "cell_type": "code", "execution_count": 6, "id": "cosmetic-employee", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.586561Z", "iopub.status.busy": "2024-04-26T12:07:11.586561Z", "iopub.status.idle": "2024-04-26T12:07:11.712701Z", "shell.execute_reply": "2024-04-26T12:07:11.712701Z" } }, "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", " \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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv')\n", "mpg_df.head()" ] }, { "cell_type": "code", "execution_count": 7, "id": "lined-series", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.712701Z", "iopub.status.busy": "2024-04-26T12:07:11.712701Z", "iopub.status.idle": "2024-04-26T12:07:11.744672Z", "shell.execute_reply": "2024-04-26T12:07:11.744672Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df, aes('cty', 'hwy')) + geom_label(aes(label='fl'))" ] }, { "cell_type": "code", "execution_count": 8, "id": "communist-welding", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.744672Z", "iopub.status.busy": "2024-04-26T12:07:11.744672Z", "iopub.status.idle": "2024-04-26T12:07:11.776300Z", "shell.execute_reply": "2024-04-26T12:07:11.776300Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df, aes('cty', 'hwy')) + \\\n", " geom_label(aes(label='fl', fill=as_discrete('cyl')), color='white')" ] }, { "cell_type": "markdown", "id": "ddd77869-1b5f-4251-b7f2-114b0eda9b0d", "metadata": {}, "source": [ "## 2. The `label_format` Parameter\n", "\n", "The `label_format` parameter specifies template for transforming value of the `label` aesthetic to a string." ] }, { "cell_type": "code", "execution_count": 9, "id": "ebeaad9f-5270-47c5-ab7b-ca0d9f8d4539", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.776300Z", "iopub.status.busy": "2024-04-26T12:07:11.776300Z", "iopub.status.idle": "2024-04-26T12:07:11.793209Z", "shell.execute_reply": "2024-04-26T12:07:11.792065Z" } }, "outputs": [], "source": [ "values_data = {\n", " 'y': list(range(5)),\n", " 'z': [1.0/3, 12.5/7, -22.5/11, 2.5/7, 31.67/1.77 ],\n", " 's': ['one', 'two', 'three', 'four', 'five']\n", "}" ] }, { "cell_type": "markdown", "id": "e644d615-0f45-4923-b92c-aa99259b3e17", "metadata": {}, "source": [ "Floating point numbers without formatting." ] }, { "cell_type": "code", "execution_count": 10, "id": "cdfa02b2-6dee-4e47-a4b7-b7671d1e9206", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.793209Z", "iopub.status.busy": "2024-04-26T12:07:11.793209Z", "iopub.status.idle": "2024-04-26T12:07:11.807847Z", "shell.execute_reply": "2024-04-26T12:07:11.807847Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(values_data) + geom_text(aes(y='y', label='z'))" ] }, { "cell_type": "markdown", "id": "5df4241c-3f4b-4fc7-b86e-9c5a611360f2", "metadata": {}, "source": [ "Floating point numbers with formatting." ] }, { "cell_type": "code", "execution_count": 11, "id": "63a637e2-5606-472c-b0bc-0b9b4fab440e", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.807847Z", "iopub.status.busy": "2024-04-26T12:07:11.807847Z", "iopub.status.idle": "2024-04-26T12:07:11.825518Z", "shell.execute_reply": "2024-04-26T12:07:11.823719Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='.3f')" ] }, { "cell_type": "markdown", "id": "b3b18569-3bdd-474f-9176-71a66d7ca35d", "metadata": {}, "source": [ "Numbers formatted as percentile values" ] }, { "cell_type": "code", "execution_count": 12, "id": "add160ac-9be4-4c86-85d3-cc2a8714bc92", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.825518Z", "iopub.status.busy": "2024-04-26T12:07:11.825518Z", "iopub.status.idle": "2024-04-26T12:07:11.839556Z", "shell.execute_reply": "2024-04-26T12:07:11.839556Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='.2%')" ] }, { "cell_type": "markdown", "id": "91406e17-c291-401b-ad0a-0ac62447d1e2", "metadata": {}, "source": [ "Number format as a part of a string pattern." ] }, { "cell_type": "code", "execution_count": 13, "id": "b0141119-8600-4ac5-ac01-6cc6f2f8b200", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.839556Z", "iopub.status.busy": "2024-04-26T12:07:11.839556Z", "iopub.status.idle": "2024-04-26T12:07:11.855377Z", "shell.execute_reply": "2024-04-26T12:07:11.855377Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(values_data) + geom_text(aes(y='y', label='z'), label_format='Ttl: ${.2f} (B)')" ] }, { "cell_type": "markdown", "id": "600723f9-5bbe-42f1-81ee-89e6365c417f", "metadata": {}, "source": [ "String pattern without value formatting." ] }, { "cell_type": "code", "execution_count": 14, "id": "2350b46d-cd07-452d-aa65-050bd1789331", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.855377Z", "iopub.status.busy": "2024-04-26T12:07:11.855377Z", "iopub.status.idle": "2024-04-26T12:07:11.871681Z", "shell.execute_reply": "2024-04-26T12:07:11.871681Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(values_data) + geom_text(aes(y='y', label='s'), label_format='\"{}\"')" ] }, { "cell_type": "markdown", "id": "776d0e84-965f-4b26-bf59-287111dd8c72", "metadata": {}, "source": [ "## 3. Support of Multiple Lines" ] }, { "cell_type": "code", "execution_count": 15, "id": "6c2d7351-27d8-442d-88f5-03507603150b", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.871681Z", "iopub.status.busy": "2024-04-26T12:07:11.871681Z", "iopub.status.idle": "2024-04-26T12:07:11.888559Z", "shell.execute_reply": "2024-04-26T12:07:11.887582Z" } }, "outputs": [], "source": [ "data1 = {\n", " 'hjust': [0, 0.5, 1],\n", " 'vjust': [0, 0.5, 1],\n", " 'angle': [0, 30],\n", " 'label': ['first line\\nsecond line']\n", "}\n", "\n", "df1 = pd.DataFrame(product(*data1.values()), columns=data1.keys())\n", "\n", "p1 = ggplot(df1, aes(x='hjust', y='vjust')) + \\\n", " geom_point(size=3) + \\\n", " theme_light() + \\\n", " theme(panel_grid=element_blank())\n", "\n", "p1_facets = p1 + \\\n", " scale_x_continuous(breaks=[0, 0.5, 1]) + \\\n", " scale_y_continuous(breaks=[0, 0.5, 1], expand=[0.4]) + \\\n", " facet_grid(x='angle', x_format='{d}°')" ] }, { "cell_type": "code", "execution_count": 16, "id": "49c2a4f6-cc9f-41a5-a91b-510cb1d5eff6", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.888559Z", "iopub.status.busy": "2024-04-26T12:07:11.888559Z", "iopub.status.idle": "2024-04-26T12:07:11.903697Z", "shell.execute_reply": "2024-04-26T12:07:11.903697Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1_facets + geom_text(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9)" ] }, { "cell_type": "code", "execution_count": 17, "id": "e5084d0f-8798-4ef8-a7cf-0709731d7b90", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.903697Z", "iopub.status.busy": "2024-04-26T12:07:11.903697Z", "iopub.status.idle": "2024-04-26T12:07:11.920664Z", "shell.execute_reply": "2024-04-26T12:07:11.919526Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1_facets + geom_label(aes(label='label', hjust='hjust', vjust='vjust', angle='angle'), size=9, alpha=0.5)" ] }, { "cell_type": "markdown", "id": "0520c8fe-19e8-4c50-9569-bae6914075fc", "metadata": {}, "source": [ "### 3.1. Change Lineheight" ] }, { "cell_type": "code", "execution_count": 18, "id": "584d8e6c-8668-4449-b73a-d1b2b688ccdb", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.920664Z", "iopub.status.busy": "2024-04-26T12:07:11.920664Z", "iopub.status.idle": "2024-04-26T12:07:11.935312Z", "shell.execute_reply": "2024-04-26T12:07:11.935312Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p11 = p1 + \\\n", " geom_text(aes(label='label', hjust='hjust', vjust='vjust'), \\\n", " size=8, lineheight=0.7) + \\\n", " ggtitle('lineheight=0.7')\n", "p12 = p1 + \\\n", " geom_text(aes(label='label', hjust='hjust', vjust='vjust'), \\\n", " size=8, lineheight=2) + \\\n", " ggtitle('lineheight=2')\n", "\n", "gggrid([p11, p12]) " ] }, { "cell_type": "code", "execution_count": 19, "id": "1e8e7734-81ee-4e5d-9d68-407ac6ce6fb2", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.935312Z", "iopub.status.busy": "2024-04-26T12:07:11.935312Z", "iopub.status.idle": "2024-04-26T12:07:11.951112Z", "shell.execute_reply": "2024-04-26T12:07:11.951112Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p13 = p1 + \\\n", " geom_label(aes(label='label', hjust='hjust', vjust='vjust'), \\\n", " size=8, alpha=0.5, lineheight=0.7) + \\\n", " ggtitle('lineheight=0.7')\n", "p14 = p1 + \\\n", " geom_label(aes(label='label', hjust='hjust', vjust='vjust'), \\\n", " size=8, alpha=0.5, lineheight=2) + \\\n", " ggtitle('lineheight=2')\n", "\n", "gggrid([p13, p14]) " ] }, { "cell_type": "markdown", "id": "cb34ed8c-fab4-47de-867f-42aea847fabc", "metadata": {}, "source": [ "## 4. Rotation and Alignment" ] }, { "cell_type": "code", "execution_count": 20, "id": "674f4bfa-fff8-41e8-9588-25796ab2ca96", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.952717Z", "iopub.status.busy": "2024-04-26T12:07:11.952717Z", "iopub.status.idle": "2024-04-26T12:07:11.966924Z", "shell.execute_reply": "2024-04-26T12:07:11.966924Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = {\n", " 'hjust': [0, 0.5, 1],\n", " 'vjust': [0, 0.5, 1],\n", " 'angle': [0, 45, 90],\n", " 'text' : ['Text'] \n", "}\n", "\n", "df2 = pd.DataFrame(product(*data2.values()), columns=data2.keys())\n", "\n", "ggplot(df2, aes(x='hjust', y='vjust')) + \\\n", " geom_point(size=3) + \\\n", " geom_label(aes(label='text', angle='angle', hjust='hjust', vjust='vjust'), size=9) + \\\n", " facet_grid(y='angle') + \\\n", " scale_x_continuous(breaks=[0, 0.5, 1], expand=[0.1]) + \\\n", " scale_y_continuous(breaks=[0, 0.5, 1], expand=[0.0, 0.5]) + \\\n", " theme_classic() + \\\n", " theme(panel_border=element_rect(size=1))" ] }, { "cell_type": "markdown", "id": "405e861e-e1dd-4de5-b90e-d7ea4fb0feff", "metadata": {}, "source": [ "### 4.1. Adjust Position by Nudging a Given Offset" ] }, { "cell_type": "code", "execution_count": 21, "id": "997eb978-b865-48ea-9387-b3f3f6e1c20c", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.969741Z", "iopub.status.busy": "2024-04-26T12:07:11.969741Z", "iopub.status.idle": "2024-04-26T12:07:11.982777Z", "shell.execute_reply": "2024-04-26T12:07:11.982777Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 = ggplot({'x': ['a', 'b', 'c'], 'y': [1.2, 3.4, 2.5]}, aes('x', 'y')) + \\\n", " geom_point(size=4) + \\\n", " ggsize(500, 300)\n", "\n", "p2 + geom_text(aes(label='y'))" ] }, { "cell_type": "markdown", "id": "07727be3-fd9b-4a28-938d-35f6c53cee10", "metadata": {}, "source": [ "### 4.2. Move Text - Use `position_nudge`" ] }, { "cell_type": "code", "execution_count": 22, "id": "0d38f47d-8514-441b-8925-724d06cc95f0", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.982777Z", "iopub.status.busy": "2024-04-26T12:07:11.982777Z", "iopub.status.idle": "2024-04-26T12:07:11.998454Z", "shell.execute_reply": "2024-04-26T12:07:11.998454Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + geom_text(aes(label='y'), position=position_nudge(y=0.2))" ] }, { "cell_type": "markdown", "id": "e958df0e-72aa-4430-86a8-ddeb69ed4d68", "metadata": {}, "source": [ "### 4.3. Move Text - Use `nudge_y` Parameter" ] }, { "cell_type": "code", "execution_count": 23, "id": "82b4ed50-108d-4dd0-aeed-43d1f9323ce8", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:11.998454Z", "iopub.status.busy": "2024-04-26T12:07:11.998454Z", "iopub.status.idle": "2024-04-26T12:07:12.014079Z", "shell.execute_reply": "2024-04-26T12:07:12.014079Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + geom_text(aes(label='y'), nudge_y=0.2)" ] }, { "cell_type": "markdown", "id": "bf236523-add6-4926-8cc8-b5c4cf708f75", "metadata": {}, "source": [ "### 4.4. Justification: `inward` and `outward`" ] }, { "cell_type": "code", "execution_count": 24, "id": "d21c4ebd-f050-4c05-b1bd-00f7699ad3e4", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:12.015209Z", "iopub.status.busy": "2024-04-26T12:07:12.015209Z", "iopub.status.idle": "2024-04-26T12:07:12.030739Z", "shell.execute_reply": "2024-04-26T12:07:12.029733Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data3 = {\n", " 'x' : [1, 1, 2, 2, 1.5],\n", " 'y' : [1, 2, 1, 2, 1.5],\n", " 'text': ['bottom-left', 'top-left', 'bottom-right', 'top-right', 'center']\n", "}\n", "\n", "p3 = ggplot(data3, aes('x', 'y')) + \\\n", " geom_point(size=4) + \\\n", " ggsize(500, 300)\n", "\n", "p3 + geom_text(aes(label='text'), size=8)" ] }, { "cell_type": "code", "execution_count": 25, "id": "8e18bd00-f3fa-4519-91a4-201788f6d73a", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:12.030739Z", "iopub.status.busy": "2024-04-26T12:07:12.030739Z", "iopub.status.idle": "2024-04-26T12:07:12.046677Z", "shell.execute_reply": "2024-04-26T12:07:12.045868Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3 + geom_text(aes(label='text'), size=8, hjust='inward', vjust='inward')" ] }, { "cell_type": "code", "execution_count": 26, "id": "1dfe45ef-72a5-4eed-b2be-f80c5b79a1a3", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:12.046677Z", "iopub.status.busy": "2024-04-26T12:07:12.046677Z", "iopub.status.idle": "2024-04-26T12:07:12.063263Z", "shell.execute_reply": "2024-04-26T12:07:12.061808Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3 + geom_text(aes(label='text'), size=8, hjust='outward', vjust='outward')" ] }, { "cell_type": "markdown", "id": "f5b2cd25-82e9-4165-9a81-cb2792d723c4", "metadata": {}, "source": [ "## 5. GeoDataFrame in `geom_text()`/`geom_label()`\n", "\n", "GeoDataFrame is supported natively in the 'data' parameter for `geom_label()` and `geom_text()`." ] }, { "cell_type": "code", "execution_count": 27, "id": "7cdf5ac7-74e1-4cde-b430-09e0fbbaf3d7", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:12.067274Z", "iopub.status.busy": "2024-04-26T12:07:12.067274Z", "iopub.status.idle": "2024-04-26T12:07:13.059141Z", "shell.execute_reply": "2024-04-26T12:07:13.059141Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gcoder = geocode_states('us-48').inc_res()\n", "\n", "ggplot() + \\\n", " geom_polygon(aes(fill='state'), data=gcoder.get_boundaries(), \\\n", " show_legend=False, color='white', tooltips='none') + \\\n", " geom_label(aes(label='state'), data=gcoder.get_centroids(), size=6) + \\\n", " coord_map() + \\\n", " theme(axis='blank', panel_grid='blank') + \\\n", " scale_fill_brewer(name='state', palette='Dark2') + \\\n", " ggsize(900, 500)" ] }, { "cell_type": "markdown", "id": "8fb78579-4ea3-4eb1-9608-7cabaa74ed9f", "metadata": {}, "source": [ "## 6. Text on Livemap" ] }, { "cell_type": "code", "execution_count": 28, "id": "d476603e-7c4e-4b76-b334-8190aaafccbc", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:13.059141Z", "iopub.status.busy": "2024-04-26T12:07:13.059141Z", "iopub.status.idle": "2024-04-26T12:07:13.074868Z", "shell.execute_reply": "2024-04-26T12:07:13.074868Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities_data = {\n", " 'city': ['New York City', 'Prague'],\n", " 'lon': [-73.7997, 14.418540],\n", " 'lat': [40.6408, 50.073658],\n", "}\n", "\n", "ggplot(cities_data, aes(x='lon', y='lat')) + \\\n", " geom_livemap(projection='epsg4326', tiles=tilesets.NASA_CITYLIGHTS_2012) + \\\n", " geom_path(color='white') + \\\n", " geom_label(aes(label='city'), \\\n", " size=8, hjust=0.5, vjust=0.5, fill='black', color='white', \\\n", " angle=5, label_padding=0.6, label_r=0.5, label_size=1.5) + \\\n", " geom_text(x=-35, y=50, label='Average flight time: 8 hrs 43 mins\\n' +\n", " 'The shortest distance (air line): 4,082.79 mi', \\\n", " size=7, hjust=.5, lineheight=2, color='white')" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }