您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

53 行
1.5KB

  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. static_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 statically
  19. #'
  20. #' @param d a processed dataset
  21. #' @param title the title of the plot
  22. #' @param text_family the font for the text
  23. #' @param title_family the font for the title
  24. #' @param text_size the size of the text
  25. #' @param title_size the size of the title
  26. #' @param ... further arguments
  27. #'
  28. #' @return a ggplot
  29. #'
  30. #' @examples
  31. #' NULL
  32. static_plot <- function(d, title = "",
  33. text_family = "Fira Sans", title_family = "Fira Mono",
  34. text_size = 7, title_size = 25, ...) {
  35. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  36. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  37. ggplot(d, aes(x = .x, group = .item_id, y = .y, fill = .color, alpha = .alpha)) +
  38. geom_tile(width = 0.9, height = 0.9) +
  39. coord_equal() +
  40. geom_text(data = d %>% filter(!is.na(.val)), aes(label = .val), color = "white",
  41. family = text_family, size = text_size) +
  42. scale_fill_identity() +
  43. scale_alpha_identity() +
  44. labs(title = title) +
  45. theme_void() +
  46. theme(plot.title = element_text(family = title_family, hjust = 0.5, size = title_size))
  47. }