Skip to content

Instantly share code, notes, and snippets.

@RamiKrispin
Created March 2, 2020 16:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RamiKrispin/46016a04eeea3191e254f4ebe161ecf4 to your computer and use it in GitHub Desktop.
Save RamiKrispin/46016a04eeea3191e254f4ebe161ecf4 to your computer and use it in GitHub Desktop.
Coronavirus daily cumulative cases by type
#---------------- 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment