library(tidyverse)
library(COVID19)
library(COVID19)
wb <- c("gdp" = "NY.GDP.MKTP.CD", "hosp_beds" = "SH.MED.BEDS.ZS", 
        "Over65" = "SP.POP.65UP.TO.ZS", 
        "deaths_cd" = "SH.DTH.COMM.ZS",
        "mortality" ="SP.DYN.AMRT.MA",
        "Infant_deaths"="SH.DTH.IMRT",
        "Under_5_deaths" = "SH.DTH.MORT",
        "Suicide" = "SH.STA.SUIC.P5"
        )
gmr <- "https://www.gstatic.com/covid19/mobility/Global_Mobility_Report.csv"
d  <- covid19(wb = wb)
## We have invested a lot of time and effort in creating COVID-19 Data Hub, please cite the following when using it:
## 
##   Guidotti, E., Ardia, D., (2020), "COVID-19 Data Hub", Journal of
##   Open Source Software 5(51):2376, doi: 10.21105/joss.02376.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {COVID-19 Data Hub},
##     year = {2020},
##     doi = {10.21105/joss.02376},
##     author = {Emanuele Guidotti and David Ardia},
##     journal = {Journal of Open Source Software},
##     volume = {5},
##     number = {51},
##     pages = {2376},
##   }
## 
## To retrieve citation and metadata of the data sources see ?covid19cite. To hide this message use 'verbose = FALSE'.
d %>% filter(date< Sys.Date()-2) -> d
d %>% group_by(iso_alpha_3) -> d
d %>%arrange(date) %>% mutate(New_cases = confirmed - lag(confirmed, default = first(confirmed))) -> d
d %>% mutate(New_deaths = deaths - lag(deaths, default = first(deaths))) %>% arrange(deaths) -> d
d %>% mutate(New_tests = tests - lag(tests, default = first(tests))) %>% arrange(tests) -> d
d %>% group_by(date) %>% summarise(daily_cases=sum(New_cases,na.rm=TRUE), daily_deaths=sum(New_deaths,na.rm=TRUE)) ->dd
ggplot(dd, aes(x=date)) +
  
  geom_line( aes(y=daily_cases), size=0.7, color="red") +
  geom_line( aes(y=daily_deaths*10), size=0.7, color="darkgreen") +
  geom_smooth(aes(y=daily_cases), size=0.7, color="red",method = "gam", formula = y ~ s(x), se=FALSE) +  geom_smooth(aes(y=daily_deaths*10), size=0.7, color="darkgreen",method = "gam", formula = y ~ s(x), se=FALSE) +
  scale_y_continuous(
    
    # Features of the first axis
    name = "Daily cases",
    
    # Add a second axis and specify its features
    sec.axis = sec_axis(sec.axis ~./10 , name="Daily deaths")
  )  +

  theme(
    axis.title.y = element_text(color = "red", size=15),
    axis.title.y.right = element_text(color = "darkgreen", size=15)
  ) +

  ggtitle("Global aggregated reported deaths and reported cases")

DT::datatable(dd, 
                              filter = "top",                         
                              extensions = c('Buttons'), options = list(
                                dom = 'Blfrtip',
                                buttons = c('copy', 'csv', 'excel'), colReorder = TRUE
                              ))