Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

161 linhas
4.6KB

  1. library(tidyexplain)
  2. library(here)
  3. library(stringr)
  4. set_font_size(title_size = 20)
  5. check_and_create <- function(ff) {
  6. if (!dir.exists(ff)) dir.create(ff, recursive = T)
  7. }
  8. check_and_create(here("images", "static", "png"))
  9. check_and_create(here("images", "static", "svg"))
  10. check_and_create(here("images", "gif"))
  11. ### Animate Joins
  12. x <- dplyr::data_frame(
  13. id = 1:3,
  14. x = paste0("x", 1:3)
  15. )
  16. y <- dplyr::data_frame(
  17. id = (1:4)[-3],
  18. y = paste0("y", (1:4)[-3])
  19. )
  20. joins <- c(
  21. full_join = animate_full_join,
  22. inner_join = animate_inner_join,
  23. left_join = animate_left_join,
  24. right_join = animate_right_join,
  25. semi_join = animate_semi_join
  26. )
  27. a <- sapply(1:length(joins), function(i) {
  28. nam <- names(joins)[i]
  29. nam <- str_replace(nam, "_", "-")
  30. cat(nam, "\n")
  31. width <- 7
  32. height <- 7
  33. gif_ <- joins[[i]](x, y, by = "id")
  34. first_ <- joins[[i]](x, y, by = "id", export = "first")
  35. last_ <- joins[[i]](x, y, by = "id", export = "last")
  36. save_animation(animate(gif_), here("images", "gif", paste0(nam, ".gif")))
  37. ggsave(here("images", "static", "png", paste0(nam, "-first.png")), first_,
  38. height = height, width = width)
  39. ggsave(here("images", "static", "svg", paste0(nam, "-first.svg")), first_,
  40. height = height, width = width)
  41. ggsave(here("images", "static", "png", paste0(nam, "-last.png")), last_,
  42. height = height, width = width)
  43. ggsave(here("images", "static", "svg", paste0(nam, "-last.svg")), last_,
  44. height = height, width = width)
  45. })
  46. ### Animate Sets
  47. x <- tibble::tribble(
  48. ~x, ~y,
  49. "1", "a",
  50. "1", "b",
  51. "2", "a"
  52. )
  53. y <- tibble::tribble(
  54. ~x, ~y,
  55. "1", "a",
  56. "2", "b"
  57. )
  58. sets <- c(
  59. union = animate_union,
  60. union_all = animate_union_all,
  61. intersect = animate_intersect,
  62. setdiff = animate_setdiff
  63. )
  64. a <- sapply(1:length(sets), function(i) {
  65. nam <- names(sets)[i]
  66. nam <- str_replace(nam, "_", "-")
  67. cat(nam, "\n")
  68. width <- 7
  69. height <- 7
  70. gif_ <- sets[[i]](x, y)
  71. first_ <- sets[[i]](x, y, export = "first")
  72. last_ <- sets[[i]](x, y, export = "last")
  73. save_animation(animate(gif_), here("images", "gif", paste0(nam, ".gif")))
  74. ggsave(here("images", "static", "png", paste0(nam, "-first.png")), first_,
  75. height = height, width = width)
  76. ggsave(here("images", "static", "svg", paste0(nam, "-first.svg")), first_,
  77. height = height, width = width)
  78. ggsave(here("images", "static", "png", paste0(nam, "-last.png")), last_,
  79. height = height, width = width)
  80. ggsave(here("images", "static", "svg", paste0(nam, "-last.svg")), last_,
  81. height = height, width = width)
  82. })
  83. ### Animate Gather Spread
  84. set_font_size(text_size = 4)
  85. set_anim_options(anim_options(cell_width = 2))
  86. # Gather
  87. wide <- dplyr::data_frame(
  88. year = 2010:2011,
  89. Alice = c(105, 110),
  90. Bob = c(100, 97),
  91. Charlie = c(90, 95)
  92. )
  93. nam <- "gather"
  94. cat(nam, "\n")
  95. width <- 7
  96. height <- 7
  97. gif_ <- animate_gather(wide, key = "person", value = "sales", -year, cell_width = 2)
  98. first_ <- animate_gather(wide, key = "person", value = "sales", -year, export = "first")
  99. last_ <- animate_gather(wide, key = "person", value = "sales", -year, export = "last")
  100. save_animation(animate(gif_), here("images", "gif", paste0(nam, ".gif")))
  101. ggsave(here("images", "static", "png", paste0(nam, "-first.png")), first_,
  102. height = height, width = width)
  103. ggsave(here("images", "static", "svg", paste0(nam, "-first.svg")), first_,
  104. height = height, width = width)
  105. ggsave(here("images", "static", "png", paste0(nam, "-last.png")), last_,
  106. height = height, width = width)
  107. ggsave(here("images", "static", "svg", paste0(nam, "-last.svg")), last_,
  108. height = height, width = width)
  109. # Spread
  110. long <- dplyr::data_frame(
  111. year = c(2010, 2011, 2010, 2011, 2010, 2011),
  112. person = c("Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie"),
  113. sales = c(105, 110, 100, 97, 90, 95)
  114. )
  115. nam <- "spread"
  116. cat(nam, "\n")
  117. width <- 7
  118. height <- 7
  119. gif_ <- animate_spread(long, key = "person", value = "sales")
  120. first_ <- animate_spread(long, key = "person", value = "sales", export = "first")
  121. last_ <- animate_spread(long, key = "person", value = "sales", export = "last")
  122. save_animation(animate(gif_), here("images", "gif", paste0(nam, ".gif")))
  123. ggsave(here("images", "static", "png", paste0(nam, "-first.png")), first_,
  124. height = height, width = width)
  125. ggsave(here("images", "static", "svg", paste0(nam, "-first.svg")), first_,
  126. height = height, width = width)
  127. ggsave(here("images", "static", "png", paste0(nam, "-last.png")), last_,
  128. height = height, width = width)
  129. ggsave(here("images", "static", "svg", paste0(nam, "-last.svg")), last_,
  130. height = height, width = width)