"Figure 1.5R"

# Data Science Journal Figures # Professor D.F.X. Mathaisel # source: https://guangchuangyu.github.io/nCov2019/ # Retrieving the COVID Dataset install.packages(‘remotes’, dependencies = TRUE) library(‘remotes’) remotes::install_github(“GuangchuangYu/nCov2019”, dependencies = TRUE, force = TRUE) library(‘nCov2019’) x <- load_nCov2019(lang = ‘en’) str(x) ################################################### # Figure 1 remove.packages(“DataExplorer”) install.packages(‘Rcpp’, dependencies = TRUE) install.packages(‘DataExplorer’, dependencies = TRUE) library(DataExplorer) plot_str(x) ################################################### # Figure 2 # Number of deaths, confirms, and cures in China library(‘tidyr’) install.packages(“ggrepel”) library(‘ggrepel’) library(‘ggplot2’) y <- load_nCov2019(lang = ‘en’) d <- subset(y[‘global’], country == ‘China’) d <- gather(d, curve, count, -time, -country) ggplot(d, aes(time, count, color = curve)) + geom_point() + geom_line() + 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(“2021-03-20″))) + labs(title=”Number of Deaths, Confirmed Cases, and Cured Cases in China”) ################################################### # Figure 3 # Outbreak Trend Curves of the Top 10 Countries Around the World, except China library(‘ggrepel’) library(‘ggplot2’) y <- load_nCov2019(lang = ‘en’) df <- y[‘global’] d <- subset(df,country != ‘China’ & time == time(y)) t10 <- d[order(d$cum_confirm,decreasing = T),]$country[1:10] df <- df[which(df$country %in% t10),] ggplot(df, aes(time, as.numeric(cum_confirm), group = country, color = country)) + geom_point() + geom_line() + geom_label_repel(aes(label = country), data = df[df$time == time(y), ], hjust = 1) + theme_bw() + theme(legend.position = ‘none’) + xlab(NULL) + ylab(NULL) + scale_x_date(date_labels = “%Y-%m-%d”, limits = c(as.Date(“2020-02-01”), as.Date(“2021-03-19”))) + theme(axis.text.x = element_text(angle = 15, hjust = 1)) + labs(title = “Outbreak Trend Curves of Top 10 Countries Around the World \n (except China)”) ###################################################### # Figure 4 (extra) Total Number of Cases all countries except China require(dplyr) require(ggplot2) install.packages(“shadowtext”) require(shadowtext) require(nCov2019) d <- load_nCov2019() dd <- d[‘global’] %>% as_tibble %>% rename(confirm=cum_confirm) %>% filter(confirm > 100 & country != “China”) %>% group_by(country) %>% mutate(days_since_100 = as.numeric(time – min(time))) %>% ungroup breaks=c(100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000) p <- ggplot(dd, aes(days_since_100, confirm, color = country)) + geom_smooth(method=’lm’, aes(group=1), data = . %>% filter(!country %in% c(“Japan”, “Singapore”)), color=’grey10′, linetype=’dashed’) + geom_line(size = 0.8) + geom_point(pch = 21, size = 1) + scale_y_log10(expand = expansion(add = c(0,0.1)), breaks = breaks, labels = breaks) + scale_x_continuous(expand = expansion(add = c(0,1))) + theme_minimal(base_size = 14) + theme( panel.grid.minor = element_blank(), legend.position = “none”, plot.margin = margin(3,15,3,3,”mm”) ) + coord_cartesian(clip = “off”) + geom_shadowtext(aes(label = paste0(” “,country)), hjust=0, vjust = 0, data = . %>% group_by(country) %>% top_n(1, days_since_100), bg.color = “white”) + labs(x = “Number of days since 100th case”, y = “”, subtitle = “Total Number of Cases all Countries except China”) print(p) ###################################################### # Figure 4 # Windrose require(nCov2019) y <- load_nCov2019(lang = ‘en’, source=’github’) d = y[‘global’] str(d) require(dplyr) dd <- filter(d, time == time(y)) %>% arrange(desc(cum_confirm)) str(dd) dd = dd[1:30, ] dd$country = factor(dd$country, levels=dd$country) str(dd) dd$angle = 1:30 * 360/30 require(ggplot2) p <- ggplot(dd, aes(country, cum_confirm, fill=cum_confirm)) + geom_col(width=1, color=’grey90′) + geom_col(aes(y=I(5)), width=1, fill=’grey90′, alpha = .2) + geom_col(aes(y=I(3)), width=1, fill=’grey90′, alpha = .2) + geom_col(aes(y=I(2)), width=1, fill = “white”) + scale_y_log10() + scale_fill_gradientn(colors=c(“darkgreen”, “green”, “orange”, “firebrick”,”red”), trans=”log”) + geom_text(aes(label=paste(country, cum_confirm, sep=”\n”), y = cum_confirm *.5), data=function(d) d[d$cum_confirm > 30000,], size=2, color = “white”, fontface=”bold”, vjust=1) + geom_text(aes(label=paste0(country,” “, cum_confirm), y = max(cum_confirm) * 5, angle=angle+90), data=function(d) d[d$cum_confirm < 30000,], size=2, vjust=0) + coord_polar(direction=-1) + theme_void() + theme(legend.position=”none”) + ggtitle(“COVID-19 Global Confirmed Cases as of:”, time(y)) print(p) dd$cum_confirm ###################################################### # Figure 5 # Geographic Map of Confirmed Cases library(‘maps’) x = load_nCov2019(lang = ‘en’) plot(x) # Dashboard install.packages(“forecast”) library(forecast) dashboard(lang = ‘en’, remote = FALSE)