{ "cells": [ { "cell_type": "markdown", "id": "89375319-5ac5-40e0-a8af-eae13ccf07aa", "metadata": {}, "source": [ "# Sunshine Hours" ] }, { "cell_type": "code", "execution_count": 1, "id": "cdd6cc17-7f95-4692-b36d-4f0ec4234e05", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:05.287672Z", "iopub.status.busy": "2024-11-01T20:41:05.285536Z", "iopub.status.idle": "2024-11-01T20:41:08.103181Z", "shell.execute_reply": "2024-11-01T20:41:08.103016Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use dataframe\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "913838dd-ad3c-4657-a5d7-42ef00972d1e", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:08.104929Z", "iopub.status.busy": "2024-11-01T20:41:08.104795Z", "iopub.status.idle": "2024-11-01T20:41:08.939333Z", "shell.execute_reply": "2024-11-01T20:41:08.939100Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":6,\"columns\":[\"Country\",\"City\",\"Year\",\"Month\",\"Sunshine hours\",\"Angle\"],\"kotlin_dataframe\":[{\"Country\":\"Spain\",\"City\":\"Barcelona\",\"Year\":2591.0,\"Month\":\"JAN\",\"Sunshine hours\":158.0,\"Angle\":-15.0},{\"Country\":\"Spain\",\"City\":\"Barcelona\",\"Year\":2591.0,\"Month\":\"FEB\",\"Sunshine hours\":171.0,\"Angle\":-45.0},{\"Country\":\"Spain\",\"City\":\"Barcelona\",\"Year\":2591.0,\"Month\":\"MAR\",\"Sunshine hours\":206.0,\"Angle\":-75.0}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 6

\n", "
CountryCityYearMonthSunshine hoursAngle
SpainBarcelona2591.000000JAN158.000000-15.000000
SpainBarcelona2591.000000FEB171.000000-45.000000
SpainBarcelona2591.000000MAR206.000000-75.000000
\n", " \n", " \n", " " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val shift = 15.0\n", "val targetCities = listOf(\"Barcelona\", \"New York City\", \"Lyon\", \"Paris\", \"Amsterdam\", \"Berlin\")\n", "val angles = mapOf(\n", " \"Jan\" to -shift, \"Feb\" to -30 - shift, \"Mar\" to -60 - shift, \"Apr\" to 90 - shift, \"May\" to 60 - shift, \"Jun\" to 30 - shift,\n", " \"Jul\" to -shift, \"Aug\" to -30 - shift, \"Sep\" to -60 - shift, \"Oct\" to 90 - shift, \"Nov\" to 60 - shift, \"Dec\" to 30 - shift,\n", ")\n", "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/sunshine_hours.csv\")\n", " .filter { \"City\"() in targetCities }\n", " .gather(\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\")\n", " .into(\"Month\", \"Sunshine hours\")\n", " .add(\"Angle\") { angles[\"Month\"()] }\n", " .update { \"Month\"() }.with { it.uppercase() }\n", " .add(\"Id\") { targetCities.indexOf(\"City\"()) }.sortBy(\"Id\").remove(\"Id\")\n", "val dataMap = df.toMap()\n", "df.head(3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "fd65e943-652f-42b8-b26b-3d763476d554", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:08.941268Z", "iopub.status.busy": "2024-11-01T20:41:08.941011Z", "iopub.status.idle": "2024-11-01T20:41:09.357260Z", "shell.execute_reply": "2024-11-01T20:41:09.356746Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val fontFamily = \"ParaType\"\n", "val fontFace = \"bold\"\n", "val backgroundColor = \"#fcf3e4\"\n", "val gridColor = \"#ccc2a2\"\n", "\n", "val yBreaks = listOf(100.0, 200.0)\n", "val yBreaksData = mapOf(\n", " \"x\" to List(yBreaks.size) { \"JAN\" },\n", " \"y\" to yBreaks,\n", " \"text\" to yBreaks.map(Any::toString),\n", ")\n", "\n", "letsPlot(dataMap) { x = asDiscrete(\"Month\", levels = angles.keys.toList()); y = \"Sunshine hours\" } +\n", " geomText(data = yBreaksData, size = 6, vjust = 0,\n", " angle = -shift, family = fontFamily, fontface = fontFace)\n", " { x = \"x\"; y = \"y\"; label = \"text\" } +\n", " geomBar(stat = Stat.identity, color = \"black\", fill = \"#fbc117\",\n", " tooltips = layerTooltips().title(\"@City\").line(\"@Month: @{Sunshine hours}\")) +\n", " geomLabel(y = 330, family = fontFamily, fontface = fontFace, size = 8,\n", " labelPadding = 0, labelR = 0, labelSize = 0, fill = backgroundColor)\n", " { x = \"Month\"; label = \"Month\"; angle = \"Angle\" } +\n", " facetWrap(facets = \"City\", order = null) +\n", " coordPolar(start = PI * shift / 180) +\n", " labs(title = \"Comparing Sunshine Hours\",\n", " caption = \"Sunshine duration is expressed in (average) hours per month\\n\" +\n", " \"Data source: \" +\n", " \"\" +\n", " \"sunshine hours for cities around the world\" +\n", " \"\\n\" +\n", " \"Original plot: \" +\n", " \"\" +\n", " \"Giulia Mezzadri, PhD's Post\" +\n", " \"\") +\n", " ggsize(1000, 850) +\n", " theme(axis = \"blank\",\n", " plotTitle = elementText(size = 32, family = fontFamily, face = fontFace, margin = listOf(20, 20, 30)),\n", " plotCaption = elementText(family = fontFamily, face = fontFace),\n", " stripText = elementText(size = 20, family = fontFamily, face = fontFace),\n", " plotBackground = elementRect(fill = backgroundColor),\n", " panelGrid = elementLine(color = gridColor, size = 2))" ] } ], "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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }