{ "cells": [ { "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": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "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": 4, "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()\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "val mpg_dat = mpg_df.toMap()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(mpg_dat) + geomPoint { x=\"displ\"; y=\"hwy\"; color=\"drv\" } \n", "p" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + ggtitle(\"The plot title using 'ggtitle()'\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + labs(title = \"The plot title using 'labs()'\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Add subtitle using ggtitle()\n", "\n", "p + ggtitle(\"The plot title\", subtitle = \"The plot subtitle\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Add subtitle using labs()\n", "\n", "p + labs(title = \"The plot title\", subtitle = \"The plot subtitle\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Add caption\n", "\n", "val p2 = p + labs(title = \"The plot title\", subtitle = \"The plot subtitle\", caption = \"The plot caption\")\n", "p2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Add color for titles\n", "\n", "// 'title' applies to plot's title, subtitle, caption\n", "p2 + theme(title = elementText(color=\"blue\"))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// 'plot_title' will also apply to the subtitle\n", "\n", "p2 + theme(plotTitle = elementText(color=\"blue\"), \n", " text = elementText(face=\"italic\"), \n", " title = elementText(face=\"italic\"),\n", " plotSubtitle = elementText(face=\"italic\"),\n", " plotCaption = elementText(face=\"italic\"),\n", " axisTitle = elementText(face=\"italic\"),\n", " legendTitle = elementText(face=\"italic\"))\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Set own colors\n", "\n", "p2 + theme(\n", " plotTitle = elementText(color=\"blue\"), \n", " plotSubtitle = elementText(color=\"red\"),\n", " plotCaption = elementText(color=\"dark_green\"))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Multiple lines - using `\\n`\n", "\n", "p + labs(\n", " title = \"The plot title:\\nFuel efficiency for most popular models of car\", \n", " subtitle = \"The plot subtitle:\\nPoints are colored by the type of drive train\",\n", " caption = \"The plot caption:\\nmpg dataset\"\n", " ) + theme(plotSubtitle = elementText(color=\"gray\"), plotCaption = elementText(color=\"light_gray\"))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Legend title\n", "\n", "val p1 = letsPlot(mpg_dat) + geomPoint(size=4) { x=\"displ\"; y=\"hwy\"; color=\"cty\"; shape=\"drv\" } \n", "p1 + labs(color=\"City mileage\", shape=\"Drive type\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Change legend position\n", "\n", "p1 + labs(color=\"City mileage\", shape=\"Drive type\") + theme().legendPositionBottom()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Use multiple lines in legend titles\n", "\n", "p1 + labs(color=\"City mileage\\n(mpg)\", \n", " shape=\"Drive type\\n(front/4/rear wheel)\") " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + theme().legendPositionBottom() +\n", " labs(color=\"City mileage\\n(mpg)\", shape=\"Drive type\\n(front/4/rear wheel)\") " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + theme().legendPositionBottom() +\n", " labs(color=\"City mileage\\n(mpg)\", shape=\"Drive type\\n(front/4/rear wheel)\") +\n", " scaleShape(guide=guideLegend(nrow=3))" ] } ], "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 }