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.

167 linhas
4.8KB

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