Advertisement
Guest User

Untitled

a guest
May 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. library(tidyverse)
  2. library(ggthemes)
  3. library(animation)
  4. library(cowplot)
  5. library(ggpub)
  6. library(readxl)
  7. library(geonames)
  8.  
  9.  
  10. gif_file <- "~/Dropbox/infographics/general/south_america_infant_mortality.gif"
  11. x_space <- 0.16
  12. speed <- 0.12
  13. start_year <- 1985
  14.  
  15. europe <- c("Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador", "Guyana", "Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela")
  16.  
  17. europe <- sort(europe, decreasing = TRUE)
  18.  
  19. mortality <- read_csv("~/Downloads/child-mortality.csv") %>%
  20. filter(Entity %in% europe)
  21. mortality %>%
  22. select(Entity) %>%
  23. distinct()
  24.  
  25.  
  26. tweet_list <- list()
  27. track_tweets <- 1
  28.  
  29. max(mortality$Year)
  30. names(mortality)[4] <- "Mortality"
  31.  
  32. max_value <- max(mortality$Mortality)
  33.  
  34. breaks = seq(0,60, 10)
  35. labels <- str_c(breaks, "%")
  36. setwd("~/empty/")
  37. file.remove(dir("~/empty/"))
  38. tracker <- 1
  39.  
  40. for (yy in 1900:2015){
  41. yy_mortality <- mortality %>%
  42. filter(Year == yy)
  43.  
  44.  
  45.  
  46.  
  47. gg <- yy_mortality %>%
  48. rename(Country = Entity) %>%
  49. mutate(Country = factor(Country, levels = europe)) %>%
  50. ggplot() +
  51. geom_bar(aes(Country, Mortality), fill = "skyblue", stat = "identity") +
  52. coord_flip(clip= "off") +
  53. theme_minimal(base_size = 6)+
  54. labs(x = NULL, y = NULL) +
  55. labs(caption = "Data source: Our World in Data", title = "The decline of infant mortality in South America")+
  56. labs(subtitle = "Proportion of infants dying before the age of 5")+
  57. geom_text(x = 13, y = max_value * 0.93, label = yy, size = 3.5) +
  58. theme(panel.grid.major.y = element_blank())+
  59. theme(panel.grid.minor.y = element_blank())+
  60. theme(panel.grid.minor.x = element_blank())+
  61. # geom_text(data = fuel_pc, aes(x = Fuel, y = fuel, label = Percentage), hjust = 0)+
  62. # geom_text(data = yy_rank, aes(x = Rank, y = -0.01 * max_value, label = Fuel), hjust = 1) +
  63. scale_y_continuous(limits = c(0, max_value), labels = labels, breaks = breaks, expand = c(0,0)) +
  64. theme(plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm")) +
  65. labs(fill = NULL) +
  66. theme(legend.position = "none")+
  67. theme(plot.title = element_text(face = "bold"))+
  68. theme(plot.margin=grid::unit(c(1,1,1,1), "mm"))
  69. gg
  70. gg_file <- str_c(str_pad(tracker, width = 3, pad = 0), ".png")
  71.  
  72. ggsave(gg_file, gg, width = 8, height = 8 * 0.8, units = "cm")
  73. tracker <- tracker + 1
  74. }
  75.  
  76. for (i in 1:10) {
  77. tracker <- tracker + 1
  78. gg_file <- str_c(str_pad(tracker, width = 3, pad = 0), ".png")
  79.  
  80. ggsave(gg_file, gg, width = 8, height = 8 * 0.8, units = "cm")
  81. }
  82.  
  83.  
  84. # system(str_glue("convert -resize 80% -delay {100 * speed} -loop 0 *.png animation.gif"))
  85. system(str_glue("convert -delay {100 * speed} -loop 0 *.png animation.gif"))
  86.  
  87.  
  88. file.rename("animation.gif", gif_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement