{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:30.705856Z", "iopub.status.busy": "2024-04-17T07:31:30.705783Z", "iopub.status.idle": "2024-04-17T07:31:31.029582Z", "shell.execute_reply": "2024-04-17T07:31:31.029385Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.042987Z", "iopub.status.busy": "2024-04-17T07:31:31.042844Z", "iopub.status.idle": "2024-04-17T07:31:31.174824Z", "shell.execute_reply": "2024-04-17T07:31:31.174386Z" } }, "outputs": [], "source": [ "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.175985Z", "iopub.status.busy": "2024-04-17T07:31:31.175892Z", "iopub.status.idle": "2024-04-17T07:31:31.177719Z", "shell.execute_reply": "2024-04-17T07:31:31.177543Z" } }, "outputs": [], "source": [ "p = ggplot(mpg) + theme(axis_title_x='blank')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.178721Z", "iopub.status.busy": "2024-04-17T07:31:31.178648Z", "iopub.status.idle": "2024-04-17T07:31:31.230486Z", "shell.execute_reply": "2024-04-17T07:31:31.230289Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p1 = p + ggtitle(\"default\") + geom_boxplot( aes('class','hwy'))\n", "\n", "p2 = (p + ggtitle(\"alphabetically\") \n", " + geom_boxplot(aes(as_discrete('class',order=1), 'hwy')))\n", "p3 = (p + ggtitle(\"middle ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..middle..'), 'hwy')))\n", "p4 = (p + ggtitle(\"middle ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..middle..'), 'hwy')))\n", "p5 = (p + ggtitle(\"ymax ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..ymax..'), 'hwy')))\n", "p6 = (p + ggtitle(\"ymax ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..ymax..'), 'hwy')))\n", "p7 = (p + ggtitle(\"ymin ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..ymin..'), 'hwy')))\n", "p8 = (p + ggtitle(\"ymin ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..ymin..'), 'hwy')))\n", "\n", "\n", "w, h = 470, 300\n", "\n", "bunch = GGBunch()\n", "\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "bunch.add_plot(p4, w, h, w, h)\n", "bunch.add_plot(p5, 0, h*2, w, h)\n", "bunch.add_plot(p6, w, h*2, w, h)\n", "bunch.add_plot(p7, 0, h*3, w, h)\n", "bunch.add_plot(p8, w, h*3, w, h)\n", "\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.231611Z", "iopub.status.busy": "2024-04-17T07:31:31.231531Z", "iopub.status.idle": "2024-04-17T07:31:31.254835Z", "shell.execute_reply": "2024-04-17T07:31:31.254646Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ordered_class = as_discrete('class', order=1)\n", "\n", "p1 = (p + ggtitle(\"x='class', color='class'\") \n", " + geom_boxplot(aes('class', 'hwy', color='class'))\n", " )\n", "\n", "p3 = (p + ggtitle(\"x=as_discrete('class', order=1), color='class'\") \n", " + geom_boxplot(aes(ordered_class, 'hwy', color='class'))\n", " )\n", "\n", "p4 = (p + ggtitle(\"x=as_discrete('class', order=1), color=as_discrete('class')\") \n", " + geom_boxplot(aes(ordered_class, 'hwy', color=as_discrete('class')))\n", " )\n", "\n", "\n", "p5 = p + ggtitle(\"x='class', color='drv'\") + geom_boxplot(aes('class','hwy', color='drv'))\n", "\n", "p7 = (p + ggtitle(\"x=as_discrete('class', order=1), color=as_discrete('drv', order=1)\") \n", " + geom_boxplot(aes(\n", " as_discrete('class', order=1), 'hwy', \n", " color=as_discrete('drv', order=1)))\n", " )\n", "\n", "p8 = (p + ggtitle(\"x=as_discrete('class', order_by='..middle..')\") \n", " + geom_boxplot(aes(\n", " as_discrete('class', order_by='..middle..', order=1), 'hwy', color='drv'))\n", " )\n", "\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "bunch.add_plot(p4, w, h, w, h)\n", "bunch.add_plot(p5, 0, h*2, w, h)\n", "bunch.add_plot(p7, 0, h*3, w, h)\n", "bunch.add_plot(p8, w, h*3, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.255918Z", "iopub.status.busy": "2024-04-17T07:31:31.255842Z", "iopub.status.idle": "2024-04-17T07:31:31.266145Z", "shell.execute_reply": "2024-04-17T07:31:31.265976Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " \n", "p1 = (ggplot(mpg, aes('class', 'hwy')) + \n", " geom_crossbar(aes(color='class'), stat='boxplot') + \n", " scale_color_brewer(type='qual', palette='Dark2') + \n", " ggtitle('default'))\n", "\n", "p2 = (ggplot(mpg, aes(x=as_discrete('class', order_by=\"..middle..\", order=1), y = 'hwy')) + \n", " geom_crossbar(aes(color='class'), stat='boxplot') + \n", " scale_color_brewer(type='qual', palette='Dark2') +\n", " ggtitle('middle ↑, order option also applies to \\'color\\'') )\n", "\n", "p3 = (ggplot(mpg, aes(x=as_discrete('class', order_by=\"..middle..\"), y = 'hwy')) + \n", " geom_crossbar(aes(color=as_discrete('class', order=1)), stat='boxplot') +\n", " scale_color_brewer(type='qual', palette='Dark2') + \n", " ggtitle('\\'class\\' with \\'order_by\\' + \\'class\\' with \\'order\\': options are combined') )\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.267175Z", "iopub.status.busy": "2024-04-17T07:31:31.267080Z", "iopub.status.idle": "2024-04-17T07:31:31.276854Z", "shell.execute_reply": "2024-04-17T07:31:31.276669Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p1 = (p + ggtitle(\"default\") + geom_bar(aes('class', 'hwy')))\n", "\n", "p2 = (p + ggtitle(\"alphabetically\") \n", " + geom_bar(aes(as_discrete('class', order=1), 'hwy')))\n", "\n", "\n", "p3 = (p + ggtitle(\"y ↓\") \n", " + geom_bar(aes(as_discrete('class', order_by='hwy'), 'hwy')))\n", "p4 = (p + ggtitle(\"y ↑\") \n", " + geom_bar(aes(as_discrete('class', order_by='hwy', order=1), 'hwy')))\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "bunch.add_plot(p4, w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.277909Z", "iopub.status.busy": "2024-04-17T07:31:31.277790Z", "iopub.status.idle": "2024-04-17T07:31:31.297252Z", "shell.execute_reply": "2024-04-17T07:31:31.297077Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# geom_bar\n", "\n", "p1 = (p + ggtitle(\"default\") + geom_bar(aes('manufacturer', fill='class')))\n", "\n", "p2 = (p + ggtitle(\"x alphabetically\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill='class')))\n", "\n", "\n", "p3 = (p + ggtitle(\"x alphabetically + sampling_pick(4)\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill='class'), \n", " sampling=sampling_pick(4)))\n", "\n", "p4 = (p + ggtitle(\"fill alphabetically\") \n", " + geom_bar(aes('manufacturer', fill=as_discrete('class',order=1))))\n", "\n", "p5 = (p + ggtitle(\"x and fill alphabetically\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill=as_discrete('class',order=1))))\n", "\n", "p6 = (p + ggtitle(\"x by count (sum is taken)\") \n", " + geom_bar(aes(as_discrete('manufacturer',order_by='..count..'), fill='class')))\n", "\n", "p7 = (p + ggtitle(\"x by count + sampling_pick(4)\") \n", " + geom_bar(aes(as_discrete('manufacturer',order_by='..count..'), fill='class'),\n", " sampling=sampling_pick(4)))\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, 0, h, w, h)\n", "bunch.add_plot(p3, w, h, w, h)\n", "bunch.add_plot(p4, 0, h*2, w, h)\n", "bunch.add_plot(p5, w, h*2, w, h)\n", "bunch.add_plot(p6, 0, h*3, w, h)\n", "bunch.add_plot(p7, w, h*3, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.298282Z", "iopub.status.busy": "2024-04-17T07:31:31.298208Z", "iopub.status.idle": "2024-04-17T07:31:31.314882Z", "shell.execute_reply": "2024-04-17T07:31:31.314710Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pp = ggplot(mpg, aes(x='fl', fill='drv'))\n", "p1 = pp + geom_bar() + ggtitle('Default')\n", "p2 = pp + geom_bar(position='dodge') + ggtitle(\"With Dodge Position\")\n", "\n", "p_order1 = ggplot(mpg, aes(x=as_discrete('fl',order=1), fill=as_discrete('drv',order=1)))\n", "title1 = ggtitle('x and fill alphabetically')\n", "p3 = p_order1 + geom_bar() + title1\n", "p4 = p_order1 + geom_bar(position='dodge') + title1\n", "\n", "p_order2 = ggplot(mpg, aes(x=as_discrete('fl',order_by=\"..count..\"), fill='drv'))\n", "title2 = ggtitle('x by count')\n", "p5 = p_order2 + geom_bar() + title2\n", "p6 = p_order2 + geom_bar(position='dodge') + title2\n", "\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "bunch.add_plot(p4, w, h, w, h)\n", "bunch.add_plot(p5, 0, h*2, w, h)\n", "bunch.add_plot(p6, w, h*2, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:31.315962Z", "iopub.status.busy": "2024-04-17T07:31:31.315817Z", "iopub.status.idle": "2024-04-17T07:31:32.020012Z", "shell.execute_reply": "2024-04-17T07:31:32.019690Z" } }, "outputs": [], "source": [ "# diamonds\n", "\n", "diamonds = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/diamonds.csv')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:32.021372Z", "iopub.status.busy": "2024-04-17T07:31:32.021279Z", "iopub.status.idle": "2024-04-17T07:31:34.382998Z", "shell.execute_reply": "2024-04-17T07:31:34.382704Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pd = ggplot(diamonds)\n", "\n", "p1 = pd + geom_bar(aes('cut', fill = 'clarity')) + ggtitle('Default')\n", "p2 = pd + geom_bar(aes('cut', fill = 'clarity'), position = 'fill') + ggtitle('position=\\'fill\\'')\n", "\n", "p3 = pd + ggtitle('x and fill alphabetically') \\\n", " + geom_bar(aes(as_discrete('cut',order=1), fill = as_discrete('clarity',order=1)), position = 'fill') \n", "p4 = pd + ggtitle('x by count') \\\n", " + geom_bar(aes(as_discrete('cut',order_by='..count..'), \n", " fill = as_discrete('clarity',order=1)),\n", " position = 'fill')\n", "\n", "p5 = pd + geom_bar(aes('cut', fill = 'clarity'), position = 'dodge') + ggtitle('position=\\'dodge\\'')\n", "p6 = pd + ggtitle('x by count and fill alphabetically') \\\n", " + geom_bar(aes(as_discrete('cut',order_by='..count..', order=1),\n", " fill = as_discrete('clarity',order=1)),\n", " position = 'dodge')\n", "\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.add_plot(p3, 0, h, w, h)\n", "bunch.add_plot(p4, w, h, w, h)\n", "bunch.add_plot(p5, 0, h*2, w, h)\n", "bunch.add_plot(p6, w, h*2, w, h)\n", "bunch.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }