Last active
May 1, 2020 11:30
-
-
Save ericpgreen/123599c21fae4a2a653ae3b7795236d0 to your computer and use it in GitHub Desktop.
animate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| library(countrycode) # install.packages("countrycode") | |
| # get the data | |
| cases_wide <- read.csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv", stringsAsFactors = FALSE) | |
| # pivot longer | |
| cases <- | |
| cases_wide %>% | |
| # sum subnational data to get national totals | |
| select(-Lat, -Long) %>% # drop these columns | |
| group_by(Country.Region) %>% # group by country | |
| mutate_at(vars(starts_with("X")), # sum each daily column | |
| funs(sum)) %>% # by county | |
| ungroup() %>% | |
| distinct(Country.Region, # countries with subnational | |
| .keep_all = TRUE) %>% # have repetitive data now | |
| select(-Province.State) %>% # so drop duplicates | |
| # pivot wide to long | |
| pivot_longer(cols = starts_with("X"), # take the daily cases | |
| names_to = "date", # put col headings in date | |
| names_prefix = "X", # pattern is X variables | |
| values_to = "cases") %>% # put case values in case | |
| mutate(date = lubridate::mdy(date)) %>% # create proper date var | |
| rename("country"="Country.Region") %>% # give nicer var name | |
| filter(country!="Diamond Princess") # drop diamond princess | |
| # filter to top 10 per day | |
| leaderboard <- | |
| cases %>% | |
| group_by(date) %>% | |
| mutate(rank = rank(-cases), | |
| relative_to_1 = cases/cases[rank == 1], | |
| label = paste0(" ", cases)) %>% | |
| # lots of countries tied for zero at the start | |
| # so order by rank and then alphabetical by country | |
| arrange(rank, country) %>% | |
| # give new ranks to break ties with alphabetical country names | |
| mutate(rank = 1:n()) %>% | |
| # limit to top 10 per day | |
| filter(rank <= 10) %>% | |
| ungroup() %>% | |
| # give a day label | |
| mutate(month = lubridate::month(date, label=TRUE, abbr=TRUE), | |
| day = lubridate::day(date), | |
| monthDay = paste(month, day, sep=" ")) %>% | |
| # convert country name to code | |
| mutate(cc = countrycode(country, origin="country.name", | |
| destination = "genc2c"), | |
| cc = tolower(cc)) | |
| # get vector of unique monthDay | |
| monthDayLevels <- | |
| leaderboard %>% | |
| distinct(date, .keep_all = TRUE) %>% | |
| pull(monthDay) | |
| df <- | |
| leaderboard %>% | |
| # convert to factor | |
| mutate(monthDay = factor(monthDay, | |
| levels = monthDayLevels, | |
| labels = monthDayLevels)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment