{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "9f366262-c64d-4779-89cd-5fb9ffd226a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.mapping import as_discrete\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "f678945a-d73f-42fe-89da-f801cf69b2a4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"x\": [1.0]}\n", "\n", "ggplot(data, aes(x = \"x\", y = as_discrete(\"x\", levels = [1.0]))) + geom_point()" ] }, { "cell_type": "code", "execution_count": 3, "id": "756e2472-f93a-4cb6-b800-dd0209c7f988", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = { \"v1\": [\"a\", \"b\", \"c\"]}\n", "\n", "ggplot(data, aes(x = as_discrete(\"v1\", levels = [\"b\", \"c\", \"a\"]), color = as_discrete(\"v1\", order = -1))) + geom_point()" ] }, { "cell_type": "code", "execution_count": 4, "id": "49f25dd5-13a9-4f4a-8e90-f4fd2867d0a4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {\n", " 'x': [0, 1, 2],\n", " 'v': [3, 1, 2],\n", " 'o': [5, 6, 4]\n", "}\n", "\n", "p = ggplot(d) + ggsize(300, 200)\n", "\n", "gggrid([\n", " p + geom_point(aes(x='x', color='v')) + ggtitle(\"Default\"),\n", " p + geom_point(aes(x='x', color=as_discrete('v'))) + ggtitle('as_discrete(v)')\n", "]) + ggsize(600, 200)" ] }, { "cell_type": "markdown", "id": "12a0a8bd-12b5-4458-8110-03065a42cc00", "metadata": {}, "source": [ "Single order option" ] }, { "cell_type": "code", "execution_count": 5, "id": "47347e9a-902a-498e-941b-1399e4eea362", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3]))) + ggtitle(\"levels=[1, 2, 3]\"),\n", " p + geom_point(aes(x='x', color=as_discrete('v', order=-1))) + ggtitle(\"order=-1\"),\n", " p + geom_point(aes(x='x', color=as_discrete('v', order_by='o'))) + ggtitle(\"order_by='o'\")\n", "]) + ggsize(1000, 200)" ] }, { "cell_type": "markdown", "id": "6685573d-960e-45a3-8809-66817cc4ee6c", "metadata": {}, "source": [ "orderBy" ] }, { "cell_type": "code", "execution_count": 6, "id": "ed4e419b-04ae-4647-8453-a421c8f7024b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_point(aes(x='x', color=as_discrete('v', order_by='o', order=-1))) + ggtitle('order_by=o, order=-1'),\n", " p + geom_point(aes(x='x', color=as_discrete('v', order_by='o', order=1))) + ggtitle('order_by=o, order=1'),\n", "]) + ggsize(600, 200)" ] }, { "cell_type": "markdown", "id": "a8b44af1-73d4-4e79-a35d-33a2589ecab7", "metadata": {}, "source": [ "Order with levels" ] }, { "cell_type": "code", "execution_count": 7, "id": "dd108472-20fb-4cd2-83f6-6c33c45afb47", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order=-1))) + ggtitle('levels=[1, 2, 3], order=-1') + ggsize(800, 600)" ] }, { "cell_type": "code", "execution_count": 8, "id": "229ccf6a-5e8e-4fc6-920a-cca34d4a653c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order=-1))) + ggtitle('levels=[1, 2, 3], order=-1'),\n", " p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order_by='o'))) + ggtitle('levels=[1, 2, 3], order_by=o'),\n", "]) + ggsize(600, 200)\n", " " ] }, { "cell_type": "markdown", "id": "ba8bec14-fcaa-4fcc-9f62-99984595173d", "metadata": {}, "source": [ "Label" ] }, { "cell_type": "code", "execution_count": 9, "id": "fba06bef-4b94-445d-a093-b4024b3ed07f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_point(aes(x='x', color=as_discrete('v', label='VVV'))) + ggtitle(\"label=VVV\"),\n", " p + geom_point(aes(x='x', color=as_discrete('v', label='VVV', levels=[1, 2, 3]))) + ggtitle(\"label=VVV, levels=[1, 2, 3]\"),\n", "]) + ggsize(600, 200)" ] }, { "cell_type": "markdown", "id": "33f93add-4e3c-4e1d-a684-52317b0bdf18", "metadata": {}, "source": [ "Grouping" ] }, { "cell_type": "code", "execution_count": 10, "id": "a46cf798-abcd-4305-bfb1-e82c48121f34", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = {\n", " 'x': [0, 5, 10, 15],\n", " 'y': [0, 5, 10, 15],\n", " 'a': [0, 0, 1, 1],\n", " 'c': ['a', 'a', 'b', 'b']\n", "}\n", "\n", "ggplot(df, aes(x='x', y='y')) + geom_line(aes(color=as_discrete('a')), size=3)" ] }, { "cell_type": "code", "execution_count": 11, "id": "5b7d6839-b399-4f09-9c04-4d8734f79b14", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = {\n", " 'displ': [11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n", " 'hwy': [27, 25, 23, 45, 43, 41, 39, 37, 35, 33, 31, 29],\n", " 'cyl': [8, 8, 8, 4, 4, 4, 5, 5, 5, 6, 6, 6]\n", "}\n", "\n", "ggplot(df, aes(x='displ', y='hwy')) \\\n", " + geom_point(aes(color=as_discrete('cyl'))) \\\n", " + geom_smooth(aes(color=as_discrete('cyl')), method='lm', size=1, se=False)" ] }, { "cell_type": "code", "execution_count": 12, "id": "e4670b9a-886f-457d-ba20-a9746dddbaa9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x='displ', y='hwy')) \\\n", " + geom_point(aes(color=as_discrete('cyl', levels=[4, 5, 6, 8]))) \\\n", " + geom_smooth(aes(color=as_discrete('cyl')), method='lm', size=1, se=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "8960ff55-3327-4efa-8908-1b0198ce35ac", "metadata": {}, "outputs": [], "source": [] } ], "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.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }