{ "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", "import java.util.Random" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "// This example was found at: \n", "// www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)\n", "\n", "val rand = java.util.Random(123)\n", "val n = 20\n", "val data = mapOf>(\n", " \"cond\" to List(n / 2) { \"A\" } + List(n / 2) { \"B\" },\n", " \"xvar\" to List(n) { i:Int-> i }, \n", " \"yvar\" to List(n) { i:Int-> i + rand.nextGaussian() * 3 }\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic scatter plot" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(data) { x = \"xvar\"; y = \"yvar\" } + ggsize(300, 250)\n", "p + geomPoint(shape = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add regression line" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomPoint(shape = 1) +\n", " geomSmooth()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Without standard error band.\n", "p + geomPoint(shape = 1) +\n", " geomSmooth(se = false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Split dataset by the `cond` variable" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p1 = letsPlot(data) { x = \"xvar\"; y = \"yvar\"; color = \"cond\" } + ggsize(500, 250)\n", "p1 + geomPoint(shape = 1) +\n", " geomSmooth(se = false)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Map `shape` to the `cond` variable.\n", "p1 + geomPoint(size = 5) { shape = \"cond\" }" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Choose different shapes using `scale_shape_manual`:\n", "// 1 - hollow circle \n", "// 2 - hollow triangle\n", "p1 + geomPoint(size = 5) { shape = \"cond\" } + \n", " scaleShapeManual(values = listOf(1,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Handling overplotting" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "// Create data with overlapping points.\n", "val data1 = mapOf(\n", " \"xvar\" to (data[\"xvar\"] as List).map { (it / 5).toInt() * 5 },\n", " \"yvar\" to (data[\"yvar\"] as List).map { (it / 5).toInt() * 5 },\n", " )" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p2 = letsPlot(data1) { x = \"xvar\"; y = \"yvar\"} + ggsize(500, 250) +\n", " scaleXContinuous(breaks = listOf(0, 5, 10, 15))\n", "// Use `alpha` to show overplotting.\n", "p2 + geomPoint(alpha = .3, size = 7)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// `jitter` points to show overplotting in another way.\n", "p2 + geomPoint(shape = 1, position = positionJitter(width=.1, height=.1))" ] } ], "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 }