| if (!dir.exists(here::here("images"))) dir.create(here::here("images")) | if (!dir.exists(here::here("images"))) dir.create(here::here("images")) | ||||
| plot_data_join <- function(x, title = "", xlims = xlim(0.5, 5.5), ylims = ylim(-3.5, -0.5)) { | |||||
| plot_data(x, title) + | |||||
| xlims + ylims | |||||
| } | |||||
| # Data ---- | # Data ---- | ||||
| x <- data_frame( | x <- data_frame( | ||||
| id = 1:3, | id = 1:3, | ||||
| y = paste0("y", (1:4)[-3]) | y = paste0("y", (1:4)[-3]) | ||||
| ) | ) | ||||
| initial_dfs <- proc_data(x, "x") %>% | |||||
| initial_join_dfs <- proc_data(x, "x") %>% | |||||
| bind_rows(mutate(proc_data(y, "y"), .x = .x + 3)) %>% | bind_rows(mutate(proc_data(y, "y"), .x = .x + 3)) %>% | ||||
| mutate(frame = 1) | mutate(frame = 1) |
| plot_data <- function(x, title = "") { | plot_data <- function(x, title = "") { | ||||
| if (!"alpha" %in% colnames(x)) x$alpha <- 1 | if (!"alpha" %in% colnames(x)) x$alpha <- 1 | ||||
| if (!".width" %in% colnames(x)) x$`.width` <- 1 | |||||
| ggplot(x) + | ggplot(x) + | ||||
| aes(.x, .y, fill = color, label = value) + | aes(.x, .y, fill = color, label = value) + | ||||
| geom_tile(aes(width = .width, alpha = alpha), color = "white", size = 3) + | geom_tile(aes(width = .width, alpha = alpha), color = "white", size = 3) + | ||||
| guides(fill = FALSE) | guides(fill = FALSE) | ||||
| } | } | ||||
| animate_plot <- function(x) { | |||||
| animate_plot <- function(x, transition_length = 2, state_length = 1) { | |||||
| x + | x + | ||||
| transition_states(frame, transition_length = 2, state_length = 1) + | |||||
| transition_states(frame, transition_length, state_length) + | |||||
| enter_fade() + | enter_fade() + | ||||
| exit_fade() + | exit_fade() + | ||||
| ease_aes("sine-in-out") | ease_aes("sine-in-out") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| step2 <- initial_dfs %>% | |||||
| initial_join_dfs <- initial_join_dfs %>% | |||||
| arrange(.x, .y) %>% | |||||
| mutate(.obj = row_number(), .obj = .obj + 90 * as.integer(.id == "y")) | |||||
| aj_step2 <- initial_join_dfs %>% | |||||
| filter(.id == "x" | value %in% paste(1:2)) %>% | filter(.id == "x" | value %in% paste(1:2)) %>% | ||||
| mutate(frame = 2, | |||||
| .x = ifelse(.id == "y", 2.5, .x + 1.5), | |||||
| alpha = case_when( | |||||
| .x > 3 && .id == "x" ~ 0.5, | |||||
| .y > -2.5 ~ 0.25, | |||||
| TRUE ~ 1 | |||||
| )) | |||||
| aj_step3 <- aj_step2 %>% | |||||
| filter(alpha == 1) %>% | |||||
| mutate(frame = 3) | |||||
| aj_step4 <- aj_step2 %>% | |||||
| filter(alpha == 1) %>% | |||||
| mutate(frame = 4, .y = -1) | |||||
| aj <- bind_rows( | |||||
| initial_join_dfs, | |||||
| aj_step2, | |||||
| aj_step3, | |||||
| aj_step4 | |||||
| ) %>% | |||||
| mutate( | mutate( | ||||
| frame = 2, | |||||
| .x = ifelse(.id == "y", 1, .x), | |||||
| .x = .x + 1 | |||||
| ) | |||||
| step3 <- step2 %>% | |||||
| filter(grepl("3", value)) %>% | |||||
| ungroup() %>% | |||||
| mutate(frame = 3, .y = -1) | |||||
| aj <- initial_dfs %>% | |||||
| mutate(removed = .id == "y", removed = as.integer(removed)) %>% | |||||
| bind_rows(step2, step3) %>% | |||||
| mutate(removed = ifelse(is.na(removed), 0, removed)) %>% | |||||
| arrange(removed, .y, .x, desc(frame)) %>% #View() | |||||
| mutate(alpha = case_when( | |||||
| grepl("3", value) ~ 1, | |||||
| frame == 2 & label == "id" ~ 0.25, | |||||
| frame == 2 ~ 0.65, | |||||
| TRUE ~ 1 | |||||
| )) %>% | |||||
| { | |||||
| plot_data(., "anti_join(x, y)") + | |||||
| aes(alpha = alpha) + | |||||
| scale_alpha_identity() | |||||
| } %>% | |||||
| animate_plot() | |||||
| alpha = ifelse(is.na(alpha), 1, alpha), | |||||
| .obj = ifelse(value == 4, 0, .obj) | |||||
| ) %>% | |||||
| arrange(.obj, frame) %>% | |||||
| plot_data("anti_join(x, y)") %>% | |||||
| animate_plot(transition_length = c(2, 1, 2), | |||||
| state_length = c(1, 0, 0, 1)) | |||||
| aj <- animate(aj) | aj <- animate(aj) | ||||
| anim_save(here::here("images", "anti-join.gif"), aj) | anim_save(here::here("images", "anti-join.gif"), aj) | ||||
| aj_g <- anti_join(x, y, by = "id") %>% | aj_g <- anti_join(x, y, by = "id") %>% | ||||
| proc_data() %>% | proc_data() %>% | ||||
| plot_data("anti_join(x, y)") | |||||
| mutate(.x = .x + 1.5) %>% | |||||
| plot_data_join("anti_join(x, y)") | |||||
| save_static_plot(aj_g, "anti-join") | save_static_plot(aj_g, "anti-join") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| joined_df <- full_join(x, y, "id") %>% | |||||
| fj_joined_df <- full_join(x, y, "id") %>% | |||||
| proc_data("x") %>% | proc_data("x") %>% | ||||
| mutate(.id = ifelse(value %in% c("4", "y4"), "y", .id)) %>% | mutate(.id = ifelse(value %in% c("4", "y4"), "y", .id)) %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| extra_blocks <- inner_join(x, y, "id") %>% | |||||
| fj_extra_blocks <- inner_join(x, y, "id") %>% | |||||
| select(id) %>% | select(id) %>% | ||||
| proc_data("y") %>% | proc_data("y") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| fj <- initial_dfs %>% | |||||
| bind_rows(joined_df, extra_blocks) %>% | |||||
| fj <- initial_join_dfs %>% | |||||
| bind_rows(fj_joined_df, fj_extra_blocks) %>% | |||||
| plot_data("full_join(x, y)") + | plot_data("full_join(x, y)") + | ||||
| transition_states(frame, transition_length = 2, state_length = 1) + | transition_states(frame, transition_length = 2, state_length = 1) + | ||||
| enter_appear() + | enter_appear() + | ||||
| fj_g <- full_join(x, y, "id") %>% | fj_g <- full_join(x, y, "id") %>% | ||||
| proc_data() %>% | proc_data() %>% | ||||
| plot_data("full_join(x, y)") | |||||
| mutate(.x = .x + 1) %>% | |||||
| plot_data_join("full_join(x, y)", ylims = ylim(-4.5, -0.5)) | |||||
| save_static_plot(fj_g, "full-join") | save_static_plot(fj_g, "full-join") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| joined_df <- inner_join(x, y, "id") | |||||
| joined_df <- bind_rows( | |||||
| proc_data(joined_df, "x"), | |||||
| proc_data(joined_df, "y") | |||||
| ij_joined_df <- inner_join(x, y, "id") | |||||
| ij_joined_df <- bind_rows( | |||||
| proc_data(ij_joined_df, "x"), | |||||
| proc_data(ij_joined_df, "y") | |||||
| ) %>% | ) %>% | ||||
| filter(!(label == "x" & .id == "y") & !(label == "y" & .id == "x")) %>% | filter(!(label == "x" & .id == "y") & !(label == "y" & .id == "x")) %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| ij <- initial_dfs %>% | |||||
| bind_rows(joined_df) %>% | |||||
| ij <- bind_rows( | |||||
| initial_join_dfs, | |||||
| ij_joined_df | |||||
| ) %>% | |||||
| mutate(removed = value %in% c("3", "4", "x3", "y4"), | mutate(removed = value %in% c("3", "4", "x3", "y4"), | ||||
| removed = as.integer(removed)) %>% | removed = as.integer(removed)) %>% | ||||
| arrange(desc(frame), removed) %>% | |||||
| arrange(desc(frame), removed, desc(.id)) %>% | |||||
| plot_data("inner_join(x, y)") %>% | plot_data("inner_join(x, y)") %>% | ||||
| animate_plot() | animate_plot() | ||||
| ij_g <- inner_join(x, y, by = "id") %>% | ij_g <- inner_join(x, y, by = "id") %>% | ||||
| proc_data() %>% | proc_data() %>% | ||||
| plot_data("inner_join(x, y)") | |||||
| mutate(.x = .x + 1) %>% | |||||
| plot_data_join("inner_join(x, y)") | |||||
| save_static_plot(ij_g, "inner-join") | save_static_plot(ij_g, "inner-join") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| joined_dfs <- left_join(x, y, "id") %>% | |||||
| lj_joined_dfs <- left_join(x, y, "id") %>% | |||||
| proc_data("x") %>% | proc_data("x") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| extra_blocks <- inner_join(x, y, "id") %>% | |||||
| lj_extra_blocks <- inner_join(x, y, "id") %>% | |||||
| select(id) %>% | select(id) %>% | ||||
| proc_data("y") %>% | proc_data("y") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| lj <- initial_dfs %>% | |||||
| bind_rows(joined_dfs) %>% | |||||
| bind_rows(extra_blocks) %>% | |||||
| lj <- bind_rows( | |||||
| initial_join_dfs, | |||||
| lj_joined_dfs, | |||||
| lj_extra_blocks | |||||
| ) %>% | |||||
| mutate(color = ifelse(is.na(value), "#ffffff", color)) %>% | mutate(color = ifelse(is.na(value), "#ffffff", color)) %>% | ||||
| arrange(value) %>% | |||||
| plot_data("left_join(x, y)") %>% | plot_data("left_join(x, y)") %>% | ||||
| animate_plot() | animate_plot() | ||||
| lj <- animate(lj) | lj <- animate(lj) | ||||
| anim_save(here::here("images", "left-join.gif"), lj) | anim_save(here::here("images", "left-join.gif"), lj) | ||||
| lj_g <- plot_data(joined_dfs, "left_join(x, y)") | |||||
| lj_g <- plot_data_join(lj_joined_dfs, "left_join(x, y)") | |||||
| save_static_plot(lj_g, "left-join") | save_static_plot(lj_g, "left-join") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| y_extra <- bind_rows(y, data_frame(id = 2, y = "y5")) | y_extra <- bind_rows(y, data_frame(id = 2, y = "y5")) | ||||
| lj_g <- left_join(x, y_extra, by = "id") %>% | lj_g <- left_join(x, y_extra, by = "id") %>% | ||||
| proc_data() %>% | proc_data() %>% | ||||
| plot_data("left_join(x, y)") | |||||
| mutate(.x = .x + 1) %>% | |||||
| plot_data_join("left_join(x, y)", ylims = ylim(-4.5, -0.5)) | |||||
| save_static_plot(lj_g, "left-join-extra") | save_static_plot(lj_g, "left-join-extra") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| joined_dfs <- right_join(x, y, "id") %>% | |||||
| rj_joined_dfs <- right_join(x, y, "id") %>% | |||||
| proc_data("y") %>% | proc_data("y") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| extra_blocks <- inner_join(x, y, "id") %>% | |||||
| rj_extra_blocks <- inner_join(x, y, "id") %>% | |||||
| select(id) %>% | select(id) %>% | ||||
| proc_data("x") %>% | proc_data("x") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | mutate(frame = 2, .x = .x + 1) | ||||
| rj <- initial_dfs %>% | |||||
| bind_rows(joined_dfs, extra_blocks) %>% | |||||
| rj <- bind_rows( | |||||
| initial_join_dfs, | |||||
| rj_joined_dfs, | |||||
| rj_extra_blocks | |||||
| ) %>% | |||||
| filter(!is.na(value)) %>% | filter(!is.na(value)) %>% | ||||
| mutate( | mutate( | ||||
| .id = ifelse(label == "x", label, .id), | .id = ifelse(label == "x", label, .id), | ||||
| removed = as.integer(grepl("3", value)) | removed = as.integer(grepl("3", value)) | ||||
| ) %>% | ) %>% | ||||
| arrange(removed, .id, frame, value) %>% | |||||
| arrange(removed, value, .id, frame) %>% | |||||
| plot_data("right_join(x, y)") %>% | plot_data("right_join(x, y)") %>% | ||||
| animate_plot() | animate_plot() | ||||
| rj <- animate(rj) | rj <- animate(rj) | ||||
| anim_save(here::here("images", "right-join.gif"), rj) | anim_save(here::here("images", "right-join.gif"), rj) | ||||
| rj_g <- plot_data(joined_dfs, "right_join(x, y)") | |||||
| rj_g <- plot_data(rj_joined_dfs, "right_join(x, y)") | |||||
| save_static_plot(rj_g, "right-join") | save_static_plot(rj_g, "right-join") |
| source(here::here("R/00_base.R")) | |||||
| source(here::here("R/00_base_join.R")) | |||||
| joined_df <- semi_join(x, y, "id") %>% | |||||
| sj_joined_df <- semi_join(x, y, "id") %>% | |||||
| proc_data("x") %>% | proc_data("x") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | |||||
| mutate(frame = 2, .x = .x + 1.5) | |||||
| extra_blocks <- inner_join(x, y, "id") %>% | |||||
| sj_extra_blocks <- inner_join(x, y, "id") %>% | |||||
| select(id) %>% | select(id) %>% | ||||
| proc_data("y") %>% | proc_data("y") %>% | ||||
| mutate(frame = 2, .x = .x + 1) | |||||
| mutate(frame = 2, .x = .x + 1.5) | |||||
| sj <- initial_dfs %>% | |||||
| bind_rows(joined_df, extra_blocks) %>% | |||||
| sj <- bind_rows( | |||||
| initial_join_dfs, | |||||
| sj_joined_df, | |||||
| sj_extra_blocks | |||||
| ) %>% | |||||
| arrange(value) %>% | |||||
| plot_data("semi_join(x, y)") %>% | plot_data("semi_join(x, y)") %>% | ||||
| animate_plot() | animate_plot() | ||||
| # Static Images | # Static Images | ||||
| sj_g <- semi_join(x, y, "id") %>% | sj_g <- semi_join(x, y, "id") %>% | ||||
| proc_data() %>% | proc_data() %>% | ||||
| plot_data("semi_join(x, y)") | |||||
| mutate(.x = .x + 1.5) %>% | |||||
| plot_data_join("semi_join(x, y)") | |||||
| save_static_plot(sj_g, "semi-join") | save_static_plot(sj_g, "semi-join") |
| uyx <- animate(uyx) | uyx <- animate(uyx) | ||||
| anim_save(here::here("images", "union.gif"), uyx) | |||||
| anim_save(here::here("images", "union-rev.gif"), uyx) | |||||
| uyx_g <- union(y, x) %>% | uyx_g <- union(y, x) %>% | ||||
| proc_data_set() %>% | proc_data_set() %>% | ||||
| mutate(.x = .x + 1.5) %>% | mutate(.x = .x + 1.5) %>% | ||||
| plot_data_set("union(x, y)", ylims = ylim(-0.5, -4.5)) | plot_data_set("union(x, y)", ylims = ylim(-0.5, -4.5)) | ||||
| save_static_plot(uyx_g, "union") | |||||
| save_static_plot(uyx_g, "union-rev") |
| ua <- animate(ua) | ua <- animate(ua) | ||||
| anim_save(here::here("images", "union_all.gif"), ua) | |||||
| anim_save(here::here("images", "union-all.gif"), ua) | |||||
| ua_g <- union_all(x, y) %>% | ua_g <- union_all(x, y) %>% | ||||
| proc_data_set() %>% | proc_data_set() %>% |