{ "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/html": [ "
manufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.819994auto(l5)f1829pcompact
2audia41.819994manual(m5)f2129pcompact
3audia42.020084manual(m6)f2031pcompact
4audia42.020084auto(av)f2130pcompact
5audia42.819996auto(l5)f1626pcompact

Shape: 5 x 12. \n", "

" ] }, "execution_count": 2, "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()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "val mpg_dat = mpg_df.toMap()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(mpg_dat) {x=\"displ\"; y=\"cty\"} + \n", " scale_size(range = 5 to 15, breaks = listOf(15, 40)) + ggsize(600, 350)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default tooltips.\n", "p + geomPoint(shape=21, color=\"white\") {fill=\"drv\"; size=\"hwy\"}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "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": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "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": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "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": [ "### \"Outlier\" tooltips." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "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": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "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": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Replace \"outlier\" 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", " .color(\"cyan\")\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": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "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": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa

Shape: 5 x 5. \n", "

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