{ "cells": [ { "cell_type": "markdown", "id": "61b72bb9", "metadata": {}, "source": [ "# Using \"Exponent Format\"\n", "\n", "The `exponenFormat` parameter in the `theme(...)` function can be used to configure the way \"exponent notation\" looks like on plot. \n", "Available values:\n", "- `'e'` for E notation, for example, 1.23e+3, which is the default format.\n", "- `'pow'` for superscript power notation.\n", "\n", "The \"exponent format\" is automatically applied to each value formatted in scientific notation, regardless whether the format is user-defined or chosen automatically based on the data. This format affects every part of a plot, including geoms, scales, labels, and tooltips." ] }, { "cell_type": "code", "execution_count": 1, "id": "976dab96", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "077e680b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.6.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.2.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d705bf00", "metadata": {}, "outputs": [], "source": [ "val n = 10\n", "val data = mapOf(\n", " \"x\" to (0 until n).toList(),\n", " \"y\" to (0 until n).map { (it + 1 + 0.025 * it) * 10.0.pow(-5) },\n", " \"c\" to (0 until n).map { it * 10.0.pow(10) }\n", ")\n", "\n", "val p = letsPlot(data) { x = \"x\"; y = \"y\"; fill = \"c\" } + geomBar(stat = Stat.identity)" ] }, { "cell_type": "markdown", "id": "74837cbc", "metadata": {}, "source": [ "#### 1. E-notation vs. Superscript Power Notation\n", "\n", "In this example \"scientific notation\" formatting for the guides is chosen automatically, basing on the data." ] }, { "cell_type": "code", "execution_count": 4, "id": "087acd3d", "metadata": {}, "outputs": [], "source": [ "val powTheme = theme(exponentFormat = \"pow\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "a698843f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " p + ggtitle(\"E-notation (default)\"),\n", " p + powTheme + ggtitle(\"Superscript Power\")\n", "))" ] }, { "cell_type": "markdown", "id": "7a5b34d7", "metadata": {}, "source": [ "#### 2. Formatting in `geomText()` and `geomLabel()`\n", "\n", "By default, values in `geomText()` and `geomLabel()` are always shown in standard notation. \n", "\n", "Thus by default, the `'pow'` in `theme()` doesn't affect numbers in `geomText()` and `geomLabel()` (see the chart on the left) unless
\n", "you specify a scientific notation formatting explicitly via the `labelFormat` parameter (see the chart on the right).\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "495ddec1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val left = p + geomLabel(alpha = 0.8, fill = \"white\") { label = \"y\" }\n", "val right = p + geomLabel(alpha = 0.8, fill = \"white\", labelFormat=\".2~e\") { label = \"y\" }\n", "\n", "gggrid(listOf(left, right)) + powTheme" ] } ], "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": 5 }