{ "cells": [ { "cell_type": "markdown", "id": "charitable-basics", "metadata": {}, "source": [ "# Lets-Plot Themes" ] }, { "cell_type": "code", "execution_count": 1, "id": "alien-support", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "DataFrame: rowsCount = 3, columnsCount = 12
\n", " \n", " \n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "df.head(3)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "younger-carolina", "metadata": {}, "outputs": [], "source": [ "// Initialize Lets-Plot GeoTools extension. \n", "%use lets-plot-gt(gt=\"[23,)\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "developed-product", "metadata": {}, "outputs": [], "source": [ "@file:DependsOn(\"org.geotools:gt-shapefile:[23,)\")\n", "@file:DependsOn(\"org.geotools:gt-cql:[23,)\")\n", "\n", "import org.geotools.data.shapefile.ShapefileDataStoreFactory\n", "import org.geotools.data.simple.SimpleFeatureCollection\n", "import java.net.URL" ] }, { "cell_type": "code", "execution_count": 6, "id": "specified-taiwan", "metadata": {}, "outputs": [], "source": [ "val factory = ShapefileDataStoreFactory()\n", "val worldFeatures : SimpleFeatureCollection = with(\"naturalearth_lowres\") {\n", " val url = \"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/shp/${this}/${this}.shp\"\n", " factory.createDataStore(URL(url)).featureSource.features\n", "}\n", "\n", "// Convert Feature Collection to SpatialDataset.\n", "// Use 10 decimals to encode floating point numbers (this is the default).\n", "val world = worldFeatures.toSpatialDataset(10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "terminal-provider", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Oceania, Africa, North America, Asia, South America, Europe, Seven seas (open ocean), Antarctica]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world[\"continent\"]?.distinct()" ] }, { "cell_type": "code", "execution_count": 8, "id": "unlike-portfolio", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p_plot = ggplot(df.toMap()) + geomPoint {x = \"cty\"; y=\"hwy\"}\n", "p_plot" ] }, { "cell_type": "code", "execution_count": 9, "id": "documented-occasion", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val b_plot = letsPlot(df.toMap()) + geomBar {x = \"fl\"; color=\"fl\"; fill=\"fl\"}\n", "b_plot" ] }, { "cell_type": "code", "execution_count": 10, "id": "simplified-stick", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val f_plot = b_plot + facetGrid(x=\"year\")\n", "f_plot" ] }, { "cell_type": "code", "execution_count": 11, "id": "modified-while", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val c_plot = CorrPlot(df.toMap(), flip=false).points().build()\n", "c_plot" ] }, { "cell_type": "code", "execution_count": 12, "id": "permanent-testimony", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val g_plot = ggplot() + geomMap(data=world)\n", "g_plot" ] }, { "cell_type": "markdown", "id": "irish-organizer", "metadata": {}, "source": [ "### Elements" ] }, { "cell_type": "code", "execution_count": 13, "id": "apparent-eating", "metadata": {}, "outputs": [], "source": [ "val e_blank = elementBlank()\n", "val e_line = elementLine(color=java.awt.Color.RED, size=4)\n", "val e_line2 = elementLine(color=\"#fcae91\", size=1)\n", "val e_line3 = elementLine(size=4)\n", "val e_rect = elementRect(color=\"#2c7fb8\", fill=\"#edf8b1\", size=2)\n", "val e_text = elementText(color=\"#31a354\")" ] }, { "cell_type": "markdown", "id": "conventional-differential", "metadata": {}, "source": [ "### Themes" ] }, { "cell_type": "code", "execution_count": 14, "id": "worst-regression", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val (w, h) = 400 to 300\n", "gggrid(\n", " plots = listOf(\n", " p_plot + ggtitle(\"themeMinimal2() - the default\"),\n", " p_plot + ggtitle(\"themeLight()\") + themeLight(),\n", " p_plot + ggtitle(\"themeClassic()\") + themeClassic(),\n", " p_plot + ggtitle(\"themeGrey()\") + themeGrey(),\n", " p_plot + ggtitle(\"themeMinimal()\") + themeMinimal(),\n", " p_plot + ggtitle(\"themeNone()\") + themeNone()\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "markdown", "id": "prepared-gardening", "metadata": {}, "source": [ "### Custom Theme" ] }, { "cell_type": "code", "execution_count": 15, "id": "gross-individual", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fun orangeTheme() : org.jetbrains.letsPlot.intern.Feature {\n", " val yellowLight = \"#ffffcc\"\n", " val orangeDark = \"#7f2704\"\n", " val orangeNormal = \"#f16913\"\n", " val orangeLight = \"#fff5eb\"\n", " \n", " return themeNone() + theme(\n", " line=elementLine(color=orangeNormal, size=2),\n", " rect=elementRect(color=orangeNormal, fill=orangeLight, size=2),\n", " text=elementText(color=orangeDark),\n", " axisOntop=true,\n", " axisTicks=elementLine(color=orangeNormal, size=1),\n", " axisTicksLength=7,\n", " legendBackground=elementRect(size=1),\n", " panelGridMajor=elementLine(color=orangeNormal, size=.5),\n", " panelGridMinor=\"blank\",\n", " plotBackground=elementRect(fill=yellowLight, size=1),\n", " axisTooltip=elementRect(color=orangeDark)\n", " ).legendPositionBottom()\n", "}\n", "\n", "gggrid(\n", " plots = listOf(\n", " p_plot + orangeTheme() + ggtitle(\"Scatter plot\"),\n", " b_plot + orangeTheme() + ggtitle(\"Bar plot\"),\n", " c_plot + orangeTheme() + ggtitle(\"Correlation plot\"),\n", " g_plot + orangeTheme() + ggtitle(\"World plot\"),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "id": "solved-mortgage", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_plot + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal() +\n", " ggtitle(\"Place legend\")" ] }, { "cell_type": "markdown", "id": "positive-evans", "metadata": {}, "source": [ "### Axis tooltip" ] }, { "cell_type": "code", "execution_count": 17, "id": "approximate-trademark", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " p_plot + ggtitle(\"axisTooltipX=e_blank\") + theme(axisTooltipX = e_blank),\n", " p_plot + ggtitle(\"axisTooltipX=e_rect\") + theme(axisTooltipX = e_rect),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", ")" ] }, { "cell_type": "markdown", "id": "divided-destiny", "metadata": {}, "source": [ "### Line, rectangle, text" ] }, { "cell_type": "code", "execution_count": 18, "id": "southeast-wireless", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " p_plot + ggtitle(\"Default\"),\n", " p_plot + ggtitle(\"line=e_line\") + theme(line = e_line),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "id": "artificial-space", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " b_plot + ggtitle(\"themeNone\") + themeNone(),\n", " b_plot + ggtitle(\"themeNone + rect\") + themeNone() + theme(rect = e_rect),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", ")" ] }, { "cell_type": "code", "execution_count": 20, "id": "controlled-railway", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " f_plot + ggtitle(\"Default\"),\n", " f_plot + ggtitle(\"text = e_text\") + theme(text = e_text),\n", " f_plot + ggtitle(\"legendText = e_text\") + theme(legendText = e_text),\n", " f_plot + ggtitle(\"stripText = e_text\") + theme(stripText = e_text),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 21, "id": "organizational-check", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " b_plot + ggtitle(\"Default\"),\n", " b_plot + ggtitle(\"text = e_text\") + theme(text = e_text),\n", " b_plot + ggtitle(\"plotTitle = e_text\") + theme(plotTitle = e_text),\n", " b_plot + ggtitle(\"legendTitle = e_text\") + theme(legendTitle = e_text),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 22, "id": "protecting-christianity", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " p_plot + ggtitle(\"Default\"),\n", " p_plot + ggtitle(\"axis = e_blank\") + theme(axis = e_blank),\n", " p_plot + ggtitle(\"axis = e_line\") + theme(axis = e_line),\n", " p_plot + ggtitle(\"axis = e_text\") + theme(axis = e_text),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 23, "id": "forty-sapphire", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " p_plot + ggtitle(\"Default\"),\n", " p_plot + ggtitle(\"panelBackground = e_rect\") + theme(panelBackground = e_rect),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 24, "id": "secret-leeds", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " b_plot + ggtitle(\"panelGrid = e_blank\") + theme(panelGrid = e_blank),\n", " p_plot + ggtitle(\"panelGrid = e_line\") + theme(panelGrid = e_line),\n", " p_plot + ggtitle(\"panelGridMajor = e_line, panelGridMinor = e_line2\") + \n", " theme(panelGridMajor = e_line, panelGridMinor = e_line2),\n", " b_plot + ggtitle(\"panelGridMajorX = e_blank, panelGridMajorY, panelGridMinorY..\") + \n", " theme(panelGridMajorX = e_blank, panelGridMajorY = e_line,\n", " panelGridMinorY = e_line2),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 25, "id": "convinced-emphasis", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " f_plot + ggtitle(\"themeGrey\") + themeGrey(),\n", " f_plot + ggtitle(\"stripBackground = e_rect\") + themeGrey() + theme(stripBackground = e_rect),\n", " f_plot + ggtitle(\"stripBackground = e_blank\") + themeGrey() + theme(stripBackground = e_blank),\n", " f_plot + ggtitle(\"stripText = e_blank\") + themeGrey() + theme(stripText = e_blank),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 26, "id": "organized-processor", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " b_plot + ggtitle(\"Default\") + theme(axisLineX = e_line3),\n", " b_plot + ggtitle(\"axisOntopX = true\") + theme(axisLineX = e_line3, axisOntopX = true),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "markdown", "id": "published-moses", "metadata": {}, "source": [ "### Miscellaneous" ] }, { "cell_type": "code", "execution_count": 27, "id": "diverse-vegetarian", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " b_plot + ggtitle(\"Default\") + coordFlip(),\n", " b_plot + ggtitle(\"Plot 1\") + coordFlip() + theme(line=e_line, text=e_text),\n", " b_plot + ggtitle(\"Plot 2\") + coordFlip() + theme(axis=e_line, legendTitle=e_text,\n", " axisOntop=true\n", " ),\n", " b_plot + ggtitle(\"Plot 3\") + coordFlip() + theme(plotTitle=e_text,\n", " panelBackground=e_rect,\n", " panelGridMajorX=e_blank,\n", " panelGridMajorY=e_line,\n", " panelGridMinorX=e_blank,\n", " panelGridMinorY=e_line2,\n", " ),\n", " f_plot + ggtitle(\"Plot 4\") + coordFlip() + theme(stripBackground=e_rect, stripText=e_text),\n", " b_plot + ggtitle(\"Plot 5\") + coordFlip() + theme(axisTitleX=e_text,\n", " axisTextX=e_text,\n", " axisTicksX=e_line,\n", " axisLineX=e_line2,\n", " axisTooltipX=e_blank,\n", " ).legendPositionBottom().legendDirectionHorizontal(),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 28, "id": "photographic-buffalo", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " c_plot + ggtitle(\"Default\"),\n", " c_plot + ggtitle(\"With theme\") + theme(text=e_text,\n", " axis=e_line,\n", " panelBackground=e_rect,\n", " axisTitleX=e_text,\n", " axisTextX=e_text,\n", " axisTicksX=e_line,\n", " axisLineX=e_line,\n", " axisTooltipX=e_blank\n", " ),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 29, "id": "seven-compromise", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(\n", " plots = listOf(\n", " g_plot + ggtitle(\"Default\"),\n", " g_plot + ggtitle(\"coordCartesian()\") + coordCartesian(),\n", " g_plot + ggtitle(\"coordFixed()\") + coordFixed(),\n", " g_plot + ggtitle(\"themeClassic()\") + themeClassic(),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", " fit = true\n", ")" ] }, { "cell_type": "code", "execution_count": 30, "id": "horizontal-asbestos", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val pt_plot = ggplot(df.toMap()) {x = \"cty\"; y = \"hwy\"} +\n", " geomPoint(tooltips=layerTooltips().anchor(\"top_center\").minWidth(50)) {color=\"fl\"}\n", "\n", "gggrid(\n", " plots = listOf(\n", " pt_plot + ggtitle(\"Tooltip: top-center\"),\n", " pt_plot + ggtitle(\"Gray background\") + theme(panelBackground=elementRect(color=\"black\", fill=\"gray\")),\n", " ),\n", " ncol = 2,\n", " cellWidth = w,\n", " cellHeight = h,\n", ") " ] } ], "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": 5 }