{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f06e1ccf-cf80-4b5f-8ffb-9b8ec97c2b2a",
   "metadata": {},
   "source": [
    "# Configuring Nudge Units in Position Adjustments\n",
    "\n",
    "The position adjustment `position_nudge()` now supports the `unit` parameter that specifies the measurement system for nudge offsets.  <br/>\n",
    "Equivalent parameter is also available as `nudge_unit` in `geom_text()` and `geom_label()` directly. \n",
    "\n",
    "Available Units:\n",
    "\n",
    "- `'identity'` (default): nudge in data coordinates - a value of 1 corresponds to the distance from 0 to 1 on the axis\n",
    "- `'size'`: nudge relative to point size - a value of 1 corresponds to the diameter of a point with size 1\n",
    "- `'px'`: nudge in fixed pixels - a value of 1 corresponds to 1 pixel\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ef6598d-3dcf-4297-8010-75f90c7838c6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.352513Z",
     "iopub.status.busy": "2025-03-07T18:26:10.352443Z",
     "iopub.status.idle": "2025-03-07T18:26:10.710632Z",
     "shell.execute_reply": "2025-03-07T18:26:10.710263Z"
    }
   },
   "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 lets_plot import *\n",
    "from lets_plot.geo_data import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2c2b568d-2da5-491d-8bad-7792afd7b68f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.711573Z",
     "iopub.status.busy": "2025-03-07T18:26:10.711454Z",
     "iopub.status.idle": "2025-03-07T18:26:10.713738Z",
     "shell.execute_reply": "2025-03-07T18:26:10.713567Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "            <div id=\"2UzuUU\"></div>\n",
       "            <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "                if(!window.letsPlotCallQueue) {\n",
       "                    window.letsPlotCallQueue = [];\n",
       "                }; \n",
       "                window.letsPlotCall = function(f) {\n",
       "                    window.letsPlotCallQueue.push(f);\n",
       "                };\n",
       "                (function() {\n",
       "                    var script = document.createElement(\"script\");\n",
       "                    script.type = \"text/javascript\";\n",
       "                    script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.6.1/js-package/distr/lets-plot.min.js\";\n",
       "                    script.onload = function() {\n",
       "                        window.letsPlotCall = function(f) {f();};\n",
       "                        window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "                        window.letsPlotCallQueue = [];\n",
       "                        \n",
       "                    };\n",
       "                    script.onerror = function(event) {\n",
       "                        window.letsPlotCall = function(f) {};    // noop\n",
       "                        window.letsPlotCallQueue = [];\n",
       "                        var div = document.createElement(\"div\");\n",
       "                        div.style.color = 'darkred';\n",
       "                        div.textContent = 'Error loading Lets-Plot JS';\n",
       "                        document.getElementById(\"2UzuUU\").appendChild(div);\n",
       "                    };\n",
       "                    var e = document.getElementById(\"2UzuUU\");\n",
       "                    e.appendChild(script);\n",
       "                })()\n",
       "            </script>\n",
       "            "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "LetsPlot.setup_html()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78a745d4-30e6-4c63-947b-99543875d337",
   "metadata": {},
   "source": [
    "#### 1. Without nudge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "44c46535-64ac-49d2-9a43-76b44d2aa8f1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.714679Z",
     "iopub.status.busy": "2025-03-07T18:26:10.714607Z",
     "iopub.status.idle": "2025-03-07T18:26:10.721015Z",
     "shell.execute_reply": "2025-03-07T18:26:10.720834Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"QC5TG3\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   const forceImmediateRender = false;\n",
       "   const responsive = false;\n",
       "   \n",
       "   let sizing = {\n",
       "       width_mode: \"MIN\",\n",
       "       height_mode: \"SCALED\",\n",
       "       width: null, \n",
       "       height: null \n",
       "   };\n",
       "   \n",
       "   const preferredWidth = document.body.dataset.letsPlotPreferredWidth;\n",
       "   if (preferredWidth !== undefined) {\n",
       "       sizing = {\n",
       "           width_mode: 'FIXED',\n",
       "           height_mode: 'SCALED',\n",
       "           width: parseFloat(preferredWidth)\n",
       "       };\n",
       "   }\n",
       "   \n",
       "   const containerDiv = document.getElementById(\"QC5TG3\");\n",
       "   let fig = null;\n",
       "   \n",
       "   function renderPlot() {\n",
       "       if (fig === null) {\n",
       "           const plotSpec = {\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":300.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":0.0,\n",
       "\"size\":10.0,\n",
       "\"color\":\"#DA8459\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":0.0,\n",
       "\"label\":\"text without nudge\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"metainfo_list\":[],\n",
       "\"spec_id\":\"1\"\n",
       "};\n",
       "           window.letsPlotCall(function() { fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, containerDiv, sizing); });\n",
       "       } else {\n",
       "           fig.updateView({});\n",
       "       }\n",
       "   }\n",
       "   \n",
       "   const renderImmediately = \n",
       "       forceImmediateRender || (\n",
       "           sizing.width_mode === 'FIXED' && \n",
       "           (sizing.height_mode === 'FIXED' || sizing.height_mode === 'SCALED')\n",
       "       );\n",
       "   \n",
       "   if (renderImmediately) {\n",
       "       renderPlot();\n",
       "   }\n",
       "   \n",
       "   if (!renderImmediately || responsive) {\n",
       "       // Set up observer for initial sizing or continuous monitoring\n",
       "       var observer = new ResizeObserver(function(entries) {\n",
       "           for (let entry of entries) {\n",
       "               if (entry.contentBoxSize && \n",
       "                   entry.contentBoxSize[0].inlineSize > 0) {\n",
       "                   if (!responsive && observer) {\n",
       "                       observer.disconnect();\n",
       "                       observer = null;\n",
       "                   }\n",
       "                   renderPlot();\n",
       "                   if (!responsive) {\n",
       "                       break;\n",
       "                   }\n",
       "               }\n",
       "           }\n",
       "       });\n",
       "       \n",
       "       observer.observe(containerDiv);\n",
       "   }\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ],
      "text/plain": [
       "<lets_plot.plot.core.PlotSpec at 0x7a7efc2f5e40>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot() + \\\n",
    "    geom_point(x=0, y=0, size=10, color='#DA8459') + \\\n",
    "    geom_text(x=0, y=0, label='text without nudge') + \\\n",
    "    ggsize(400, 300)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75564153-0761-456f-ad25-21a9f965ecce",
   "metadata": {},
   "source": [
    "#### 2. Unit Comparison: Pixel, Size, and Identity\n",
    "The `'size'` unit enables precise positioning relative to point dimensions.\n",
    "\n",
    "In this example, the point has `size=30` and text is positioned at half that distance (`nudge_y=15`). When combined with `vjust/hjust`, this allows for placing text at specific locations relative to the point boundary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7ee95dd2-7096-4dc0-86d5-c88f75f072f8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.721885Z",
     "iopub.status.busy": "2025-03-07T18:26:10.721803Z",
     "iopub.status.idle": "2025-03-07T18:26:10.726057Z",
     "shell.execute_reply": "2025-03-07T18:26:10.725888Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"XhAqJy\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   const forceImmediateRender = false;\n",
       "   const responsive = false;\n",
       "   \n",
       "   let sizing = {\n",
       "       width_mode: \"MIN\",\n",
       "       height_mode: \"SCALED\",\n",
       "       width: null, \n",
       "       height: null \n",
       "   };\n",
       "   \n",
       "   const preferredWidth = document.body.dataset.letsPlotPreferredWidth;\n",
       "   if (preferredWidth !== undefined) {\n",
       "       sizing = {\n",
       "           width_mode: 'FIXED',\n",
       "           height_mode: 'SCALED',\n",
       "           width: parseFloat(preferredWidth)\n",
       "       };\n",
       "   }\n",
       "   \n",
       "   const containerDiv = document.getElementById(\"XhAqJy\");\n",
       "   let fig = null;\n",
       "   \n",
       "   function renderPlot() {\n",
       "       if (fig === null) {\n",
       "           const plotSpec = {\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"limits\":[-1.0,4.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":1.0,\n",
       "\"size\":5.0,\n",
       "\"color\":\"#B9534C\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":0.2,\n",
       "\"x\":0.0,\n",
       "\"y\":1.0,\n",
       "\"label\":\"identity 0.2\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":1.0,\n",
       "\"y\":1.0,\n",
       "\"size\":5.0,\n",
       "\"color\":\"#DA8459\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":40.0,\n",
       "\"nudge_unit\":\"px\",\n",
       "\"x\":1.0,\n",
       "\"y\":1.0,\n",
       "\"label\":\"px 40\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":2.0,\n",
       "\"y\":1.0,\n",
       "\"size\":30.0,\n",
       "\"color\":\"#EEAB65\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":15.0,\n",
       "\"nudge_unit\":\"size\",\n",
       "\"x\":2.0,\n",
       "\"y\":1.0,\n",
       "\"label\":\"size 15\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":3.0,\n",
       "\"y\":1.0,\n",
       "\"size\":30.0,\n",
       "\"color\":\"#F6C971\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":15.0,\n",
       "\"nudge_unit\":\"size\",\n",
       "\"x\":3.0,\n",
       "\"y\":1.0,\n",
       "\"label\":\"size 15 vjust 0\",\n",
       "\"vjust\":0.0,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":-15.0,\n",
       "\"nudge_unit\":\"size\",\n",
       "\"x\":3.0,\n",
       "\"y\":1.0,\n",
       "\"label\":\"size -15 vjust 1\",\n",
       "\"vjust\":1.0,\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"metainfo_list\":[],\n",
       "\"spec_id\":\"2\"\n",
       "};\n",
       "           window.letsPlotCall(function() { fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, containerDiv, sizing); });\n",
       "       } else {\n",
       "           fig.updateView({});\n",
       "       }\n",
       "   }\n",
       "   \n",
       "   const renderImmediately = \n",
       "       forceImmediateRender || (\n",
       "           sizing.width_mode === 'FIXED' && \n",
       "           (sizing.height_mode === 'FIXED' || sizing.height_mode === 'SCALED')\n",
       "       );\n",
       "   \n",
       "   if (renderImmediately) {\n",
       "       renderPlot();\n",
       "   }\n",
       "   \n",
       "   if (!renderImmediately || responsive) {\n",
       "       // Set up observer for initial sizing or continuous monitoring\n",
       "       var observer = new ResizeObserver(function(entries) {\n",
       "           for (let entry of entries) {\n",
       "               if (entry.contentBoxSize && \n",
       "                   entry.contentBoxSize[0].inlineSize > 0) {\n",
       "                   if (!responsive && observer) {\n",
       "                       observer.disconnect();\n",
       "                       observer = null;\n",
       "                   }\n",
       "                   renderPlot();\n",
       "                   if (!responsive) {\n",
       "                       break;\n",
       "                   }\n",
       "               }\n",
       "           }\n",
       "       });\n",
       "       \n",
       "       observer.observe(containerDiv);\n",
       "   }\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ],
      "text/plain": [
       "<lets_plot.plot.core.PlotSpec at 0x7a7efc129c60>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot() + \\\n",
    "    xlim(-1, 4) + \\\n",
    "    geom_point(x=0, y=1, size=5, color='#B9534C') + \\\n",
    "    geom_text(x=0, y=1, label='identity 0.2', nudge_y=0.2) + \\\n",
    "    geom_point(x=1, y=1, size=5, color='#DA8459') + \\\n",
    "    geom_text(x=1, y=1, label='px 40', nudge_y=40, nudge_unit='px') + \\\n",
    "    geom_point(x=2, y=1, size=30, color='#EEAB65') + \\\n",
    "    geom_text(x=2, y=1, label='size 15', nudge_y=15, nudge_unit='size') + \\\n",
    "    geom_point(x=3, y=1, size=30, color='#F6C971') + \\\n",
    "    geom_text(x=3, y=1, label='size 15 vjust 0', nudge_y=15, vjust=0, nudge_unit='size') + \\\n",
    "    geom_text(x=3, y=1, label='size -15 vjust 1', nudge_y=-15, vjust=1, nudge_unit='size')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "343d2dda-6faf-4dcd-95ef-cc95e12b915e",
   "metadata": {},
   "source": [
    "#### 3. Parameter `unit` in `position_nudge()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0c7a82f-7e27-4427-9b88-3282349e2944",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.726898Z",
     "iopub.status.busy": "2025-03-07T18:26:10.726827Z",
     "iopub.status.idle": "2025-03-07T18:26:10.729749Z",
     "shell.execute_reply": "2025-03-07T18:26:10.729578Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"2zQLQI\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   const forceImmediateRender = false;\n",
       "   const responsive = false;\n",
       "   \n",
       "   let sizing = {\n",
       "       width_mode: \"MIN\",\n",
       "       height_mode: \"SCALED\",\n",
       "       width: null, \n",
       "       height: null \n",
       "   };\n",
       "   \n",
       "   const preferredWidth = document.body.dataset.letsPlotPreferredWidth;\n",
       "   if (preferredWidth !== undefined) {\n",
       "       sizing = {\n",
       "           width_mode: 'FIXED',\n",
       "           height_mode: 'SCALED',\n",
       "           width: parseFloat(preferredWidth)\n",
       "       };\n",
       "   }\n",
       "   \n",
       "   const containerDiv = document.getElementById(\"2zQLQI\");\n",
       "   let fig = null;\n",
       "   \n",
       "   function renderPlot() {\n",
       "       if (fig === null) {\n",
       "           const plotSpec = {\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":0.0,\n",
       "\"size\":50.0,\n",
       "\"color\":\"#DA8459\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.0,\n",
       "\"y\":25.0,\n",
       "\"unit\":\"size\"\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":0.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"blue\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":50.0,\n",
       "\"y\":25.0,\n",
       "\"unit\":\"size\"\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":0.0,\n",
       "\"y\":0.0,\n",
       "\"label\":\"position_nudge size 25\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"metainfo_list\":[],\n",
       "\"spec_id\":\"3\"\n",
       "};\n",
       "           window.letsPlotCall(function() { fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, containerDiv, sizing); });\n",
       "       } else {\n",
       "           fig.updateView({});\n",
       "       }\n",
       "   }\n",
       "   \n",
       "   const renderImmediately = \n",
       "       forceImmediateRender || (\n",
       "           sizing.width_mode === 'FIXED' && \n",
       "           (sizing.height_mode === 'FIXED' || sizing.height_mode === 'SCALED')\n",
       "       );\n",
       "   \n",
       "   if (renderImmediately) {\n",
       "       renderPlot();\n",
       "   }\n",
       "   \n",
       "   if (!renderImmediately || responsive) {\n",
       "       // Set up observer for initial sizing or continuous monitoring\n",
       "       var observer = new ResizeObserver(function(entries) {\n",
       "           for (let entry of entries) {\n",
       "               if (entry.contentBoxSize && \n",
       "                   entry.contentBoxSize[0].inlineSize > 0) {\n",
       "                   if (!responsive && observer) {\n",
       "                       observer.disconnect();\n",
       "                       observer = null;\n",
       "                   }\n",
       "                   renderPlot();\n",
       "                   if (!responsive) {\n",
       "                       break;\n",
       "                   }\n",
       "               }\n",
       "           }\n",
       "       });\n",
       "       \n",
       "       observer.observe(containerDiv);\n",
       "   }\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ],
      "text/plain": [
       "<lets_plot.plot.core.PlotSpec at 0x7a7efc2f7820>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot() + \\\n",
    "    geom_point(x=0, y=0, size=50, color='#DA8459') + \\\n",
    "    geom_point(x=0, y=0, size=20, color='blue', position=position_nudge(0, 25, 'size')) + \\\n",
    "    geom_text(x=0, y=0, label='position_nudge size 25', position=position_nudge(50, 25, 'size'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75b464e4-129e-4f3a-869c-7ebe0f7f8a0e",
   "metadata": {},
   "source": [
    "#### 4. LiveMap\n",
    "There are specific behaviors of nudge on LiveMap:\n",
    "\n",
    "- `'identity'`: A value of 1 corresponds to 1 degree of latitude or longitude. The offset visually changes when zooming in.\n",
    "- `'size'`: Like point size, it follows the `const_size_zoomin` parameter when zooming in.\n",
    "- `'px'`: Maintains the offset in pixels regardless of zoom level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0a99e214-03f0-4425-9d3e-15f1f5b4a5fc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.730530Z",
     "iopub.status.busy": "2025-03-07T18:26:10.730454Z",
     "iopub.status.idle": "2025-03-07T18:26:10.732483Z",
     "shell.execute_reply": "2025-03-07T18:26:10.732306Z"
    }
   },
   "outputs": [],
   "source": [
    "layers = geom_point(x=-60, y=30, size=20, color='#B9534C') + \\\n",
    "    geom_text(x=-60, y=30, label='identity 5', nudge_y=5) + \\\n",
    "    geom_point(x=-40, y=30, size=20, color='#DA8459') + \\\n",
    "    geom_text(x=-40, y=30, label='px 40', nudge_y=40, nudge_unit='px') + \\\n",
    "    geom_point(x=-20, y=30, size=20, color='#EEAB65') + \\\n",
    "    geom_text(x=-20, y=30, label='size 15', nudge_y=15, nudge_unit='size')\n",
    "\n",
    "scale_true = ggplot() + \\\n",
    "    geom_livemap(const_size_zoomin=-1)\n",
    "    \n",
    "scale_false = ggplot() + \\\n",
    "    geom_livemap(const_size_zoomin=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d1e2bf35-d34b-4371-a400-b20eee45b810",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-07T18:26:10.733209Z",
     "iopub.status.busy": "2025-03-07T18:26:10.733138Z",
     "iopub.status.idle": "2025-03-07T18:26:10.737535Z",
     "shell.execute_reply": "2025-03-07T18:26:10.737364Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"pKoIup\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   const forceImmediateRender = false;\n",
       "   const responsive = false;\n",
       "   \n",
       "   let sizing = {\n",
       "       width_mode: \"MIN\",\n",
       "       height_mode: \"SCALED\",\n",
       "       width: null, \n",
       "       height: null \n",
       "   };\n",
       "   \n",
       "   const preferredWidth = document.body.dataset.letsPlotPreferredWidth;\n",
       "   if (preferredWidth !== undefined) {\n",
       "       sizing = {\n",
       "           width_mode: 'FIXED',\n",
       "           height_mode: 'SCALED',\n",
       "           width: parseFloat(preferredWidth)\n",
       "       };\n",
       "   }\n",
       "   \n",
       "   const containerDiv = document.getElementById(\"pKoIup\");\n",
       "   let fig = null;\n",
       "   \n",
       "   function renderPlot() {\n",
       "       if (fig === null) {\n",
       "           const plotSpec = {\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Try Zooming the Map\"\n",
       "},\n",
       "\"theme\":{\n",
       "\"plot_title\":{\n",
       "\"size\":30.0,\n",
       "\"hjust\":0.5,\n",
       "\"blank\":false\n",
       "}\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":900.0,\n",
       "\"height\":500.0\n",
       "},\n",
       "\"kind\":\"subplots\",\n",
       "\"layout\":{\n",
       "\"ncol\":2.0,\n",
       "\"nrow\":1.0,\n",
       "\"name\":\"grid\"\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Point size: scaled\"\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"geom\":\"livemap\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"tiles\":{\n",
       "\"kind\":\"vector_lets_plot\",\n",
       "\"url\":\"wss://tiles.datalore.jetbrains.com\",\n",
       "\"theme\":\"color\",\n",
       "\"attribution\":\"<a href=\\\"https://lets-plot.org\\\">© Lets-Plot</a>, map data: <a href=\\\"https://www.openstreetmap.org/copyright\\\">© OpenStreetMap contributors</a>.\"\n",
       "},\n",
       "\"geocoding\":{\n",
       "\"url\":\"https://geo2.datalore.jetbrains.com/map_data/geocoding\"\n",
       "},\n",
       "\"const_size_zoomin\":-1.0,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-60.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#B9534C\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":5.0,\n",
       "\"x\":-60.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"identity 5\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-40.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#DA8459\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":40.0,\n",
       "\"nudge_unit\":\"px\",\n",
       "\"x\":-40.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"px 40\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-20.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#EEAB65\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":15.0,\n",
       "\"nudge_unit\":\"size\",\n",
       "\"x\":-20.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"size 15\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"metainfo_list\":[],\n",
       "\"spec_id\":\"4\"\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Point size: fixed\"\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"geom\":\"livemap\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"tiles\":{\n",
       "\"kind\":\"vector_lets_plot\",\n",
       "\"url\":\"wss://tiles.datalore.jetbrains.com\",\n",
       "\"theme\":\"color\",\n",
       "\"attribution\":\"<a href=\\\"https://lets-plot.org\\\">© Lets-Plot</a>, map data: <a href=\\\"https://www.openstreetmap.org/copyright\\\">© OpenStreetMap contributors</a>.\"\n",
       "},\n",
       "\"geocoding\":{\n",
       "\"url\":\"https://geo2.datalore.jetbrains.com/map_data/geocoding\"\n",
       "},\n",
       "\"const_size_zoomin\":0.0,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-60.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#B9534C\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":5.0,\n",
       "\"x\":-60.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"identity 5\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-40.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#DA8459\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":40.0,\n",
       "\"nudge_unit\":\"px\",\n",
       "\"x\":-40.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"px 40\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"point\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"x\":-20.0,\n",
       "\"y\":30.0,\n",
       "\"size\":20.0,\n",
       "\"color\":\"#EEAB65\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data_meta\":{\n",
       "},\n",
       "\"nudge_y\":15.0,\n",
       "\"nudge_unit\":\"size\",\n",
       "\"x\":-20.0,\n",
       "\"y\":30.0,\n",
       "\"label\":\"size 15\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"metainfo_list\":[],\n",
       "\"spec_id\":\"5\"\n",
       "}]\n",
       "};\n",
       "           window.letsPlotCall(function() { fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, containerDiv, sizing); });\n",
       "       } else {\n",
       "           fig.updateView({});\n",
       "       }\n",
       "   }\n",
       "   \n",
       "   const renderImmediately = \n",
       "       forceImmediateRender || (\n",
       "           sizing.width_mode === 'FIXED' && \n",
       "           (sizing.height_mode === 'FIXED' || sizing.height_mode === 'SCALED')\n",
       "       );\n",
       "   \n",
       "   if (renderImmediately) {\n",
       "       renderPlot();\n",
       "   }\n",
       "   \n",
       "   if (!renderImmediately || responsive) {\n",
       "       // Set up observer for initial sizing or continuous monitoring\n",
       "       var observer = new ResizeObserver(function(entries) {\n",
       "           for (let entry of entries) {\n",
       "               if (entry.contentBoxSize && \n",
       "                   entry.contentBoxSize[0].inlineSize > 0) {\n",
       "                   if (!responsive && observer) {\n",
       "                       observer.disconnect();\n",
       "                       observer = null;\n",
       "                   }\n",
       "                   renderPlot();\n",
       "                   if (!responsive) {\n",
       "                       break;\n",
       "                   }\n",
       "               }\n",
       "           }\n",
       "       });\n",
       "       \n",
       "       observer.observe(containerDiv);\n",
       "   }\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ],
      "text/plain": [
       "<lets_plot.plot.subplots.SupPlotsSpec at 0x7a7efc2f7610>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid([\n",
    "    scale_true + layers + ggtitle('Point size: scaled'), \n",
    "    scale_false + layers + ggtitle('Point size: fixed')    \n",
    "]) + ggtitle('Try Zooming the Map') + theme(plot_title=element_text(hjust=0.5, size=30)) + ggsize(900, 500)"
   ]
  }
 ],
 "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
}