{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "impressive-meeting", "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, "id": "existing-affiliate", "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": "code", "execution_count": 3, "id": "individual-cleaning", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 12

\n", " \n", " \n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "df.head(3)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "reasonable-baltimore", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(df.toMap()) { x = \"cty\"; y = \"hwy\"; color = \"drv\" } + geomPoint() + labs(color=\"Drive type\") \n", "p" ] }, { "cell_type": "code", "execution_count": 5, "id": "smooth-willow", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Horizontal justification for plot title, subtitle, caption, legend and axis titles\n", "\n", "val hjustLeft = elementText(hjust = 0.0)\n", "val hjustMiddle = elementText(hjust = 0.5)\n", "val hjustRight = elementText(hjust = 1.0)\n", "\n", "\n", "fun withHJjust(hjust: Map): org.jetbrains.letsPlot.intern.OptionsMap {\n", " return theme(title = hjust, axisTitle = hjust, plotCaption = hjust, legendTitle = hjust)\n", "}\n", "\n", "val p2 = p + labs(caption = \"The plot caption\")\n", "\n", "\n", "gggrid(\n", " plots = listOf(\n", " p2 + withHJjust(hjustLeft) + ggtitle(\"All titles with hjust = 0.0\"),\n", " p2 + withHJjust(hjustMiddle) + ggtitle(\"All titles with hjust = 0.5\"),\n", " p2 + withHJjust(hjustRight) + ggtitle(\"All titles with hjust = 1.0\"),\n", " ),\n", " ncol = 1,\n", " cellWidth = 600,\n", " cellHeight = 400,\n", " fit = true\n", ")\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "allied-issue", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Vertical justification for legend title\n", "\n", "val vjustBottom = elementText(vjust =0.0)\n", "val vjustCenter = elementText(vjust = 0.5)\n", "val vjustTop = elementText(vjust = 1.0)\n", "\n", "fun withVJjust(vjust: Map): org.jetbrains.letsPlot.intern.OptionsMap {\n", " return theme(legendTitle = vjust)\n", "}\n", "\n", "val p3 = p + theme().legendPositionBottom() + scaleColorDiscrete(guide = guideLegend(ncol = 1))\n", "\n", "gggrid(\n", " plots = listOf(\n", " p3 + withVJjust(vjustBottom) + ggtitle(\"Legend title with vjust = 0.0\"),\n", " p3 + withVJjust(vjustCenter) + ggtitle(\"Legend title with vjust = 0.5\"),\n", " p3 + withVJjust(vjustTop) + ggtitle(\"Legend title with vjust = 1.0\"),\n", " ),\n", " ncol = 1,\n", " cellWidth = 600,\n", " cellHeight = 400,\n", " fit = true\n", ")" ] } ], "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 }