選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

113 行
3.4KB

  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 (!".text_color" %in% colnames(x)) x$`.text_color` <- "white"
  66. if (!".text_size" %in% colnames(x)) x$`.text_size` <- 12
  67. ggplot(x) +
  68. aes(.x, .y, fill = color, label = value) +
  69. geom_tile(aes(alpha = alpha), width = 0.9, height = 0.9) +
  70. geom_text(aes(x = .x, color = .text_color, size = .text_size), hjust = 0.5, family = "Fira Sans") +
  71. scale_fill_identity() +
  72. scale_alpha_identity() +
  73. scale_color_identity() +
  74. scale_size_identity() +
  75. coord_equal() +
  76. ggtitle(title) +
  77. theme_void() +
  78. theme(plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = 24)) +
  79. guides(fill = FALSE)
  80. }
  81. animate_plot <- function(x, transition_length = 2, state_length = 1) {
  82. x +
  83. transition_states(frame, transition_length, state_length) +
  84. enter_fade() +
  85. exit_fade() +
  86. ease_aes("sine-in-out")
  87. }
  88. save_static_plot <- function(g, filename, formats = c("png", "svg")) {
  89. filenames <- formats %>%
  90. purrr::set_names() %>%
  91. purrr::map_chr(static_plot_filename, x = filename) %>%
  92. purrr::iwalk(
  93. ~ ggsave(filename = .x, plot = g, dev = .y)
  94. )
  95. }
  96. static_plot_filename <- function(x, ext) {
  97. here::here("images", "static", ext, paste0(x, ".", ext))
  98. }
  99. options(tidy_verb_anim.functions_loaded = TRUE)