{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting means and error ranges.\n", "\n", "There are several ways to show error ranges on a plot. Among them are\n", "- `geomErrorBar()`\n", "- `geomCrossbar()`\n", "- `geomLineRange()`\n", "- `geomPointRange()`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "// This example was found at: www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)\n", "val data = mapOf(\n", " \"supp\" to listOf(\"OJ\", \"OJ\", \"OJ\", \"VC\", \"VC\", \"VC\"),\n", " \"dose\" to listOf(0.5, 1.0, 2.0, 0.5, 1.0, 2.0),\n", " \"length\" to listOf(13.23, 22.70, 26.06, 7.98, 16.77, 26.14),\n", " \"len_min\" to listOf(11.83, 21.2, 24.50, 4.24, 15.26, 23.35),\n", " \"len_max\" to listOf(15.63, 24.9, 27.11, 10.72, 19.28, 28.93)\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(data) {x=\"dose\"; color=\"supp\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Error-bars with lines and points." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomErrorBar(width = .1) {ymin = \"len_min\"; ymax = \"len_max\"} +\n", " geomLine {y = \"length\"} +\n", " geomPoint {y = \"length\"}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// The errorbars overlapped, so use position_dodge to move them horizontally\n", "val pd = positionDodge(0.1) // move them .05 to the left and right\n", "p + geomErrorBar(width=.1, position=pd) {ymin=\"len_min\"; ymax=\"len_max\"} +\n", " geomLine(position=pd) {y=\"length\"} +\n", " geomPoint(position=pd) {y=\"length\"}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Black errorbars - notice the mapping of 'group=supp'\n", "// Without it, the errorbars won't be dodged!\n", "p + geomErrorBar(\n", " color = \"pen\", \n", " width = .1, \n", " position = pd) {ymin = \"len_min\"; ymax = \"len_max\"; group = \"supp\"} +\n", " geomLine(position = pd) {y = \"length\"} +\n", " geomPoint(position = pd, size = 5) {y = \"length\"}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Finished graph\n", "// - fixed size\n", "// - point shape # 21 is filled circle \n", "// - position legend in bottom right\n", "val p1 = p +\n", " xlab(\"Dose (mg)\") +\n", " ylab(\"Tooth length (mm)\") +\n", " scaleColorManual(listOf(\"orange\", \"dark_green\"), naValue = \"gray\") +\n", " ggsize(700, 400)\n", "p1 + geomErrorBar(color = \"pen\", \n", " width = .1, \n", " position = pd) {ymin = \"len_min\"; ymax = \"len_max\"; group = \"supp\"} +\n", " geomLine(position = pd) {y = \"length\"} +\n", " geomPoint(position = pd, size = 5, shape = 21) {y = \"length\"} +\n", " theme().legendJustification(1,0).legendPosition(1,0) +\n", " ggtitle(\"The Effect of Vitamin C on Tooth Growth in Guinea Pigs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Error-bars on bar plot." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Plot error ranges on Bar plot\n", "p1 + \n", " geomBar(stat = Stat.identity, \n", " position = positionDodge(), \n", " color = \"pen\") {y = \"length\"; fill = \"supp\"} +\n", " geomErrorBar(width = .1, \n", " position = positionDodge(0.9),\n", " color = \"pen\") {\n", " ymin = \"len_min\"\n", " ymax = \"len_max\" \n", " group = \"supp\"} +\n", " theme().legendJustification(0,1).legendPosition(0,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Crossbars." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Thickness of the horizontal mid-line can be adjusted using `fatten` parameter.\n", "p1 + geomCrossbar(fatten = 5.0) {\n", " ymin=\"len_min\"\n", " ymax=\"len_max\"\n", " y=\"length\"\n", " color=\"supp\"\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line-range." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + geomLineRange(position=pd) {ymin=\"len_min\"; ymax=\"len_max\"; color=\"supp\"} +\n", " geomLine(position=pd) {y=\"length\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Point-range." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Point-range is the same as line-range but with an added mid-point.\n", "p1 + geomPointRange(position=pd) {y=\"length\"; ymin=\"len_min\"; ymax=\"len_max\"; color=\"supp\"} +\n", " geomLine(position=pd) {y=\"length\"}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Size of the mid-point can be adjuasted using `fatten` parameter - multiplication factor relative to the line size.\n", "p1 + geomLine(position = pd) {y = \"length\"} +\n", " geomPointRange(position = pd, \n", " color = \"rgb(230, 230, 230)\", \n", " size = 5, \n", " linewidth = 5, \n", " shape = 23, fatten = 1.0) {\n", " y = \"length\"\n", " ymin = \"len_min\"\n", " ymax = \"len_max\"\n", " fill = \"supp\"\n", " } +\n", " scaleFillManual(listOf(\"orange\", \"dark_green\"), naValue=\"gray\")" ] } ], "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 }