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

71 lines
1.7KB

  1. #' Animates a plot
  2. #'
  3. #' @param d a preprocessed dataset
  4. #' @param title the plot title
  5. #' @param ... further arguments passed to base_plot
  6. #'
  7. #' @return a gif
  8. #'
  9. #' @examples
  10. #' NULL
  11. animate_plot <- function(d, title = "", ...) {
  12. base_plot(d, title, ...) +
  13. transition_states(.frame, 2, 1) +
  14. enter_fade() +
  15. exit_fade() +
  16. ease_aes("sine-in-out")
  17. }
  18. #' Prints the tiles for a processed dataset
  19. #'
  20. #' @param d a processed dataset
  21. #' @param title the title of the plot
  22. #' @param ... further arguments
  23. #'
  24. #' @return a ggplot
  25. #'
  26. #' @examples
  27. #' NULL
  28. base_plot <- function(d, title = "", ...) {
  29. dots <- list(...)
  30. if ("text_family" %in% names(dots)) {
  31. text_family <- dots$text_family
  32. } else {
  33. text_family <- "Fira Sans"
  34. }
  35. if ("title_family" %in% names(dots)) {
  36. title_family <- dots$title_family
  37. } else {
  38. title_family <- "Fira Mono"
  39. }
  40. if ("title_size" %in% names(dots)) {
  41. title_size <- dots$title_size
  42. } else {
  43. title_size <- 20
  44. }
  45. if ("text_size" %in% names(dots)) {
  46. text_size <- dots$text_size
  47. } else {
  48. text_size <- 10
  49. }
  50. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  51. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  52. ggplot(d, aes(x = .x, group = .item_id, y = .y, fill = .color, alpha = .alpha)) +
  53. geom_tile(width = 0.9, height = 0.9) +
  54. coord_equal() +
  55. geom_text(data = d %>% filter(!is.na(.val)), aes(label = .val), color = "white",
  56. family = text_family, size = text_size) +
  57. scale_fill_identity() +
  58. scale_alpha_identity() +
  59. labs(title = title) +
  60. theme_void() +
  61. theme(plot.title = element_text(family = title_family, hjust = 0.5, size = title_size))
  62. }