{ "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" }, { "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, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.0.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", " \n", " \n", " " ] }, "execution_count": 3, "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": 4, "metadata": {}, "outputs": [], "source": [ "val mpg_dat = mpg_df.toMap()" ] }, { "cell_type": "code", "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default tooltips.\n", "p + geomPoint(shape=21, color=\"white\") {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "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": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "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": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "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": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "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": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "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": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "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": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "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": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "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 = mapOf(\n", " \"x\" to List(n) { rand.nextGaussian() },\n", " \"y\" to List(n) { rand.nextGaussian() }\n", ")\n", "\n", "letsPlot(dat) {x=\"x\"; y=\"y\"} + \n", " geomPoint() +\n", " geomVLine(xintercept=(dat[\"x\"] as List).average(), color=\"red\", linetype=\"dashed\", size=1,\n", " tooltips=layerTooltips().format(\"^xintercept\", \".4f\").line(\"mean = ^xintercept\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Some other examples." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":5,\"columns\":[\"sepal_length\",\"sepal_width\",\"petal_length\",\"petal_width\",\"species\"],\"kotlin_dataframe\":[{\"sepal_length\":5.1,\"sepal_width\":3.5,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.9,\"sepal_width\":3.0,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.7,\"sepal_width\":3.2,\"petal_length\":1.3,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":4.6,\"sepal_width\":3.1,\"petal_length\":1.5,\"petal_width\":0.2,\"species\":\"setosa\"},{\"sepal_length\":5.0,\"sepal_width\":3.6,\"petal_length\":1.4,\"petal_width\":0.2,\"species\":\"setosa\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 5

\n", " \n", " \n", " " ] }, "execution_count": 18, "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": 19, "metadata": {}, "outputs": [], "source": [ "val iris_dat = iris_df.toMap()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "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": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 21, "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": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 22, "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.8.20" } }, "nbformat": 4, "nbformat_minor": 4 }