Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

73 rindas
1.9KB

  1. #' Animation Options
  2. #'
  3. #' @param d a processed dataset
  4. #' @param title the title of the plot
  5. #' @param anim_opts Animation options generated with [anim_options()]. Overrides
  6. #' any options set in `...`.
  7. #' @return a `gganim` object
  8. #' @examples
  9. #' NULL
  10. animate_plot <- function(
  11. d,
  12. title = "",
  13. ...,
  14. anim_opts = anim_options(...)
  15. ) {
  16. ao <- validate_anim_opts(anim_opts)
  17. ease_opts <- if (!is.null(ao$ease_other)) {
  18. ao$ease_other$default <- ao$ease_default
  19. ao$ease_other
  20. } else list(default = ao$ease_default)
  21. ao_ease_aes <- do.call(ease_aes, ease_opts)
  22. static_plot(d, title, anim_opts = ao) +
  23. transition_states(.frame, ao$transition_length, ao$state_length) +
  24. ao$enter[[1]] +
  25. ao$exit[[1]] +
  26. ao_ease_aes
  27. }
  28. #' Prints the tiles for a processed dataset statically
  29. #'
  30. #' @inheritParams animate_plot
  31. #' @inheritDotParams anim_options
  32. #'
  33. #' @return a ggplot
  34. #'
  35. #' @examples
  36. #' NULL
  37. static_plot <- function(
  38. d,
  39. title = "",
  40. ...,
  41. anim_opts = anim_options(...)
  42. ) {
  43. ao <- validate_anim_opts(anim_opts)
  44. text_size <- get_text_size(ao$text_size)
  45. title_size <- get_title_size(ao$title_size)
  46. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  47. if (!".textcolor" %in% names(d))
  48. d <- d %>% mutate(.textcolor = choose_text_color(.color))
  49. if (".id_long" %in% names(d)) {
  50. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  51. } else {
  52. # tidyr
  53. d <- d %>% mutate(.item_id = .id)
  54. }
  55. ggplot(d, aes(x = .x, y = .y, fill = .color, alpha = .alpha, group = .item_id)) +
  56. geom_tile(width = 0.9, height = 0.9) +
  57. coord_equal() +
  58. geom_text(data = d %>% filter(!is.na(.val)), aes(label = .val, color = .textcolor),
  59. family = ao$text_family, size = text_size) +
  60. scale_fill_identity() +
  61. scale_color_identity() +
  62. scale_alpha_identity() +
  63. labs(title = title) +
  64. theme_void() +
  65. theme(plot.title = element_text(family = ao$title_family, hjust = 0.5, size = title_size))
  66. }