{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GGBunch\n", "\n", "*GGBunch* allows to show a collection of plots on one figure. Each plot in the collection can have arbitrary location and size. There is no automatic layout inside the bunch." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "\n", "@file:DependsOn(\"org.apache.commons:commons-math3:3.6.1\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import kotlin.random.Random\n", "import org.apache.commons.math3.distribution.MultivariateNormalDistribution" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "val cov : Array = arrayOf(doubleArrayOf(1.0, 0.0),\n", " doubleArrayOf(0.0, 1.0))\n", "val means : DoubleArray = doubleArrayOf(0.0, 0.0)\n", "val xy = MultivariateNormalDistribution(means, cov).sample(400)\n", "\n", "val xs = xy.map { it[0] }\n", "val ys = xy.map { it[1] }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View this data as a scatter plot and as a histogram" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() + ggsize(600,200)\n", "\n", "val scatter = p + geomPoint(color=\"black\", alpha=.4) {x=xs; y=ys}\n", "scatter" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val histogram = p + geomHistogram(fill=\"dark_magenta\") {x=xs}\n", "histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combine both plots in one figure" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Set scale X limits manually because of computed automatically\n", "// the scale used by each plot would be slightly different\n", "// and the stacked plots wouldn't be aligned.\n", "val scale_x = scaleXContinuous(limits=-3.5 to 3.5)\n", "GGBunch().addPlot(histogram + scale_x, 0, 0)\n", " .addPlot(scatter + scale_x, 0, 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust visuals of the bunch figure" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "val upper_theme = theme(axis=\"blank\", axisText=elementText(), axisTitleY=elementText(), panelGridMajorY=\"blank\")\n", "val lower_theme = theme(axis=\"blank\", axisTextY=elementText(), axisTitle=elementText())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GGBunch().addPlot(histogram + upper_theme + scale_x, 0, 0)\n", " .addPlot(scatter + lower_theme + scale_x, 0, 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust plot sizes\n", "\n", "*addPlot()* method has two more (optional) parameters: *width* and *height*.\n", "\n", "This values will override plot size earlier defined via *ggsize()* function." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GGBunch().addPlot(histogram + upper_theme + scale_x, 0, 0, 600, 100)\n", " .addPlot(scatter + lower_theme + scale_x, 0, 100, 600, 300)" ] } ], "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": 4 }