Dataset

We are using dataset from John Hopkins CSSE for global desease. The github repository with the raw data can be found here

The graph bellow shows Brazil and Italy death count

extract_ts <- function( country, include_zero = T ) {
  df <- time_series_covid19_deaths_global %>%
  filter( `Country/Region` == country)

  df <- map_if(df, is.numeric, sum) %>% as_tibble()
  tmp <- as_tibble(cbind(days = names(df), t(df)))
  tmp <- tmp[ -c(1:4), ] # remove header
  class(tmp[[2]]) <- "double" # convert to double
  # rename death count
  tmp <- tmp %>%
    rename( deaths = V2) 
  # create days for time series
  tmp <- tmp %>%
    mutate( days = parse_date(days, format = "%m/%d/%y"))
  
  # create the time series
  ts <- tmp %>%
    as_tsibble( index = days )
  
  if ( include_zero )
    return <- ts
  else
    return <- ts %>% filter( deaths > 0 )
}

do_plots <- function(countries, plotly = F ) {
  dfs <- map(countries, extract_ts)

  if ( ! plotly ) {
    plot_me <- function(df) {
      df %>% 
        ggplot( aes( x = days, y = deaths)) +
        geom_line()
      }
  } else {
    plot_me <- function(df) {
      df %>% 
        plot_ly( x = ~days, y = ~deaths, mode = "lines")
      }
  }

  plots <- map(dfs, plot_me)
}

compare_countries <- function ( countries_list, plotly = F ) {
  plots <- do_plots(countries_list, plotly)

  # add label to plot
  for( i in seq_along(plots)) {
    if( plotly )
      plots[[i]] <- plots[[i]] %>% layout( title = countries_list[[i]] )
    else
      plots[[i]] <- plots[[i]] + labs(title = countries_list[[i]])
  }

  # arrange in N-columns the plots
  if(plotly) {
    # put labels each line
    for( i in seq_along(plots)) {
      plots[[i]] <- plots[[i]] %>% add_lines( name = countries_list[[i]] )
    }
    do.call("subplot", plots)
  }
  else { 
    args_list = c(plots, ncol = length(countries_list))
    do.call("grid.arrange", args_list)  
  }
}

# make plots for brazil and italy
compare_countries(c("Brazil", "Italy"))
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Comparing US and China

compare_countries(c("US", "China"))

Comparing Brazil and US

compare_countries(c("Brazil","US"), plotly = T)
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.