You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 line
1.4KB

  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 (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  41. ggplot(d, aes(x = .x, group = .id_long, y = .y, fill = .color, alpha = .alpha)) +
  42. geom_tile(width = 0.9, height = 0.9) +
  43. coord_equal() +
  44. geom_text(data = d %>% filter(!is.na(val)), aes(label = val), color = "white",
  45. family = text_family, size = 10) +
  46. scale_fill_identity() +
  47. scale_alpha_identity() +
  48. labs(title = title) +
  49. theme_void() +
  50. theme(plot.title = element_text(family = title_family, hjust = 0.5, size = 20))
  51. }