{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Tooltip Customization" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "//%use krangl" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
4 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
Shape: 5 x 12. \n", "
" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var mpg_df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpg_df.head()\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "val mpg_dat = mpg_df.toMap()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(mpg_dat) {x=\"displ\"; y=\"cty\"} + \n", " scaleSize(range = 5 to 15, breaks = listOf(15, 40)) + ggsize(600, 350)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default tooltips.\n", "p + geomPoint(shape=21, color=\"white\") {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// No tooltips.\n", "p + geomPoint(shape=21, color=\"white\", tooltips=tooltipsNone) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Change format for the \"size\" aesthetic which is already shown in the tooltip by default.\n", "p + geomPoint(shape=21,\n", " color=\"white\",\n", " tooltips=layerTooltips().format(\"^size\", \"{.0f} mpg\")) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Show the vehicle \"class\" value in the tooltip (instead of the value of the \"size\" aesthetic).\n", "p + geomPoint(shape=21, \n", " color=\"white\", \n", " tooltips=layerTooltips().line(\"@class\")) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Configure a multiline tooltip.\n", "p + geomPoint(shape=21, \n", " color=\"white\",\n", " tooltips=layerTooltips()\n", " .format(\"cty\", \".0f\")\n", " .format(\"hwy\", \".0f\")\n", " .format(\"drv\", \"{}wd\")\n", " .line(\"@manufacturer @model\")\n", " .line(\"cty/hwy [mpg]|@cty/@hwy\")\n", " .line(\"@|@class\")\n", " .line(\"drive train|@drv\")\n", " .line(\"@|@year\")) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// List of variables to place in a multiline tooltip with the default formatting.\n", "p + geomPoint(shape=21, \n", " color=\"white\",\n", " tooltips=layerTooltips(\"manufacturer\", \"model\", \"class\", \"year\")\n", " ) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Define the format for the variable from the list and specify an additional line.\n", "p + geomPoint(shape=21, \n", " color=\"white\",\n", " tooltips=layerTooltips(\"manufacturer\", \"model\", \"class\", \"drv\")\n", " .format(\"drv\", \"{}wd\")\n", " .line(\"cty/hwy [mpg]|@cty/@hwy\")\n", " ) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Anchor the tooltip in the top-right corner of the plot.\n", "p + geomPoint(shape=21,\n", " color=\"white\",\n", " tooltips=layerTooltips()\n", " .anchor(\"top_right\")\n", " .minWidth(180)\n", " .format(\"cty\", \".0f\")\n", " .format(\"hwy\", \".0f\")\n", " .format(\"drv\", \"{}wd\")\n", " .line(\"@manufacturer @model\")\n", " .line(\"cty/hwy [mpg]|@cty/@hwy\")\n", " .line(\"@|@class\")\n", " .line(\"drive train|@drv\")\n", " .line(\"@|@year\")) {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \"Side\" tooltips." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p2 = letsPlot(mpg_dat) {x=\"class\"; y=\"hwy\"} + theme().legendPositionNone() + ggsize(600, 350)\n", "\n", "// Default tooltips\n", "p2 + geomBoxplot()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Configure text in outlier tootips using the 'format()' function.\n", "p2 + geomBoxplot(tooltips=layerTooltips()\n", " .format(\"^Y\", \"{.0f}\") // all Y-positionals (note: no 'labels')\n", " .format(\"^middle\", \".2f\") // different precision for 'middle' (note: default 'label')\n", " .format(\"^ymin\", \"min: {}\") // ymin/ymax aesthetics:\n", " .format(\"^ymax\", \"max: {}\")) // - add custom 'label'" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Replace \"side\" tooltips with anchored (top_center) \"general\" tooltip.\n", "// The 'line()' function assigns aesthetic or 'variable' to a general multiline tooltip.\n", "p2 + geomBoxplot(tooltips=layerTooltips()\n", " .anchor(\"top_center\")\n", " .format(\"^Y\", \".0f\")\n", " .format(\"^middle\", \".2f\")\n", " .line(\"min/max|^ymin/^ymax\")\n", " .line(\"lower/upper|^lower/^upper\")\n", " .line(\"@|^middle\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Showing constants in tooltip." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// By default tooltip never shows values defined via layer parameters (constants).\n", "// Still, these values can be added to a layer tooltip using the 'layer_tooltips()' function.\n", "val rand = java.util.Random()\n", "val n = 100\n", "val dat = mapOfsepal_length | sepal_width | petal_length | petal_width | species |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
Shape: 5 x 5. \n", "
" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val iris_df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/iris.csv\")\n", "iris_df.head()\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "val iris_dat = iris_df.toMap()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default density plot.\n", "letsPlot(iris_dat) + ggsize(650, 300) + \n", " geomArea(stat=Stat.density(), color=\"white\") {x=\"sepal_length\"; fill=\"species\"}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Change the tooltip content.\n", "letsPlot(iris_dat) + ggsize(650, 300) +\n", " geomArea(stat=Stat.density(),\n", " color=\"white\",\n", " tooltips=layerTooltips()\n", " .anchor(\"top_right\")\n", " .line(\"^fill\")\n", " .line(\"length|^x\")\n", " .line(\"density|^y\")) {x=\"sepal_length\"; fill=\"species\"}" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Use '..density..' variable in the tooltip\n", "letsPlot(iris_dat) + ggsize(650, 300) +\n", " geomArea(stat=Stat.density(), \n", " color=\"white\",\n", " tooltips=layerTooltips()\n", " .anchor(\"top_right\")\n", " .format(\"..density..\", \".4f\")\n", " .line(\"density|@..density..\")) {x=\"sepal_length\"; fill=\"species\"}" ] } ], "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.7.20-dev-1299" } }, "nbformat": 4, "nbformat_minor": 4 }