{ "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": "markdown", "metadata": {}, "source": [ "### The US Unemployment Rates 2000-2016" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "var economics = DataFrame.readCSV(\"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "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": 3, "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": 4, "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": "markdown", "metadata": {}, "source": [ "#### Default plot (no formatting)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine() + \n", " scale_x_datetime() + \n", " ggsize(900, 400)" ] }, { "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": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine() + \n", " scaleXDateTime(format=\"%b %Y\") + \n", " scaleYContinuous(format=\"{} %\") + \n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure tooltip's text and location." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(economics.toMap()){x=\"date\"; y=\"uempmed\"} + \n", " geomLine(tooltips=layerTooltips()\n", " .line(\"Unemployment rate:|^y\")\n", " .color(\"black\")\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": [ "#### Add the unemployment rate mean.\n", "\n", "The `geomText` label is formatted using the `labelFormat` parameter." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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", " .color(\"black\")\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": "kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.6.0-dev-1733" } }, "nbformat": 4, "nbformat_minor": 4 }