|
#---------------- Plotting Daily Cumulative Cases of the Coronavirus---------------- |
|
# Installing the most update version of the coronavirus |
|
# install.packages("devtools") |
|
devtools::install_github("RamiKrispin/coronavirus") |
|
data("coronavirus") |
|
|
|
|
|
# Reformat and aggregate the data to daily by country and type of case |
|
df_daily <- coronavirus %>% |
|
dplyr::group_by(date, type) %>% |
|
dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>% |
|
tidyr::pivot_wider(names_from = type, |
|
values_from = total) %>% |
|
dplyr::arrange(date) %>% |
|
dplyr::ungroup() %>% |
|
dplyr::mutate(active = confirmed - death - recovered) %>% |
|
dplyr::mutate(confirmed_cum = cumsum(confirmed), |
|
death_cum = cumsum(death), |
|
recovered_cum = cumsum(recovered), |
|
active_cum = cumsum(active)) |
|
|
|
# plotting the data |
|
|
|
plotly::plot_ly(data = df_daily) %>% |
|
plotly::add_trace(x = ~ date, |
|
y = ~ active_cum, |
|
type = "scatter", |
|
mode = "lines+markers", |
|
name = "Active", |
|
line = list(color = "#1f77b4")) %>% |
|
plotly::add_trace(x = ~ date, |
|
y = ~ recovered_cum, |
|
type = "scatter", |
|
mode = "lines+markers", |
|
name = "Recovered", |
|
line = list(color = "green"), |
|
marker = list(color = "green")) %>% |
|
plotly::add_trace(x = ~ date, |
|
y = ~ death_cum, |
|
type = "scatter", |
|
mode = 'lines+markers', |
|
name = "Death", |
|
line = list(color = "red"), |
|
marker = list(color = "red")) %>% |
|
plotly::add_annotations(x = as.Date("2020-03-01"), |
|
y = 42716, |
|
text = paste("# of recovered cases surpass", |
|
"<br>", |
|
"the number of active cases"), |
|
xref = "x", |
|
yref = "y", |
|
arrowhead = 5, |
|
arrowhead = 3, |
|
arrowsize = 1, |
|
showarrow = TRUE, |
|
ax = -270, |
|
ay = 30) %>% |
|
plotly::layout(title = "", |
|
yaxis = list(title = "Cumulative Number of Cases"), |
|
xaxis = list(title = "Date"), |
|
legend = list(x = 0.1, y = 0.9), |
|
hovermode = "compare") |