library('remotes')
## Warning: package 'remotes' was built under R version 4.0.2
#remotes::install_github("GuangchuangYu/nCov2019", dependencies = TRUE)
library('nCov2019')

x <- get_nCov2019(lang = 'en')

x
## China (total confirmed cases): 92046
## last update: 2020-11-04 15:26:32
head(summary(x))
##   confirm suspect dead heal nowConfirm nowSevere importedCase deadRate healRate
## 1      41       0    1    0          0         0            0      2.4      0.0
## 2      41       0    1    0          0         0            0      2.4      0.0
## 3      41       0    2    5          0         0            0      4.9     12.2
## 4      45       0    2    8          0         0            0      4.4     17.8
## 5      62       0    2   12          0         0            0      3.2     19.4
## 6     198       0    3   17          0         0            0      1.5      8.6
##    date noInfect
## 1 01.13        0
## 2 01.14        0
## 3 01.15        0
## 4 01.16        0
## 5 01.17        0
## 6 01.18        0
head(x[])
##        name nowConfirm confirm suspect dead deadRate showRate heal healRate
## 1 Hong Kong        131    5345       0  105     1.96    FALSE 5109    95.58
## 2  Shanghai         92    1200       0    7     0.58    FALSE 1101    91.75
## 3  Xinjiang         64     966       0    3     0.31    FALSE  899    93.06
## 4    Taiwan         40     568       0    7     1.23    FALSE  521    91.73
## 5   Sichuan         39     757       0    3     0.40    FALSE  715    94.45
## 6 Guangdong         38    1938       0    8     0.41    FALSE 1892    97.63
##   showHeal
## 1     TRUE
## 2     TRUE
## 3     TRUE
## 4     TRUE
## 5     TRUE
## 6     TRUE
head(x['Taiwan', by = 'today'])
##           name confirm confirmCuts isUpdated
## 1 Location TBD       5           0      TRUE
library(ggplot2)

d <- summary(x)

ggplot(d) + 
  aes(x = date, y = confirm) +
  geom_col()

ggplot(d) + 
  aes(x = as.Date(date, "%m.%d"), y = as.numeric(confirm)) +
  geom_col(fill = 'firebrick') +
  theme_minimal(base_size = 14) +
  xlab(NULL) + 
  ylab(NULL) + 
  scale_x_date(date_labels = "%Y/%m/%d") +
  labs(caption = paste("accessed date:", time(x)))

y <- load_nCov2019(lang = 'en')
d <- y['Taiwan']


ggplot(d) + 
  aes(x = as.Date(time, "%m.%d"), y = as.numeric(cum_confirm)) +
  geom_col(fill = 'firebrick') +
  theme_minimal(base_size = 14) +
  xlab(NULL) + 
  ylab(NULL) + 
  scale_x_date(date_labels = "%Y/%m/%d") +
  labs(caption = paste("accessed date:", time(x)))

d <- y['Anhui']

ggplot(d) + 
  aes(x = as.Date(time, "%m.%d"), y = as.numeric(cum_confirm)) +
  geom_col(fill = 'firebrick') +
  theme_minimal(base_size = 14) +
  xlab(NULL) + 
  ylab(NULL) + 
  scale_x_date(date_labels = "%Y/%m/%d") +
  labs(caption = paste("accessed date:", time(x)))

library(ggplot2)
x <- get_nCov2019(lang = 'en')
d <- x['Anhui', ]
d <- d[order(d$confirm), ]

ggplot(d) + 
  aes(name, as.numeric(confirm)) + 
  geom_col() +
  geom_col(fill = 'firebrick') +
  theme_minimal(base_size = 14) +
  xlab(NULL) + 
  ylab(NULL) +
  labs(caption = paste("accessed date:", time(x))) +
  scale_x_discrete(limits = d$name) + 
  coord_flip()

library('nCov2019')
y <- load_nCov2019(lang = 'en')
#y

y_china <- aggregate(cum_confirm ~ + time, summary(y), sum)

library(ggplot2)
#y_china
ggplot(y_china) + 
  aes(x = time, y = cum_confirm) +
  geom_line()

y   <- load_nCov2019(lang = 'en', source = 'github')
dxy <- load_nCov2019(lang = 'en', source = 'dxy')
nhc <- load_nCov2019(lang = 'en', source = 'cnnhc')



dxy_china  <- aggregate(cum_confirm ~ + time, summary(dxy), sum)
y_china    <- aggregate(cum_confirm ~ + time, summary(y), sum)
nhc_china  <- aggregate(cum_confirm ~ + time, summary(nhc), sum)

