Skip to content

Instantly share code, notes, and snippets.

@RamiKrispin
Created February 23, 2020 21:17
Show Gist options
  • Save RamiKrispin/01ac34404b572cd13f29bee498439654 to your computer and use it in GitHub Desktop.
Save RamiKrispin/01ac34404b572cd13f29bee498439654 to your computer and use it in GitHub Desktop.
Coronavirus daily new cases dist. China vs. rest of the world plot
#----------------------------------------
# Plotting the daily new confirmed cases
# China vs. rest of the world
#----------------------------------------
# Data: coronavirus dataset from the coronavirus package
# Using github version as it update the data on daily bases
#----------------------------------------
# Installing most recent version from Github
# must reset the R session after installation
# install.packages("devtools")
devtools::install_github("RamiKrispin/coronavirus")
#----------------------------------------
# Loading the package and dataset
library(coronavirus)
data("coronavirus")
#----------------------------------------
# Required packages
# Data manipulation - dplyr and tidyr
# Data viz - plotly
#----------------------------------------
# Manipulate the data to daily confirmed cases China vs. rest of the world
daily_confirmed <- coronavirus %>%
dplyr::mutate(country = dplyr::if_else(Country.Region == "Mainland China",
"China",
"Rest of the World")) %>%
dplyr::group_by(date, country) %>%
dplyr::summarise(total = sum(cases)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)
#----------------------------------------
# Plotting the data
daily_confirmed %>%
plotly::plot_ly(x = ~date,
y = ~ China,
name = 'China',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
groupnorm = 'percent',
fillcolor = 'red') %>%
plotly::add_trace(y = ~ `Rest of the World`, name = 'Rest of the World', fillcolor = 'yellow') %>%
plotly::layout(title = "Coronavirus Daily Confirmed Cases Dist - China vs. Rest of the World",
yaxis = list(title = "Proportion of New Cases", ticksuffix = '%'),
xaxis = list(title = "Date"),
legend = list(orientation = 'h'),
paper_bgcolor = "black",
plot_bgcolor = "black",
font = list(color = 'white'),
margin = list(
l = 60,
r = 40,
b = 50,
t = 50,
pad = 4
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment