animated_plot.R 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. library(readr)
  2. library(dplyr)
  3. # Preparing data ----------------------------------------------------------
  4. dataset <- read_delim('../data/preprocessed/DIB_dataset.tsv', delim='\t')
  5. # https://www.kaggle.com/statchaitya/country-to-continent
  6. country_to_continent <- read_delim('countryContinent.csv', delim=',')
  7. colnames(country_to_continent)[1] <- 'locality_name'
  8. # Fixing country names
  9. country_to_continent %>%
  10. mutate(locality_name =
  11. case_when(
  12. locality_name == 'United States of America' ~ 'United States',
  13. locality_name == 'Venezuela (Bolivarian Republic of)' ~ 'Venezuela',
  14. locality_name == 'Bolivia (Plurinational State of)' ~ 'Bolivia',
  15. locality_name == 'Cabo Verde' ~ 'Cape Verde',
  16. locality_name == 'Czech Republic' ~ 'Czechia',
  17. locality_name == 'United Kingdom of Great Britain and Northern Ireland' ~ 'United Kingdom',
  18. locality_name == 'Korea (Republic of)' ~ 'South Korea',
  19. locality_name == 'Bahamas' ~ 'The Bahamas',
  20. locality_name == 'Lao People\'s Democratic Republic' ~ 'Laos',
  21. locality_name == 'Moldova (Republic of)' ~ 'Moldova',
  22. locality_name == 'Macedonia (the former Yugoslav Republic of)' ~ 'North Macedonia',
  23. locality_name == 'Myanmar' ~ 'Myanmar (Burma)',
  24. locality_name == 'Taiwan, Province of China' ~ 'Taiwan',
  25. locality_name == 'Tanzania, United Republic of' ~ 'Tanzania',
  26. locality_name == 'Viet Nam' ~ 'Vietnam',
  27. # locality_name == '' ~ 'Réunion',
  28. # locality_name == '' ~ 'Côte d\'Ivoire',
  29. TRUE ~ locality_name)
  30. ) -> country_to_continent
  31. dataset <- left_join(dataset, country_to_continent, by='locality_name')
  32. dataset %>%
  33. mutate(continent =
  34. case_when(
  35. locality_name == 'Réunion' ~ 'Africa',
  36. locality_name == 'Côte d\'Ivoire' ~ 'Africa',
  37. TRUE ~ continent)
  38. ) -> dataset
  39. rm(country_to_continent)
  40. # Preparing animation -----------------------------------------------------
  41. library(ggplot2)
  42. library(gganimate)
  43. theme_set(theme_bw())
  44. p <- ggplot(
  45. dataset,
  46. aes(x = workplaces, y=residential, size = acc_cases, colour = continent)
  47. ) +
  48. geom_point(show.legend = TRUE, alpha = 0.7) +
  49. scale_color_viridis_d() +
  50. scale_y_continuous(labels = function(x) paste0(x, "%")) +
  51. scale_x_continuous(labels = function(x) paste0(x, "%")) +
  52. labs(x = "Variation in visit/stay at a workplace", y = "Variation in visit/Stay at a residence",
  53. color = 'Continent', size = 'Accumulated number of cases',
  54. title = "Impact of COVID-19 pandemic on mobility",
  55. caption = "Ribeiro-Dantas et al (2020) 'Dataset for country profile and mobility analysis in the assessment of COVID19\npandemic', Data in Brief. http://mribeirodantas.me") +
  56. theme(legend.position = "bottom", legend.box="vertical") +
  57. transition_states(date) +
  58. labs(subtitle = 'Every point is a country. Date: {closest_state}') +
  59. shadow_wake(wake_length = 0.1, alpha = FALSE)
  60. animate(p, fps = 7, nframes = 7*83, height=1000, width=1000, res = 70)
  61. anim_save('GIF_by_country.gif', animation = last_animation(), path = '.')
Tip!

Press p or to see the previous file or, n or to see the next file