{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting labels on plots.\n", "\n", "In Lets-Plot you can apply a formatting to:\n", "\n", "- axis break values.\n", "- labels displayed by `geomText()`.\n", "- tooltip text.\n", "- facet labels.\n", "\n", "Using format string you can format values of numeric and date-time types.\n", "\n", "In addition, you can use a *string template*.\n", "\n", "In *string template* the value's format string is surrounded by curly braces: `\"... {.2f} ...\"`.\n", "\n", "An empty placeholder `{}` is also allowed. In this case a default string representation will be shown. This is also applicable to categorical values.\n", "\n", "To learn more about formatting templates see: [Formatting](https://github.com/JetBrains/lets-plot-kotlin/blob/master/docs/formats.md)." ] }, { "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": "markdown", "metadata": {}, "source": [ "### The US Unemployment Rates 2000-2016" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "var economics = DataFrame.readCSV(\"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":7,\"columns\":[\"untitled\",\"date\",\"pce\",\"pop\",\"psavert\",\"uempmed\",\"unemploy\"],\"kotlin_dataframe\":[{\"untitled\":1,\"date\":\"1967-07-01\",\"pce\":506.7,\"pop\":198712.0,\"psavert\":12.6,\"uempmed\":4.5,\"unemploy\":2944},{\"untitled\":2,\"date\":\"1967-08-01\",\"pce\":509.8,\"pop\":198911.0,\"psavert\":12.6,\"uempmed\":4.7,\"unemploy\":2945},{\"untitled\":3,\"date\":\"1967-09-01\",\"pce\":515.6,\"pop\":199113.0,\"psavert\":11.9,\"uempmed\":4.6,\"unemploy\":2958},{\"untitled\":4,\"date\":\"1967-10-01\",\"pce\":512.2,\"pop\":199311.0,\"psavert\":12.9,\"uempmed\":4.9,\"unemploy\":3143},{\"untitled\":5,\"date\":\"1967-11-01\",\"pce\":517.4,\"pop\":199498.0,\"psavert\":12.8,\"uempmed\":4.7,\"unemploy\":3066}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 7

\n", " \n", " \n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "economics.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import kotlinx.datetime.*\n", "\n", "fun LocalDate.toEpochMilliseconds(): Long {\n", " return atStartOfDayIn((TimeZone.currentSystemDefault())).toEpochMilliseconds()\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "val startDate = LocalDate.parse(\"2000-01-01\")\n", "economics = economics.filter { date.compareTo(startDate) >= 0 }.convert {date}.with {it.toEpochMilliseconds()}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine() +\n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default plot (no formatting)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Apply formatting to X and Y axis labels.\n", "\n", "Use the `format` parameter in `scaleXxx()`.\n", "\n", "Note that the text in tooltips is now also formatted." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scaleXDateTime(format=\"%b %Y\") + scaleYContinuous(format=\"{} %\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Format axis labels for breaks specified manually\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val breaks = listOf(\n", " LocalDate.parse(\"2001-01-01\"), \n", " LocalDate.parse(\"2016-01-01\")\n", ").map {it.toEpochMilliseconds()}\n", "\n", "p + scaleXDateTime(format=\"%b %Y\", breaks=breaks) + scaleYContinuous(format=\"{} %\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure tooltip's text and location." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine(tooltips=layerTooltips()\n", " .line(\"Unemployment rate:|^y\")\n", " .anchor(\"top_center\")\n", " .minWidth(170)) + \n", " scaleXDateTime(format=\"%b %Y\") +\n", " scaleYContinuous(format=\"{} %\") +\n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Format value shown in tooltip." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} +\n", " geomLine(tooltips=layerTooltips()\n", " .line(\"@uempmed % in @date\")\n", " .format(\"date\", \"%B %Y\")\n", " .anchor(\"top_left\")\n", " .minWidth(170)) +\n", " scaleXDateTime() +\n", " scaleYContinuous() +\n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add the unemployment rate mean.\n", "\n", "The `geomText` label is formatted using the `labelFormat` parameter." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val unemploymentMean = economics.uempmed.mean()\n", "\n", "\n", "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine(tooltips=layerTooltips()\n", " .line(\"Unemployment rate:|^y %\")\n", " .anchor(\"top_center\")\n", " .minWidth(170)) + \n", " geomHLine(yintercept=unemploymentMean, color=\"red\", linetype=\"dashed\", tooltips=tooltipsNone) +\n", " geomText(label=unemploymentMean.toString(), \n", " labelFormat=\"{.2f} %\",\n", " x=startDate.toEpochMilliseconds(), y=unemploymentMean!! + 0.5, \n", " color=\"red\") +\n", " scaleXDateTime(format=\"%b %Y\") + \n", " scaleYContinuous(format=\"{} %\") + \n", " ylab(\"unemployment rate\") +\n", " ggtitle(\"The US Unemployment Rates 2000-2016.\") +\n", " ggsize(900, 400)" ] } ], "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 }