{ "cells": [ { "cell_type": "markdown", "id": "f3b11b5c-987d-46f3-956d-3004db6d2344", "metadata": {}, "source": [ "# Unemployment in the US since 1967" ] }, { "cell_type": "code", "execution_count": 1, "id": "00fe4a2f-4bd0-4ab5-be23-461dd2c7c43f", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:52.040768Z", "iopub.status.busy": "2024-11-01T20:41:52.038484Z", "iopub.status.idle": "2024-11-01T20:41:54.844009Z", "shell.execute_reply": "2024-11-01T20:41:54.843736Z" } }, "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": "76ace100-3139-4d3a-a683-f14fc232adb9", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:54.845723Z", "iopub.status.busy": "2024-11-01T20:41:54.845523Z", "iopub.status.idle": "2024-11-01T20:41:54.863882Z", "shell.execute_reply": "2024-11-01T20:41:54.863614Z" } }, "outputs": [], "source": [ "import kotlinx.datetime.*" ] }, { "cell_type": "code", "execution_count": 3, "id": "ae2ff904-f7fd-461f-a5f5-3b901a31f98b", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:54.865988Z", "iopub.status.busy": "2024-11-01T20:41:54.865802Z", "iopub.status.idle": "2024-11-01T20:41:55.000029Z", "shell.execute_reply": "2024-11-01T20:41:54.999882Z" } }, "outputs": [], "source": [ "fun LocalDateTime.toMillis(): Long {\n", " return toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()\n", "}\n", "\n", "fun LocalDateTime.Companion.fromYear(year: Int): LocalDateTime {\n", " return LocalDateTime(year, 1, 1, 0, 0)\n", "}\n", "\n", "fun DataFrame<*>.parseDates(column: String): DataFrame<*> {\n", " return this.convert(column).toLocalDateTime()\n", " .convert(column).with { (it as LocalDateTime).toMillis() }\n", "}\n", "\n", "fun millisByYear(year: Int): Long {\n", " return LocalDateTime.fromYear(year).toMillis()\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "615d5402-4a42-427f-b0ef-e9ee17c990bb", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:55.001534Z", "iopub.status.busy": "2024-11-01T20:41:55.001330Z", "iopub.status.idle": "2024-11-01T20:41:55.735338Z", "shell.execute_reply": "2024-11-01T20:41:55.735164Z" } }, "outputs": [], "source": [ "val presidentialDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/presidential.csv\")\n", " .parseDates(\"start\").parseDates(\"end\")\n", "val economicsDf = DataFrame.readCSV(\"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv\")\n", " .parseDates(\"date\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "0532a832-c389-44af-bc36-05e7b4a549ed", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:41:55.737083Z", "iopub.status.busy": "2024-11-01T20:41:55.736703Z", "iopub.status.idle": "2024-11-01T20:41:56.039696Z", "shell.execute_reply": "2024-11-01T20:41:56.039406Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val breaks = listOf(2.5, 5.0, 10.0)\n", "\n", "letsPlot() +\n", " geomBand(data = presidentialDf.toMap(), size = 0, alpha = 0.4) { xmin = \"start\"; xmax = \"end\"; fill = \"party\" } +\n", " geomLabel(data = presidentialDf.toMap(), y = 4_000, alpha = 0.6, angle = 90, vjust = 1, showLegend = false)\n", " { x = \"start\"; label = \"name\"; color = \"party\" } +\n", " geomLine(data = economicsDf.toMap()) { x = \"date\"; y = \"unemploy\" } +\n", " scaleXDateTime(name = \"Year\") +\n", " scaleYContinuous(\"Unemployment (x\\\\(10^3\\\\))\", trans = \"log10\",\n", " breaks = breaks.map { it * 10.0.pow(3) },\n", " labels = breaks.map { it.toString() }) +\n", " scaleFillDiscrete(name = \"\") +\n", " coordCartesian(xlim = Pair(millisByYear(1966), millisByYear(2016))) +\n", " guides(fill = guideLegend(alpha = 0.4, color = \"paper\")) +\n", " ggsize(1000, 400) +\n", " ggtitle(\"Unemployment in the US\", \"1967 to 2015\") +\n", " theme(plotTitle = elementText(size = 18, face = \"bold\")).legendPositionTop()" ] } ], "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 }