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.

114 satır
3.5KB

  1. proc_data <- function(x, .id = "x", color_fun = colorize_keys, color_when = c("after", "before"), ...) {
  2. color_when <- match.arg(color_when)
  3. n_colors <- max(x$id)
  4. if (color_when == "before") x <- color_fun(x, n_colors, ...)
  5. x <- x %>%
  6. mutate(.y = -row_number()) %>%
  7. tidyr::gather("label", "value", setdiff(colnames(x), c(".y", "color"))) %>%
  8. mutate(value = as.character(value)) %>%
  9. group_by(.y) %>%
  10. mutate(
  11. .x = 1:n(),
  12. .id = .id,
  13. .width = 1
  14. ) %>%
  15. ungroup(.y)
  16. if (color_when == "after") x <- color_fun(x, n_colors, ...)
  17. x
  18. }
  19. colorize_keys <- function(df, n_colors, key_col = "id", color_other = "#d0d0d0", color_missing = "#ffffff") {
  20. # Assumes that key_col is integer
  21. colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
  22. mutate(
  23. df,
  24. color = ifelse(label == key_col, value, n_colors + 1),
  25. color = colors[as.integer(color)],
  26. color = ifelse(is.na(color), "#d0d0d0", color),
  27. color = ifelse(is.na(value), "#ffffff", color)
  28. )
  29. }
  30. colorize_row_id <- function(df, n_colors, key_col = "id") {
  31. # Assumes that key_col is integer
  32. colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
  33. df$color <- colors[df[[key_col]]]
  34. df
  35. }
  36. colorize_wide_tidyr <- function(df, n_colors, key_col = "id") {
  37. n_colors <- n_colors + length(setdiff(unique(df$label), key_col))
  38. colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
  39. df$value_int <- as.integer(gsub("[a-zA-Z]", "0", df$value))
  40. max_id_color <- max(df$value_int)
  41. df %>%
  42. bind_rows(
  43. filter(df, .y == "-1") %>% mutate(.y = 0)
  44. ) %>%
  45. mutate(
  46. idcp = max_id_color - 1L,
  47. idc = case_when(
  48. label == "id" ~ value_int,
  49. TRUE ~ map_int(label, ~which(. == unique(label))) + idcp
  50. )
  51. ) %>%
  52. select(-idcp, -value_int) %>%
  53. mutate(
  54. idc = ifelse(.y == 0 & label == "id", 100, idc),
  55. value = ifelse(.y == 0, label, value),
  56. .id = ifelse(.y == 0, "n", .id),
  57. color = colors[idc],
  58. ) %>%
  59. filter(!is.na(color)) %>%
  60. mutate(alpha = ifelse(label != "id" & .y < 0, 0.6, 1.0)) %>%
  61. select(-idc)
  62. }
  63. plot_data <- function(x, title = "") {
  64. if (!"alpha" %in% colnames(x)) x$alpha <- 1
  65. if (!".width" %in% colnames(x)) x$`.width` <- 1
  66. if (!".text_color" %in% colnames(x)) x$`.text_color` <- "white"
  67. if (!".text_size" %in% colnames(x)) x$`.text_size` <- 12
  68. ggplot(x) +
  69. aes(.x, .y, fill = color, label = value) +
  70. geom_tile(aes(width = .width, alpha = alpha), color = "white", size = 3) +
  71. geom_text(aes(x = .x, color = .text_color, size = .text_size), hjust = 0.5, family = "Fira Sans") +
  72. scale_fill_identity() +
  73. scale_alpha_identity() +
  74. scale_color_identity() +
  75. scale_size_identity() +
  76. coord_equal() +
  77. ggtitle(title) +
  78. theme_void() +
  79. theme(plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = 24)) +
  80. guides(fill = FALSE)
  81. }
  82. animate_plot <- function(x, transition_length = 2, state_length = 1) {
  83. x +
  84. transition_states(frame, transition_length, state_length) +
  85. enter_fade() +
  86. exit_fade() +
  87. ease_aes("sine-in-out")
  88. }
  89. save_static_plot <- function(g, filename, formats = c("png", "svg")) {
  90. filenames <- formats %>%
  91. purrr::set_names() %>%
  92. purrr::map_chr(static_plot_filename, x = filename) %>%
  93. purrr::iwalk(
  94. ~ ggsave(filename = .x, plot = g, dev = .y)
  95. )
  96. }
  97. static_plot_filename <- function(x, ext) {
  98. here::here("images", "static", ext, paste0(x, ".", ext))
  99. }
  100. options(tidy_verb_anim.functions_loaded = TRUE)