{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "prescription-boost", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "id": "rural-denial", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "employed-browser", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1 = {\n", " 'x' : ['c', 'c', 'a', 'a', 'd', 'b', 'b', 'a']\n", "}\n", "\n", "gggrid([\n", " ggplot(df1) + geom_bar(aes('x')),\n", " ggplot(df1) + geom_bar(aes(as_discrete('x', levels=['a','b','c','d']))),\n", " ggplot(df1) + geom_bar(aes(as_discrete('x', levels=['a','d']))) # missed values will be added\n", "])" ] }, { "cell_type": "code", "execution_count": 4, "id": "trying-happiness", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 = {\n", " 'x' : [3, 2, 1, 3, 3, 1, 4]\n", "}\n", "\n", "gggrid([\n", " ggplot(df2) + geom_bar(aes('x', fill = as_discrete('x'))),\n", " ggplot(df2) + geom_bar(aes('x', fill = as_discrete('x', levels=[1,2,3,4])))\n", "],ncol=1)" ] }, { "cell_type": "code", "execution_count": 5, "id": "inner-yacht", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df2) + geom_bar(aes(as_discrete('x',order=-1), fill = as_discrete('x', levels=[1,2,3,4])))" ] }, { "cell_type": "code", "execution_count": 6, "id": "understood-birmingham", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df2) + geom_bar(aes(as_discrete('x',order=-1), fill = as_discrete('x', levels=[1,2])))" ] }, { "cell_type": "code", "execution_count": 7, "id": "structured-anthony", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# https://github.com/JetBrains/lets-plot/issues/914\n", "\n", "data = dict(\n", " cat = [\"B\", \"C\", \"A\"],\n", " c = [\"blue\", \"cyan\", \"gray\"]\n", ")\n", "\n", "(ggplot(data) \n", " + geom_point(aes(color=\"c\"), size=15) \n", " + scale_color_identity() \n", " + theme_void() + ggsize(400, 200)\n", " + facet_wrap(\"cat\", ncol=3, order=0)\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "convinced-mouse", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Facet ordering corresponds to the levels (facet_wrap)\n", "\n", "(ggplot(data) \n", " + geom_point(aes(color=as_discrete(\"cat\", levels=[\"C\",\"A\",\"B\"])), size=15)\n", " + theme_void() + ggsize(400, 200)\n", " + facet_wrap(\"cat\", ncol=3, order=0)\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "726ecdca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Facet ordering corresponds to the levels (facet_grid)\n", "\n", "(ggplot(data) \n", " + geom_point(aes(color=as_discrete(\"cat\", levels=[\"C\",\"A\",\"B\"])), size=15)\n", " + theme_void() + ggsize(400, 200)\n", " + facet_grid(\"cat\", x_order=0)\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "id": "f517eafb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# levels in reverse order\n", "\n", "(ggplot(data) \n", " + geom_point(aes(color=as_discrete(\"cat\", levels=[\"C\",\"A\",\"B\"], order=-1)), size=15)\n", " + theme_void() + ggsize(400, 200)\n", " + facet_wrap(\"cat\", ncol=3, order=0)\n", ")" ] } ], "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.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }