No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

67 líneas
1.9KB

  1. proc_data <- function(x, .id = "x") {
  2. colors <- scales::brewer_pal(type = "qual", "Set1")(max(x$id))
  3. x %>%
  4. mutate(.y = -row_number()) %>%
  5. tidyr::gather("label", "value", -.y) %>%
  6. mutate(value = as.character(value)) %>%
  7. group_by(.y) %>%
  8. mutate(
  9. .x = 1:n(),
  10. .id = .id,
  11. color = ifelse(label == "id", value, max(x$id) + 1),
  12. color = colors[as.integer(color)],
  13. color = ifelse(is.na(color), "#d0d0d0", color),
  14. color = ifelse(is.na(value), "#ffffff", color)
  15. )
  16. }
  17. proc_data2 <- function(x, .id = "x") {
  18. colors <- scales::brewer_pal(type = "qual", "Set1")(max(x$id))
  19. x %>%
  20. mutate(.y = -row_number()) %>%
  21. mutate(color = colors[id]) %>%
  22. tidyr::gather("label", "value", -.y, -color) %>%
  23. mutate(value = as.character(value)) %>%
  24. group_by(.y) %>%
  25. mutate(
  26. .x = 1:n(),
  27. .id = .id,
  28. )
  29. }
  30. plot_data <- function(x, title = "") {
  31. ggplot(x) +
  32. aes(.x, .y, fill = color, label = value) +
  33. geom_tile(color = "white", size = 3) +
  34. geom_text(aes(x = .x), hjust = 0.5, size = 12, family = "Fira Sans", color = "white") +
  35. scale_fill_identity() +
  36. coord_equal() +
  37. ggtitle(title) +
  38. theme_void() +
  39. theme(plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = 24)) +
  40. guides(fill = FALSE)
  41. }
  42. animate_plot <- function(x) {
  43. x +
  44. transition_states(frame, transition_length = 2, state_length = 1) +
  45. enter_fade() +
  46. exit_fade() +
  47. ease_aes("sine-in-out")
  48. }
  49. save_static_plot <- function(g, filename, formats = c("png", "svg")) {
  50. filenames <- formats %>%
  51. purrr::set_names() %>%
  52. purrr::map_chr(static_plot_filename, x = filename) %>%
  53. purrr::iwalk(
  54. ~ ggsave(filename = .x, plot = g, dev = .y)
  55. )
  56. }
  57. static_plot_filename <- function(x, ext) {
  58. here::here("images", "static", ext, paste0(x, ".", ext))
  59. }
  60. options(tidy_verb_anim.functions_loaded = TRUE)