library(reshape2)
library(googlesheets4)

library(tidyverse)
library(countrycode)
library(ggplot2)
library(gganimate)


deathCases <- read_sheet("https://docs.google.com/spreadsheets/d/1lirxEXoIeCTucFdrYmzwjG-nja7R94xWBPt6sIAT01M/edit#gid=0",3)
## Using an auto-discovered, cached token.
## To suppress this message, modify your code or options to clearly consent to the use of a cached token.
## See gargle's "Non-interactive auth" vignette for more details:
## https://gargle.r-lib.org/articles/non-interactive-auth.html
## The googlesheets4 package is using a cached token for krishnakumarshrestha00@gmail.com.
deathCases<-deathCases %>%
              select(-c(Lat,Long))%>% 
                   melt(id=c('Country/Region','Province/State'))


deathCases<-deathCases%>%
              group_by(`Country/Region`,variable)%>%
                    summarise(Deaths=sum(value))

colnames(deathCases)<-c("Country","Date","Death")




data1<-deathCases

data1$Date<-as.Date(data1$Date,"%m/%d/%y")

data1$Continent <- countrycode(sourcevar = data1$Country,origin = "country.name",destination="continent")



by_day<-data1 %>% group_by(Date) %>% 
            arrange(Date, -Death)%>% 
               mutate(rank = 1:n())%>%
                  filter(rank <= 10)



theme <- ggthemes::theme_solarized() +
  theme(axis.text.y = element_blank()) +
  theme(axis.ticks.y = element_blank()) +
  theme(axis.line.y = element_blank()) +
  theme(legend.background = element_rect(fill = "gainsboro")) +
  theme(plot.background = element_rect(fill = "gainsboro")) +
  theme(panel.background = element_rect(fill = "gainsboro"))


library(ggplot2)
by_day %>%  
  ggplot() +  
  aes(xmin = 0 ,
      xmax = Death)+
  aes(ymin = rank - .45,
      ymax = rank + .45,
      y = rank)+
  facet_wrap(~ Date)+
  geom_rect(alpha = .7)+
  aes(fill = Continent)+
  scale_fill_viridis_d(option = "magma",
                       direction = -1)+
  scale_x_continuous(  
    limits = c(-3000, 18000),  
    breaks = seq(0,15000,1500)) +  
  geom_text(col = "gray13",
            hjust = "right",
            aes(label = Country),size=5,
            x = 0)+geom_text(aes(label=Death,x=Death,hjust=-1))+
  scale_y_reverse()+
  labs(fill = NULL) +  
  labs(x = 'Death') +  
  labs(y = "") + ggtitle("Death Due To Corona") +
  theme ->  
  my_plot



my_plot<-my_plot +  
  facet_null() +  
  scale_x_continuous(  
    limits = c(-1500, 15000),  
    breaks = seq(0,15000,1400)) +  
  geom_text(x = 7000 , y = -5,  
            family = "Times",  
            aes(label = as.character(Date)),  
            size =8, col = "grey18") +  
  aes(group = Country) +  
  gganimate::transition_time(Date)
my_plot