From fab92b413d5e845e97261b3680489da09403a02f Mon Sep 17 00:00:00 2001 From: Christine Choirat <cchoirat@gmail.com> Date: Wed, 25 Mar 2020 15:14:10 +0000 Subject: [PATCH] Added Chile plot example --- install.R | 4 + notebooks/examples-R/covid19-chile.ipynb | 109 +++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 notebooks/examples-R/covid19-chile.ipynb diff --git a/install.R b/install.R index db741f9..e3e3530 100644 --- a/install.R +++ b/install.R @@ -3,3 +3,7 @@ install.packages("knitr") install.packages("tidyverse") install.packages("data.table") + +install.packages("janitor") + +install.packages("rtweet") diff --git a/notebooks/examples-R/covid19-chile.ipynb b/notebooks/examples-R/covid19-chile.ipynb new file mode 100644 index 0000000..1071d65 --- /dev/null +++ b/notebooks/examples-R/covid19-chile.ipynb @@ -0,0 +1,109 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Code slightly adapted from https://github.com/itoledor/coronavirus/blob/master/code/covid19-chile_03_plot-data.R" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "suppressPackageStartupMessages(library(tidyverse))\n", + "suppressPackageStartupMessages(library(rvest))\n", + "suppressPackageStartupMessages(library(magrittr))\n", + "suppressPackageStartupMessages(library(janitor))\n", + "suppressPackageStartupMessages(library(RColorBrewer))\n", + "suppressPackageStartupMessages(library(rtweet))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "options(repr.plot.width = 12, repr.plot.height = 8)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "paleta <- c(brewer.pal(n = 8, 'YlOrRd') %>% rev, brewer.pal(n = 9, 'YlGnBu') %>% tail(-1)) %>% unique()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "covid19_chile <- read_rds(\"../../data/covid-19-chile/covid19_chile.rds\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "covid19_chile_gg_1 <- covid19_chile %>% filter(region == 'Total')\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %>% pivot_longer(starts_with('casos'), names_to = 'casos', names_prefix = 'casos_', values_to = 'Total')\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %>% filter(casos %in% c('totales', 'recuperados'))\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %>% mutate(casos = casos %>% str_replace('totales','confirmados'))\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %>% ggplot(aes(x = fecha, y = Total, color = casos))\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% geom_point(size = 4)\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% geom_line()\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% labs(title = \"COVID19 Casos confirmados en Chile\")\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% labs(y = \"Total casos confirmados\")\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% labs(caption\t= \"Fuente: https://www.minsal.cl/\\n @NachoToledoR\")\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% theme_minimal()\n", + "covid19_chile_gg_1 <- covid19_chile_gg_1 %+% theme(axis.text.x = element_text(angle = 45, hjust = 1))\n", + "covid19_chile_gg_1 " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "covid19_chile_gg_2 <- covid19_chile %>% filter(region != 'Total')\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %>% ggplot(aes(x = fecha, y = casos_totales, fill = region %>% fct_inorder))\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% geom_col(position = \"stack\")\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% labs(title = \"COVID19 Casos confirmados en Chile\", subtitle = \"por Región\", fill = \"Región\")\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% labs(y = \"Total casos confirmados\")\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% labs(caption\t= \"Fuente: https://www.minsal.cl/\\n @NachoToledoR\")\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% theme_minimal()\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% theme_minimal()\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% scale_fill_manual(values = paleta)\n", + "covid19_chile_gg_2 <- covid19_chile_gg_2 %+% theme(axis.text.x = element_text(angle = 45, hjust = 1))\n", + "covid19_chile_gg_2 " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "R", + "language": "R", + "name": "ir" + }, + "language_info": { + "codemirror_mode": "r", + "file_extension": ".r", + "mimetype": "text/x-r-source", + "name": "R", + "pygments_lexer": "r", + "version": "3.6.1" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab