U1A3 distribución de frecuencia de casos confirmados, decesos y recuperados

de COVID en España usando los datos de la universidad Johns Hopkins

Importar paquetes/librerías

library(readr) #leer archivos
library(plotly) #gráficos interactivos
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse) #paquete de ciencia de datos
## -- Attaching packages ------------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.1     v stringr 1.4.0
## v purrr   0.3.4     v forcats 0.5.0
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gganimate) #animar graficas.
library(gifski) #hacer gifs

Importar

leer las url del repositorio

url_conf <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"

url_decesos <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"

url_recuperados <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv"
datos_conf <- read.csv(url_conf)
datos_decesos <-read.csv(url_decesos)
datos_recuperados <- read.csv(url_recuperados)
conf_esp <- t(datos_conf [datos_conf$Country.Region=="Spain" ,])
dec_esp <- t(datos_decesos [datos_decesos$Country.Region=="Spain" ,])
rec_esp <- t(datos_recuperados [datos_recuperados$Country.Region=="Spain" ,])

#Ordenar y transformar Primeras gráficas para reconocimieto

plot(conf_esp)
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs introducidos por coerción

plot(dec_esp)
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs introducidos por coerción

plot(rec_esp)
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs introducidos por coerción

#Transformación de datos

Fecha <- seq(from = as.Date("2020-01-22"), to = as.Date("2020-09-03"), by = "day")

#datos transformados a vector númerico
#confirmados

vec1 <- as.vector(conf_esp) #transformación de lista a vector

vec2 <- vec1[5:230] #filtrado de los primeros 

num1 <- as.numeric(vec2) #transformacion a datos numericos

confirmados <- as.vector(num1) #vector numerico

##Recuperados

vec1 <- as.vector(rec_esp) #transformación de lista a vector
vec2 <- vec1[5:230] #filtrado de los primeros 
num1 <- as.numeric(vec2) #transformacion a datos numericos
recuperados <- as.vector(num1) #vector numerico

#Decesos
vec1 <- as.vector(dec_esp) #transformación de lista a vector

vec2 <- vec1[5:230] #filtrado de los primeros 

num1 <- as.numeric(vec2) #transformacion a datos numericos

decesos <- as.vector(num1) #vector numerico

#Generación de un data frame
datos1 <- data.frame(Fecha, confirmados, decesos, recuperados)

#Figura 2 Confirmados ggplot
ggplot(data = datos1) +
  geom_line(mapping = aes(x = Fecha, y = confirmados))

#Figura 3 Recuperados ggplot
ggplot(data = datos1) +
  geom_line(mapping = aes(x = Fecha, y = recuperados))

#Figura 4 Decesos ggplot
ggplot(data = datos1) +
  geom_line(mapping = aes(x = Fecha, y = decesos))

#Animación 1 simple
ggplot(data = datos1) +
  geom_line(mapping = aes(x = Fecha, y = confirmados)) +
  ggtitle("Casos acumulados confirmados de COVID-19 en España") +
  transition_reveal(Fecha)

#Figura 5 multi ejes ggplot
ggplot(data=datos1) +
  geom_line(aes(x=Fecha,y=confirmados),color='red') + 
  geom_line(aes(x=Fecha,y=decesos),color='blue') + 
  geom_line(aes(x=Fecha,y=recuperados),color='green')+
  ylab('COVID-19 España') + xlab('Fecha')

#segunda animación
ggplot(data=datos1) +
  geom_line(aes(x=Fecha, y=confirmados), colour="red") +
  geom_line(aes(x=Fecha, y=decesos), colour="blue") +
  geom_line(aes(x=Fecha, y=recuperados), colour="green") +
  ylab("COVID-19 en España") + xlab("Fecha")

  transition_reveal(Fecha)
## <ggproto object: Class TransitionReveal, Transition, gg>
##     adjust_nframes: function
##     expand_data: function
##     expand_layer: function
##     expand_panel: function
##     finish_data: function
##     get_all_row_vars: function
##     get_frame_data: function
##     get_frame_vars: function
##     get_row_vars: function
##     map_data: function
##     mapping: (.+)
##     params: list
##     remap_frames: function
##     require_late_tween: function
##     setup_params: function
##     setup_params2: function
##     static_layers: function
##     unmap_frames: function
##     var_names: along
##     super:  <ggproto object: Class TransitionReveal, Transition, gg>
#Grafica interactiva compuesta
gcov <-ggplot(data=datos1) +
  geom_line(aes(x=Fecha,y=confirmados),colour='red') + 
  geom_line(aes(x=Fecha,y=decesos),colour='blue') + 
  geom_line(aes(x=Fecha,y=recuperados),colour="green")+
  ylab('COVID-19 España') + xlab('Fecha') +
  transition_reveal(Fecha)  
  
ggplotly(gcov)