{ "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" } ], "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": "markdown", "metadata": {}, "source": [ "### The US Unemployment Rates 2000-2016" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "var economics = DataFrame.readCSV(\"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
datepcepoppsavertuempmedunemploy
1Sat Jul 01 00:00:00 EDT 1967506.719871212.64.52944
2Tue Aug 01 00:00:00 EDT 1967509.819891112.64.72945
3Fri Sep 01 00:00:00 EDT 1967515.619911311.94.62958

Shape: 3 x 7. \n", "

" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.text.SimpleDateFormat\n", "val yyyyMmDd = SimpleDateFormat(\"yyyy-MM-dd\")\n", "economics = economics.addColumn(\"date\") { it[\"date\"].map {yyyyMmDd.parse(it)}}\n", "economics.head(3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import java.util.Date\n", "val startDate = yyyyMmDd.parse(\"2000-01-01\")\n", "economics = economics.filterByRow { (it[\"date\"] as Date).compareTo(startDate) >= 0 }" ] }, { "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": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scaleXDateTime()" ] }, { "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val breaks = listOf(\n", " yyyyMmDd.parse(\"2001-01-01\"), \n", " yyyyMmDd.parse(\"2016-01-01\")\n", ")\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": 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(\"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": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "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": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "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.toInstant().toEpochMilli(), 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.7.20-dev-1299" } }, "nbformat": 4, "nbformat_minor": 4 }