浏览代码

Merge 1d4cb9a887 into fe4d4b0690

pull/22/merge
Kirill Müller 7 年前
父节点
当前提交
b1e8cab2c6
没有帐户链接到提交者的电子邮件
共有 8 个文件被更改,包括 166 次插入165 次删除
  1. +1
    -1
      R/00_base_tidyr.R
  2. +4
    -3
      R/02_functions.R
  3. +7
    -7
      R/tidyr_spread_gather.R
  4. 二进制
      images/static/png/tidyr-gather.png
  5. 二进制
      images/static/png/tidyr-spread.png
  6. +83
    -83
      images/static/svg/tidyr-gather.svg
  7. +71
    -71
      images/static/svg/tidyr-spread.svg
  8. 二进制
      images/tidyr-spread-gather.gif

+ 1
- 1
R/00_base_tidyr.R 查看文件

@@ -25,4 +25,4 @@ wide <- data_frame(
z = letters[5:6]
)

long <- tidyr::gather(wide, key, val, x:z)
long <- tidyr::gather(wide, key, val, -id)

+ 4
- 3
R/02_functions.R 查看文件

@@ -65,11 +65,12 @@ colorize_wide_tidyr <- function(df, n_colors, key_col = "id") {
color = colors[idc],
) %>%
filter(!is.na(color)) %>%
mutate(alpha = ifelse(label != "id" & .y < 0, 0.6, 1.0)) %>%
mutate(alpha = ifelse(.y < 0, 1.0, 0.2)) %>%
mutate(.text_color = ifelse(.y < 0, "white", "black")) %>%
select(-idc)
}

plot_data <- function(x, title = "") {
plot_data <- function(x, title = "", title_size = 20) {
if (!"alpha" %in% colnames(x)) x$alpha <- 1
if (!".text_color" %in% colnames(x)) x$`.text_color` <- "white"
if (!".text_size" %in% colnames(x)) x$`.text_size` <- 12
@@ -84,7 +85,7 @@ plot_data <- function(x, title = "") {
coord_equal() +
ggtitle(title) +
theme_void() +
theme(plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = 24)) +
theme(plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = title_size)) +
guides(fill = FALSE)
}


+ 7
- 7
R/tidyr_spread_gather.R 查看文件

@@ -7,6 +7,7 @@ sg_wide <- wide %>%
sg_long <- wide %>%
tidyr::gather("key", "val", -id) %>%
proc_data("3-tall", color_fun = function(x, y) x) %>%
mutate(.text_color = if_else(label == "key", "black", "white")) %>%
split(.$label)

sg_long$id <-
@@ -22,26 +23,26 @@ sg_long$key <-
select(label, color) %>%
left_join(sg_long$key, ., by = c("value" = "label")) %>%
distinct() %>%
mutate(alpha = 1)
mutate(alpha = 0.6)

sg_long$val <-
sg_wide %>%
filter(label != "id", .y < 0) %>%
select(value, color) %>%
left_join(sg_long$val, ., by = "value") %>%
mutate(alpha = 0.6)
mutate(alpha = 1)

sg_long <- bind_rows(sg_long) %>% mutate(frame = 2)

sg_long_labels <- data_frame(id = 1, a = "id", x = "key", y = "val") %>%
proc_data("4-label") %>%
filter(label != "id") %>%
mutate(color = "#FFFFFF", .y = 0, .x = .x -1, frame = 2, alpha = 1, label = recode(label, "a" = "id"))
mutate(color = "white", .text_color = "black", .y = 0, .x = .x -1, frame = 2, alpha = 1, label = recode(label, "a" = "id"))

sg_wide_labels <- data_frame(id = 1, a = "id") %>%
proc_data("2-label") %>%
filter(label != "id") %>%
mutate(color = "#FFFFFF", .y = 0, .x = .x -1, frame = 1, alpha = 1, label = recode(label, "a" = "id"))
mutate(color = "white", .text_color = "black", .y = 0, .x = .x -1, frame = 1, alpha = 1, label = recode(label, "a" = "id"))

sg_long_extra_keys <- map_dfr(
seq_len(nrow(wide) - 1),
@@ -66,11 +67,10 @@ sg_data <- bind_rows(
mutate(
label = ifelse(value %in% setdiff(colnames(wide), "id"), "key", label),
label = ifelse(value %in% c("key", "val"), "zzz", label),
.text_color = ifelse(grepl("label", .id), "black", "white"),
.text_size = ifelse(grepl("label", .id), 8, 12)
.text_size = ifelse(grepl("label", .id) | .y == 0, 8, 12)
) %>%
arrange(label, .id, value) %>%
mutate(frame = factor(frame, labels = c('spread(long, key, val)', 'gather(wide, key, val, x:z)'))) %>%
mutate(frame = factor(frame, labels = c('spread(long, key, val)', 'gather(wide, key, val, -id)'))) %>%
select(.x, .y, everything())

sg_static <-

二进制
images/static/png/tidyr-gather.png 查看文件

之前 之后
宽度: 3429  |  高度: 4658  |  大小: 373KB 宽度: 2100  |  高度: 2100  |  大小: 39KB

二进制
images/static/png/tidyr-spread.png 查看文件

之前 之后
宽度: 3429  |  高度: 4658  |  大小: 347KB 宽度: 2100  |  高度: 2100  |  大小: 32KB

+ 83
- 83
images/static/svg/tidyr-gather.svg
文件差异内容过多而无法显示
查看文件


+ 71
- 71
images/static/svg/tidyr-spread.svg
文件差异内容过多而无法显示
查看文件


二进制
images/tidyr-spread-gather.gif 查看文件

之前 之后
宽度: 480  |  高度: 480  |  大小: 789KB 宽度: 480  |  高度: 480  |  大小: 893KB

正在加载...
取消
保存