Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

64 Zeilen
1.9KB

  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(d, title = "",
  35. text_family = "Fira Sans", title_family = "Fira Mono",
  36. text_size = 5, title_size = 17, ...) {
  37. if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
  38. if (!".textcolor" %in% names(d))
  39. d <- d %>% mutate(.textcolor = set_text_color(.color))
  40. if (".id_long" %in% names(d)) {
  41. d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-"))
  42. } else {
  43. # tidyr
  44. d <- d %>% mutate(.item_id = .id)
  45. }
  46. ggplot(d, aes(x = .x, y = .y, fill = .color, alpha = .alpha, group = .item_id)) +
  47. geom_tile(width = 0.9, height = 0.9) +
  48. coord_equal() +
  49. geom_text(data = d %>% filter(!is.na(.val)), aes(label = .val, color = .textcolor),
  50. family = text_family, size = text_size) +
  51. scale_fill_identity() +
  52. scale_color_identity() +
  53. scale_alpha_identity() +
  54. labs(title = title) +
  55. theme_void() +
  56. theme(plot.title = element_text(family = title_family, hjust = 0.5, size = title_size))
  57. }