Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

111 lines
3.2KB

  1. #' Animates a plot
  2. #'
  3. #' @param d a preprocessed dataset
  4. #' @param title the plot title
  5. #' @param transition_length see transition_states
  6. #' @param state_length see transition_states
  7. #' @param ... further arguments passed to static_plot
  8. #'
  9. #' @return a gif
  10. #'
  11. #' @examples
  12. #' NULL
  13. animate_plot <- function(d, title = "", transition_length = 2, state_length = 1, ...) {
  14. static_plot(d, title, ...) +
  15. transition_states(.frame, transition_length, state_length) +
  16. enter_fade() +
  17. exit_fade() +
  18. ease_aes("sine-in-out")
  19. }
  20. #' Prints the tiles for a processed dataset statically
  21. #'
  22. #' @param d a processed dataset
  23. #' @param title the title of the plot
  24. #' @param text_family the font for the text
  25. #' @param title_family the font for the title
  26. #' @param text_size the size of the text
  27. #' @param title_size the size of the title
  28. #' @param ... further arguments
  29. #'
  30. #' @return a ggplot
  31. #'
  32. #' @examples
  33. #' NULL
  34. static_plot <- function(
  35. d,
  36. title = "",
  37. text_family = "Fira Sans", title_family = "Fira Mono",
  38. text_size = NULL, title_size = NULL,
  39. ...
  40. ) {
  41. text_size <- get_text_size(text_size, default = 5)
  42. title_size <- get_title_size(title_size, default = 17)
  43. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  44. if (!".textcolor" %in% names(d))
  45. d <- d %>% mutate(.textcolor = choose_text_color(.color))
  46. if (".id_long" %in% names(d)) {
  47. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  48. } else {
  49. # tidyr
  50. d <- d %>% mutate(.item_id = .id)
  51. }
  52. ggplot(d, aes(x = .x, y = .y, fill = .color, alpha = .alpha, group = .item_id)) +
  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 = .textcolor),
  56. family = text_family, size = text_size) +
  57. scale_fill_identity() +
  58. scale_color_identity() +
  59. scale_alpha_identity() +
  60. labs(title = title) +
  61. theme_void() +
  62. theme(plot.title = element_text(family = title_family, hjust = 0.5, size = title_size))
  63. }
  64. #' Set Default Text Sizes for Animation Plots
  65. #'
  66. #' Sets the default text sizes for the animated and static plots produced by
  67. #' this package during the current session.
  68. #'
  69. #' @param text_size Font size of value labels inside the data frame squares
  70. #' @param title_size Font size of the function call or plot title
  71. #' @export
  72. set_font_size <- function(text_size = NULL, title_size = NULL) {
  73. old <- list()
  74. if (!is.null(text_size)) old$text_size <- set_text_size(text_size)
  75. if (!is.null(title_size)) old$title_size <- set_title_size(title_size)
  76. invisible(old)
  77. }
  78. set_text_size <- function(size) {
  79. old <- plot_settings$text_size
  80. plot_settings$text_size <- size
  81. invisible(old)
  82. }
  83. set_title_size <- function(size) {
  84. old <- plot_settings$title_size
  85. plot_settings$title_size <- size
  86. invisible(old)
  87. }
  88. get_text_size <- function(x = NULL, default = 5) {
  89. if (!is.null(x)) return(x)
  90. plot_settings$text_size %||%
  91. getFromNamespace("theme_env", "ggplot2")$current$text$size %||%
  92. default
  93. }
  94. get_title_size <- function(x = NULL, default = 17) {
  95. if (!is.null(x)) return(x)
  96. plot_settings$title_size %||%
  97. getFromNamespace("theme_env", "ggplot2")$current$plot.title$size %||%
  98. default
  99. }