dxy_china$source <- 'DXY data'
y_china$source <- 'GitHub data'
nhc_china$source <- 'NHC data'


df <- rbind(dxy_china, y_china, nhc_china)

#df

ggplot(subset(df, time >= '2020-01-11'),
    aes(time,cum_confirm, color = source)) +
    geom_line() + 
  scale_x_date(date_labels = "%Y-%m-%d") +
  ylab('Confirmed Cases in China') + 
  xlab('Time') + 
  theme_bw() +
  theme(axis.text.x = element_text(hjust = 1)) +
  theme(legend.position = 'bottom')

library('tidyr')
library('ggrepel')
## Warning: package 'ggrepel' was built under R version 4.0.2
library('nCov2019')
library('ggplot2')
y <- load_nCov2019(lang = 'en')
d <- subset(y['global'], country == 'China')
#d

d <- gather(d, curve, count, -time, -country)
#d

ggplot(d) + 
  aes(time, count, color = curve)+
  geom_line() +
  geom_point() +
  xlab(NULL) + 
  ylab(NULL) +
  theme_bw() + 
  theme(legend.position = "none") +
  geom_text_repel(aes(label = curve),data = d[d$time == time(y), ], hjust = 1) +
  theme(axis.text.x = element_text(angle = 15, hjust = 1)) + 
  scale_x_date(date_labels = "%Y-%m-%d",
        limits = c(as.Date("2020-01-15"), as.Date("2020-11-04"))) +
  labs(title="Number of deaths, confirms, and cures in China")
## Warning: Removed 135 row(s) containing missing values (geom_path).
## Warning: Removed 135 rows containing missing values (geom_point).

plotCountryCase <- function(coun){
  y <- load_nCov2019(lang = 'en')
  d <- subset(y['global'], country == coun)
  #d
  
  d <- gather(d, curve, count, -time, -country)
  #d
  
  fig <- ggplot(d) + 
    aes(time, count, color = curve)+
    geom_line() +
    geom_point() +
    xlab(NULL) + 
    ylab(NULL) +
    theme_bw() + 
    theme(legend.position = "none") +
    geom_text_repel(aes(label = curve),data = d[d$time == time(y), ], hjust = 1) +
    theme(axis.text.x = element_text(angle = 15, hjust = 1)) + 
    scale_x_date(date_labels = "%Y-%m-%d",
          limits = c(as.Date("2020-01-15"), as.Date("2020-11-04"))) +
    labs(title=paste("Number of deaths, confirms, and cures in ", coun) )
    fig
}


plotCountryCase('United States')

plotCountryCase('France')

y <- load_nCov2019(lang = 'en')
df <- y['global']
time(y)
## [1] "2020-11-02"
latest_df <- df[df$time == time(y),]
t10 <- latest_df[order(latest_df$cum_confirm, decreasing = TRUE),'country'][1:10]

t10
##  [1] "United States"  "India"          "Brazil"         "Russia"        
##  [5] "France"         "Spain"          "Argentina"      "Colombia"      
##  [9] "United Kingdom" "Mexico"
df <- df[df$country %in% t10,]

ggplot(df) + 
  aes(time, as.numeric(cum_confirm),group = country, color = country) +
  geom_line() + 
  geom_point() +
  theme_bw() + 
  theme(legend.position = "none") +
  geom_text_repel(aes(label = country),data = df[df$time == time(y), ], hjust = 1) +
  theme(axis.text.x = element_text(angle = 15, hjust = 1)) + 
  scale_x_date(date_labels = "%Y-%m-%d",
        limits = c(as.Date("2020-01-15"), as.Date("2020-11-02"))) 

ggplot(df) + 
  aes(time, as.numeric(cum_confirm),group = country, color = country) +
  geom_line() + 
  geom_point() +
  theme_bw() + 
  theme(legend.position = "none") +
  geom_label_repel(aes(label = country),data = df[df$time == time(y), ], hjust = 1) +
  theme(axis.text.x = element_text(angle = 15, hjust = 1)) + 
  scale_x_date(date_labels = "%Y-%m-%d",
        limits = c(as.Date("2020-01-15"), as.Date("2020-11-02"))) 

