Skip to content

Instantly share code, notes, and snippets.

@simon-anders
Created March 11, 2020 13:40
Show Gist options
  • Save simon-anders/a62ade566e977074f6869aacf7d5b25e to your computer and use it in GitHub Desktop.
Save simon-anders/a62ade566e977074f6869aacf7d5b25e to your computer and use it in GitHub Desktop.
library( tidyverse )
library( ggplot2 )
jhu_url <- paste("https://raw.githubusercontent.com/CSSEGISandData/",
"COVID-19/master/csse_covid_19_data/", "csse_covid_19_time_series/",
"time_series_19-covid-Confirmed.csv", sep = "")
seq( 0, by=.211, length.out=300 ) -> a
hsv( a - floor(a), 1, 1 ) -> palette
tibble(
startdate = seq( as.Date("2020-02-01"), as.Date(Sys.time())+7, by=7 ),
startcount = 10 ) %>%
mutate( enddate = max(a$date) ) %>%
mutate( endcount = ( 10 ^ (as.integer(enddate-startdate)/7) ) * startcount ) %>%
mutate( idx = row_number() ) -> aa
bind_rows(
aa %>% select( idx, date=startdate, count=startcount ),
aa %>% select( idx, date=enddate, count=endcount ) ) -> slopelines
read_csv(jhu_url) %>%
select( -`Province/State`, -Lat, -Long ) %>%
gather( date, count, -`Country/Region` ) %>%
rename( region = `Country/Region` ) %>%
mutate_at( "date", as.Date, "%m/%d/%y" ) %>%
group_by( region, date ) %>%
summarise_at( "count", sum ) %>%
filter( count>0, max(count) > 100 ) %>%
ungroup -> a
a %>% filter( date==max(date) ) %>% arrange(count) -> b
(a %>% mutate_at( "region", factor, levels=rev(b$region) ) %>%
ggplot( aes( x=date, y=count ) ) +
geom_line( aes( col=region ) ) +
scale_y_log10( limits=c(.1,1e7) ) +
scale_color_manual( values=palette ) +
ylab( "number of cases" ) +
ggtitle( "COVID-19 (visualization of statistics collected by JHU CSSE)" ) ) +
geom_line( aes( group=idx ), col="gray", data = slopelines ) +
xlim( min(a$date), max(a$date)+7 ) #%>%
plotly::ggplotly() #%>%
htmlwidgets::saveWidget( "covid.html", selfcontained = FALSE, title="COVID19 cases" )
a %>% group_by( region ) %>%
do( broom::tidy( coef( glm( count ~ date, data = ., family=poisson() ) ) ) ) %>%
spread( names, x ) -> b
a %>%
left_join( b, by="region" ) %>%
mutate( date_shifted = date.x + `(Intercept)` ) %>%
ggplot() +
geom_line( aes( x=date_shifted, y=count, col=region ) ) +
scale_y_log10() +
scale_color_manual( values=palette )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment