Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

163 lines
4.6KB

  1. #' Animation Options
  2. #'
  3. #' Helper function to set animation and plotting options to be passed to
  4. #' [animate_plot()] and [static_plot()].
  5. #'
  6. #' @param text_family Font family for the plot text
  7. #' @param title_family Font family for the plot title
  8. #' @param text_size Font size of the plot text
  9. #' @param title_size Font size of the plot title
  10. #' @param ease_default Default aes easing function. See [tweenr::display_ease]
  11. #' for more options.
  12. #' @param ease_other Additional aes easing options, named with aesthetic to
  13. #' which the easeing should be applied, consistent with [gganimate::ease_aes()].
  14. #' @param enter_exit Enter/exit fading functions applied to objects in the animation.
  15. #' See [gganimate::enter_exit] for a complete list of options.
  16. #' @inheritParams gganimate::transition_states
  17. #' @export
  18. anim_options <- function(
  19. transition_length = 2,
  20. state_length = 1,
  21. ease_default = "sine-in-out",
  22. ease_other = NULL,
  23. enter_ = enter_fade(),
  24. exit_ = exit_fade(),
  25. text_family = "Fira Sans",
  26. title_family = "Fira Mono",
  27. text_size = NULL,
  28. title_size = NULL,
  29. ...
  30. ){
  31. structure(
  32. list(
  33. transition_length = transition_length,
  34. state_length = state_length,
  35. ease_default = ease_default,
  36. ease_other = ease_other,
  37. enter_ = enter_,
  38. exit_ = exit_,
  39. text_family = text_family,
  40. text_size = text_size,
  41. title_family = title_family,
  42. title_size = title_size,
  43. ...
  44. ),
  45. class = "anim_opts"
  46. )
  47. }
  48. #' Animates a plot
  49. #'
  50. #' @param d a processed dataset
  51. #' @param title the title of the plot
  52. #' @param anim_opts Animation options generated with [anim_options()]. Overrides
  53. #' any options set in `...`.
  54. #' @return a `gganim` object
  55. #' @examples
  56. #' NULL
  57. animate_plot <- function(
  58. d,
  59. title = "",
  60. ...,
  61. anim_opts = anim_options(...)
  62. ) {
  63. ao <- anim_opts
  64. ease_opts <- if (!is.null(ao$ease_other)) {
  65. ao$ease_other$default <- ao$ease_default
  66. ao$ease_other
  67. } else list(default = ao$ease_default)
  68. ao_ease_aes <- do.call(ease_aes, ease_opts)
  69. static_plot(d, title, ao$text_family, ao$title_family, ao$text_size, ao$title_size) +
  70. transition_states(.frame, ao$transition_length, ao$state_length) +
  71. ao$enter_ +
  72. ao$exit_ +
  73. ao_ease_aes
  74. }
  75. #' Prints the tiles for a processed dataset statically
  76. #'
  77. #' @inheritParams animate_plot
  78. #' @inheritDotParams anim_options
  79. #'
  80. #' @return a ggplot
  81. #'
  82. #' @examples
  83. #' NULL
  84. static_plot <- function(
  85. d,
  86. title = "",
  87. ...,
  88. anim_opts = anim_options(...)
  89. ) {
  90. ao <- anim_opts
  91. text_size <- get_text_size(ao$text_size, default = 5)
  92. title_size <- get_title_size(ao$title_size, default = 17)
  93. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  94. if (!".textcolor" %in% names(d))
  95. d <- d %>% mutate(.textcolor = choose_text_color(.color))
  96. if (".id_long" %in% names(d)) {
  97. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  98. } else {
  99. # tidyr
  100. d <- d %>% mutate(.item_id = .id)
  101. }
  102. ggplot(d, aes(x = .x, y = .y, fill = .color, alpha = .alpha, group = .item_id)) +
  103. geom_tile(width = 0.9, height = 0.9) +
  104. coord_equal() +
  105. geom_text(data = d %>% filter(!is.na(.val)), aes(label = .val, color = .textcolor),
  106. family = ao$text_family, size = text_size) +
  107. scale_fill_identity() +
  108. scale_color_identity() +
  109. scale_alpha_identity() +
  110. labs(title = title) +
  111. theme_void() +
  112. theme(plot.title = element_text(family = ao$title_family, hjust = 0.5, size = title_size))
  113. }
  114. #' Set Default Text Sizes for Animation Plots
  115. #'
  116. #' Sets the default text sizes for the animated and static plots produced by
  117. #' this package during the current session.
  118. #'
  119. #' @param text_size Font size of value labels inside the data frame squares
  120. #' @param title_size Font size of the function call or plot title
  121. #' @export
  122. set_font_size <- function(text_size = NULL, title_size = NULL) {
  123. old <- list()
  124. if (!is.null(text_size)) old$text_size <- set_text_size(text_size)
  125. if (!is.null(title_size)) old$title_size <- set_title_size(title_size)
  126. invisible(old)
  127. }
  128. set_text_size <- function(size) {
  129. old <- plot_settings$text_size
  130. plot_settings$text_size <- size
  131. invisible(old)
  132. }
  133. set_title_size <- function(size) {
  134. old <- plot_settings$title_size
  135. plot_settings$title_size <- size
  136. invisible(old)
  137. }
  138. get_text_size <- function(x = NULL, default = 5) {
  139. if (!is.null(x)) return(x)
  140. plot_settings$text_size %||%
  141. getFromNamespace("theme_env", "ggplot2")$current$text$size %||%
  142. default
  143. }
  144. get_title_size <- function(x = NULL, default = 17) {
  145. if (!is.null(x)) return(x)
  146. plot_settings$title_size %||%
  147. getFromNamespace("theme_env", "ggplot2")$current$plot.title$size %||%
  148. default
  149. }