您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

135 行
3.6KB

  1. #' Animates a set operation
  2. #'
  3. #' Functions to visualise the set operations either static as a ggplot, or
  4. #' dynamic as a gif.
  5. #'
  6. #' @param x the x dataset
  7. #' @param y the y dataset
  8. #' @param export the export type, either gif, first or last. The latter two
  9. #' export ggplots of the first/last state of the join
  10. #' @param type type of the set, i.e., intersect, setdiff, etc.
  11. #' @param ... further argument passed to static_plot
  12. #'
  13. #' @return either a gif or a ggplot
  14. #'
  15. #' @seealso \code{\link[dplyr]{setops}}
  16. #'
  17. #' @examples
  18. #' x <- data_frame(x = c(1, 1, 2), y = c("a", "b", "a"))
  19. #' y <- data_frame(x = c(1, 2), y = c("a", "b"))
  20. #'
  21. #' # Animate the first or last state of the set
  22. #' animate_union(x, y, export = "first")
  23. #' animate_union(x, y, export = "last")
  24. #'
  25. #' # animate the transition as a gif (default)
  26. #' \donttest{
  27. #' animate_union(x, y, export = "gif")
  28. #' }
  29. #'
  30. #' # different options include
  31. #' \donttest{
  32. #' animate_union(x, y)
  33. #' animate_union_all(x, y)
  34. #' animate_intersect(x, y)
  35. #' animate_setdiff(x, y)
  36. #'
  37. #' # further arguments can be passed to all animate_* functions
  38. #' animate_union(
  39. #' x, y,
  40. #' text_size = 5, title_size = 25,
  41. #' color_header = "black",
  42. #' color_other = "lightblue",
  43. #' color_fun = viridis::viridis
  44. #' )
  45. #' }
  46. #'
  47. #' # Save the results
  48. #' \dontrun{
  49. #' # to save the ggplot, use
  50. #' un <- animate_union(x, y, by = "id", export = "last")
  51. #' ggsave("union.pdf", un)
  52. #'
  53. #' animate_union(x, y, by = "id", export = "gif")
  54. #' # to save the gif, use
  55. #' un <- animate_union(x, y, by = "id", export = "gif")
  56. #' anim_save(un, "union.gif")
  57. #' }
  58. animate_set <- function(
  59. x, y,
  60. type = c("union", "union_all", "intersect", "setdiff"),
  61. export = c("gif", "first", "last"),
  62. ...
  63. ) {
  64. type <- match.arg(type)
  65. export <- match.arg(export)
  66. x_name <- get_input_text(x)
  67. y_name <- get_input_text(y)
  68. data <- make_named_data(x, y)
  69. col_names <- purrr::map(data, names)
  70. if (!all(names(data$x) %in% names(data$y)) && ncol(data$x) == ncol(data$y))
  71. stop("x and y must have the same variables/column-names")
  72. title <- sprintf(paste0(type, "(%s, %s)"), x_name, y_name)
  73. if (type %in% c("union", "intersect", "setdiff")) {
  74. data <- purrr::map(data, dplyr::distinct)
  75. }
  76. if (type == "union_all") {
  77. ll <- process_join(data$x, data$y, by = names(data$x), fill = FALSE, ...)
  78. ll <- purrr::map(ll, ~ mutate(., .id_long = paste(.id_long, .side, sep = "-")))
  79. } else {
  80. ll <- process_join(data$x, data$y, by = names(data$x), ...)
  81. }
  82. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  83. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  84. all <- bind_rows(step0, step1)
  85. if (export == "gif") {
  86. animate_plot(all, title, ...) %>% animate()
  87. } else if (export == "first") {
  88. title <- ""
  89. static_plot(step0, title, ...)
  90. } else if (export == "last") {
  91. static_plot(step1, title, ...)
  92. }
  93. }
  94. #' @rdname animate_set
  95. #' @export
  96. animate_union <- function(x, y, export = "gif", ...) {
  97. x <- rlang::enquo(x)
  98. y <- rlang::enquo(y)
  99. animate_set(x, y, type = "union", export = export, ...)
  100. }
  101. #' @rdname animate_set
  102. #' @export
  103. animate_union_all <- function(x, y, export = "gif", ...) {
  104. x <- rlang::enquo(x)
  105. y <- rlang::enquo(y)
  106. animate_set(x, y, type = "union_all", export = export, ...)
  107. }
  108. #' @rdname animate_set
  109. #' @export
  110. animate_intersect <- function(x, y, export = "gif", ...) {
  111. x <- rlang::enquo(x)
  112. y <- rlang::enquo(y)
  113. animate_set(x, y, type = "intersect", export = export, ...)
  114. }
  115. #' @rdname animate_set
  116. #' @export
  117. animate_setdiff <- function(x, y, export = "gif", ...) {
  118. x <- rlang::enquo(x)
  119. y <- rlang::enquo(y)
  120. animate_set(x, y, type = "setdiff", export = export, ...)
  121. }