{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This page is available as an executable or viewable Jupyter Notebook:\n", "

\n", "\n", " \n", "\n", "\n", " \n", "\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%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 = ggplot(data) { x = \"xvar\"; y = \"yvar\" } + ggsize(300, 250)\n", "p + geom_point(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 + geom_point(shape = 1) +\n", " geom_smooth()" ] }, { "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 + geom_point(shape = 1) +\n", " geom_smooth(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 = ggplot(data) { x = \"xvar\"; y = \"yvar\"; color = \"cond\" } + ggsize(500, 250)\n", "p1 + geom_point(shape = 1) +\n", " geom_smooth(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 + geom_point(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 + geom_point(size = 5) { shape = \"cond\" } + \n", " scale_shape_manual(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 = ggplot(data1) { x = \"xvar\"; y = \"yvar\"} + ggsize(500, 250) +\n", " scale_x_continuous(breaks = listOf(0, 5, 10, 15))\n", "// Use `alpha` to show overplotting.\n", "p2 + geom_point(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 + geom_point(shape = 1, position = position_jitter(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", "pygments_lexer": "kotlin", "version": "1.4.20-dev-2342" } }, "nbformat": 4, "nbformat_minor": 4 }