y <- load_nCov2019(lang = 'en')
df <- y['global']
time(y)
## [1] "2020-11-02"
latest_df <- df[df$time == time(y),]
latest_df$new_case <- c(0,diff(latest_df$cum_confirm))
t10 <- latest_df[order(latest_df$new_case, decreasing = TRUE),'country'][1:10]
t10
##  [1] "United States"  "India"          "Brazil"         "Russia"        
##  [5] "Spain"          "France"         "Argentina"      "United Kingdom"
##  [9] "Colombia"       "Mexico"
ggplot(df) + 
  aes(time, as.numeric(cum_confirm),group = country, color = country) +
  geom_line() + 
  geom_point() +
  theme_bw() + 
  theme(legend.position = "none") +
  geom_label_repel(aes(label = country),data = df[df$time == time(y), ], hjust = 1) +
  theme(axis.text.x = element_text(angle = 15, hjust = 1)) + 
  scale_x_date(date_labels = "%Y-%m-%d",
        limits = c(as.Date("2020-01-15"), as.Date("2020-11-02"))) 
## Warning: Removed 45 row(s) containing missing values (geom_path).
## Warning: Removed 45 rows containing missing values (geom_point).
## Warning: Removed 2 rows containing missing values (geom_label_repel).

d <- y['Anhui']
ggplot(d) +
  aes(time, as.numeric(cum_confirm), group = city, color = city) +
  geom_point() + 
  geom_line() +
  geom_label_repel(aes(label = city),
 data = d[d$time == time(y), ], hjust = 1) +
 theme_minimal(base_size = 14) + theme(legend.position = 'none') +
 scale_x_date(date_labels = "%Y-%m-%d") + xlab(NULL) + ylab(NULL) +
 theme(axis.text.x = element_text(hjust = 1)) +
 labs(title = "Growth curve of confirms in Anhui Province, China")
## Warning: Removed 1 rows containing missing values (geom_label_repel).

#y['Taiwan']
d <- y['global']
max_time <- max(d$time)
min_time <- max_time - 7

max_time
## [1] "2020-11-02"
min_time
## [1] "2020-10-26"
d <- na.omit(d[d$time >= min_time & d$time <= max_time,])
dd <- d[d$time == max(d$time, na.rm = TRUE),]
#dd

d$country <- factor(d$country,
  levels=unique(dd$country[order(dd$cum_confirm)]))

breaks <- c(10, 100, 1000, 10000)
  
#d$country
  
  
ggplot(d) +
  aes(time, country) +
  geom_tile(aes(fill = cum_confirm), color = 'black') +
  scale_fill_viridis_c(trans = 'log', breaks = breaks,  labels = breaks) +
  xlab(NULL) + 
  ylab(NULL) +
 scale_x_date(date_labels = "%Y-%m-%d") + 
 theme_minimal()

df <- y['global']
time(y)
## [1] "2020-11-02"
latest_df <- df[df$time == time(y),]
t10 <- latest_df[order(latest_df$cum_confirm, decreasing = TRUE),'country'][1:10]
t11 <- c('China',t10)

t11
##  [1] "China"          "United States"  "India"          "Brazil"        
##  [5] "Russia"         "France"         "Spain"          "Argentina"     
##  [9] "Colombia"       "United Kingdom" "Mexico"
d <- y['global']
max_time <- max(d$time)
min_time <- max_time - 300

max_time
## [1] "2020-11-02"
min_time
## [1] "2020-01-07"
d <- d[d$country %in% t11, ]
d <- na.omit(d[d$time >= min_time & d$time <= max_time,])
dd <- d[d$time == max(d$time, na.rm = TRUE),]
#dd

d$country <- factor(d$country,
  levels=unique(dd$country[order(dd$cum_confirm)]))

breaks <- c(10, 100, 1000, 10000)
  
#d$country
  
  
ggplot(d) +
  aes(time, country) +
  geom_tile(aes(fill = cum_confirm), color = 'black') +
  scale_fill_viridis_c(trans = 'log', breaks = breaks,  labels = breaks) +
  xlab(NULL) + 
  ylab(NULL) +
 scale_x_date(date_labels = "%Y-%m-%d") + 
 theme_minimal()

library('nCov2019')
y <- load_nCov2019(lang = 'en')
d <- y['global']
#d
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dd <- d %>%
  filter(time == time(y)) %>%
  arrange(desc(cum_confirm)) %>%
  slice(1:40)

#dd
dd$country <- factor(dd$country, levels=dd$country)
dd$angle = 1:40 * 360/40
library(ggplot2)
ggplot(dd) + 
  aes(country, cum_confirm, fill = cum_confirm) + 
  geom_col() +
  scale_y_log10() +
  coord_polar(direction=-1) 

