Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

60 lines
1.7KB

  1. #' Animates a set - wrapper function
  2. #'
  3. #' @param x left dataset
  4. #' @param y right dataset
  5. #' @param type type of the set, i.e., intersect, setdiff, etc.
  6. #' @param export if the function exports a gif, the first, or last picture
  7. #' @param ... further arguments passed to static_plot or to add_color
  8. #'
  9. #'
  10. #' @name animate_set_function
  11. #' @return either a gif or a ggplot
  12. #'
  13. #' @examples
  14. #' NULL
  15. animate_set <- function(x, y, type, export = "gif", ...) {
  16. if (!all(names(x) %in% names(y)) && ncol(x) == ncol(y))
  17. stop("x and y must have the same variables/column-names")
  18. if (!type %in% c("union", "union_all", "intersect", "setdiff"))
  19. stop("type has to be a dplyr-set operation")
  20. if (!export %in% c("gif", "first", "last"))
  21. stop("export must be either gif, first, or last")
  22. title <- sprintf(paste0(type, "(%s, %s)"),
  23. deparse(substitute(x)),
  24. deparse(substitute(y)))
  25. if (type %in% c("union", "intersect", "setdiff")) {
  26. x <- dplyr::distinct(x)
  27. y <- dplyr::distinct(y)
  28. }
  29. if (type == "union_all") {
  30. ll <- process_join(x, y, by = names(x), fill = FALSE, ...)
  31. ll <- lapply(ll, function(a)
  32. a %>% mutate(.id_long = paste(.id_long, .side, sep = "-"))
  33. )
  34. } else {
  35. ll <- process_join(x, y, by = names(x), ...)
  36. }
  37. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  38. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  39. all <- bind_rows(step0, step1)
  40. if (export == "gif") {
  41. animate_plot(all, title, ...) %>% animate()
  42. } else if (export == "first") {
  43. title <- ""
  44. static_plot(step0, title, ...)
  45. } else if (export == "last") {
  46. static_plot(step1, title, ...)
  47. }
  48. }