{ "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": [ "val 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 = mpg_df.toMap()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default legend\n", "val p = letsPlot(mpg) {x=\"displ\"; y=\"hwy\"; color=\"manufacturer\"} + geomPoint(size=5.0) + ggsize(600,250)\n", "p" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Layout the legend in two columns\n", "p + scaleColorDiscrete(guide=guideLegend(ncol=2)) + ggtitle(\"Two columns legend\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Same and fill by rows\n", "p + scaleColorDiscrete(guide=guideLegend(ncol=2, byRow=true)) +\n", "ggtitle(\"Two columns legend filled by rows\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Adjust legend and axis\n", "// - five rows legend\n", "// - draw legend below plot\n", "// - remove axis\n", "p + scaleColorDiscrete(guide=guideLegend(nrow=5)) +\n", " theme(axisTitle=\"blank\").legendPositionBottom() +\n", " ggtitle(\"Five rows legend and below\") + ggsize(700,400)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Set color to city MPG and shape to driveĀ·train\n", "val p1 = letsPlot(mpg) {x=\"displ\"; y=\"hwy\"} + geomPoint(size=5.0) {color=\"cty\"; shape=\"drv\"} + ggsize(700, 350)\n", "p1" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Change legend position and orientation\n", "p1 + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Adjust colorbar size\n", "// Change order in drive-train legend\n", "// Set labels to legends and axis\n", "// Remove vertical axis line\n", "p1 + theme(axisLineY=\"blank\").legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal() +\n", " scaleColorContinuous(name=\"City MPG\", \n", " low=\"dark_blue\", high=\"light_blue\", \n", " guide=guideColorbar(barHeight=10, barWidth=300)) +\n", " scaleShape(name=\"Drive-train\", breaks=listOf('f', 'r', '4'), labels=listOf(\"front\", \"rear\", \"4X4\")) +\n", " xlab(\"Engine displacement (L)\") + ylab(\"Highway MPG\")" ] } ], "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 }