ggplot(dd) + 
  aes(country, cum_confirm, fill = cum_confirm) + 
  geom_col() +
  scale_y_log10() +
  scale_fill_gradientn(colors=c("darkgreen", "green", "orange", "firebrick","red"), trans="log") +
  coord_polar(direction=-1) +
  ggtitle("COVID19 global trend", time(y))

dd <- d %>%
  filter(time == time(y)) %>%
  arrange(desc(cum_confirm)) %>%
  slice(1:20)
library(plotly)
## 
## 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
dd$country <- factor(dd$country, levels=dd$country)
#dd$angle = 1:20 * 360/20
fig <- ggplot(dd) + 
  aes(country, cum_confirm, fill = cum_confirm) + 
  geom_col() +
  scale_y_log10() +
  scale_fill_gradientn(colors=c("darkgreen", "green", "orange", "firebrick","red"), trans="log") +
  coord_polar(direction=-1) +
  ggtitle("COVID19 global trend", time(y))
#ggplotly(fig)
d <- y['global']
#d

dd <- d %>%
  filter(cum_confirm > 100) %>%
  group_by(country) %>%
  mutate(day_since_100 = as.numeric(time - min(time))) %>%
  ungroup
  


breaks<-c(100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000)

#install.packages('shadowtext')
require(shadowtext)
## Loading required package: shadowtext
## Warning: package 'shadowtext' was built under R version 4.0.2
ggplot(dd) +
  aes(day_since_100, cum_confirm, color = country) + 
  geom_smooth(method = 'lm', color='grey10', linetype = 'dashed') + 
  geom_line() +
  scale_y_log10(expand = expansion(add = c(0,0.1)),
            breaks = breaks, labels = breaks) +
  scale_x_continuous(expand = expansion(add = c(0,1))) +
  theme(panel.grid.minor = element_blank(), legend.position = "none", plot.margin = margin(3,15,3,3,"mm")) +
  geom_shadowtext(aes(label = paste0(" ",country)), hjust=0, vjust = 0, data = . %>% group_by(country) %>% top_n(1, day_since_100),bg.color = "white") 
## `geom_smooth()` using formula 'y ~ x'

#dd
latest_df <- dd[dd$time == time(y),]
#latest_df
latest_df
## # A tibble: 194 x 6
##    time       country                cum_confirm cum_heal cum_dead day_since_100
##    <date>     <chr>                        <int>    <int>    <int>         <dbl>
##  1 2020-11-02 Bhutan                         354      325        0            95
##  2 2020-11-02 China                        92015    86712     4746           289
##  3 2020-11-02 Central African Repub…        4866     1924       62           177
##  4 2020-11-02 Denmark                      47811    35465      723           237
##  5 2020-11-02 Ukraine                     402194   163768     7375           222
##  6 2020-11-02 Uzbekistan                   67254    64466      571           219
##  7 2020-11-02 Uganda                       12971     7556      114           178
##  8 2020-11-02 Uruguay                       3165     2658       60           226
##  9 2020-11-02 Chad                          1499     1329       98           183
## 10 2020-11-02 Yemen                         2063     1375      601           170
## # … with 184 more rows
top10 <- latest_df[order(latest_df$cum_confirm, decreasing = TRUE),'country']$country[1:10]

dd <- d %>%
  filter(cum_confirm > 100, country %in% top10) %>%
  group_by(country) %>%
  mutate(day_since_100 = as.numeric(time - min(time))) %>%
  ungroup

fig <- ggplot(dd) +
  aes(day_since_100, cum_confirm, color = country) + 
  geom_smooth(method = 'lm', color='grey10', linetype = 'dashed') + 
  geom_line() +
  scale_y_log10(expand = expansion(add = c(0,0.1)),
            breaks = breaks, labels = breaks) +
  scale_x_continuous(expand = expansion(add = c(0,1))) +
  theme(panel.grid.minor = element_blank(), legend.position = "none", plot.margin = margin(3,15,3,3,"mm")) +
  geom_shadowtext(aes(label = paste0(" ",country)), hjust=0, vjust = 0, data = . %>% group_by(country) %>% top_n(1, day_since_100),bg.color = "white") 


library(plotly)
ggplotly(fig)
## `geom_smooth()` using formula 'y ~ x'
## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues
## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

## Warning in geom2trace.default(dots[[1L]][[10L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomShadowText() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues