在 R flextable 中,如何在列标题之间添加分隔符
In R flextable how can I add a break between column headings
我有一个包含嵌套列标题的意外事件 table,我需要帮助在列标题之间添加 space。共有三种组织学,每种组织学内部都有三个阶段。我想在三组舞台之间休息。我走到这一步:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(blank = " ") %>%
mutate(blank2 = " ") %>%
select(time, blank, `1_1`, `2_1`, `3_1`, blank2, everything())
my_header <- data.frame(
col_keys = c("time", "blank", "1_1", "2_1", "3_1", "blank", "1_2", "2_2", "3_2", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", rep("I", 3), "blank", rep("II", 3), rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", c(1,2,3), "blank", rep(c(1,2,3),2))
)
library(flextable)
flextable(cancerCountWide) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks()
这让我很接近:
在空白列中添加不是很优雅,阶段下方的细线也不好。我在想必须有更好的方法来做到这一点....
有人可以建议我如何在阶段组之间添加一些填充吗?我是 flextable 的新手。因此,对于学习教程的建议也将不胜感激。
我会做:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
library(scales)
library(flextable)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(`histo` = " ") %>%
select(time, histo, `1_1`, `2_1`, `3_1`, everything())
my_header <- data.frame(
col_keys = c("time", "histo", "blank1", "1_1", "2_1", "3_1", "blank2", "1_2", "2_2", "3_2", "blank3", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", "", rep("I", 3), "", rep("II", 3), "", rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", rep(c("", "1", "2", "3"), 3))
)
flextable(cancerCountWide, col_keys = my_header$col_keys) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks() %>%
fix_border_issues()
确保 my_header
包含所有 col_keys
描述并使用 col_keys
参数获取空白列(如果 col_keys
之一在原始数据集中不存在,则fake/blank 列)。
> my_header
col_keys line2 line3
1 time Follow-up Follow-up
2 histo Histology Disease Stage
3 blank1
4 1_1 I 1
5 2_1 I 2
6 3_1 I 3
7 blank2
8 1_2 II 1
9 2_2 II 2
10 3_2 II 3
11 blank3
12 1_3 III 1
13 2_3 III 2
14 3_3 III 3
您可以在此处找到文档:https://davidgohel.github.io/flextable/
我有一个包含嵌套列标题的意外事件 table,我需要帮助在列标题之间添加 space。共有三种组织学,每种组织学内部都有三个阶段。我想在三组舞台之间休息。我走到这一步:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(blank = " ") %>%
mutate(blank2 = " ") %>%
select(time, blank, `1_1`, `2_1`, `3_1`, blank2, everything())
my_header <- data.frame(
col_keys = c("time", "blank", "1_1", "2_1", "3_1", "blank", "1_2", "2_2", "3_2", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", rep("I", 3), "blank", rep("II", 3), rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", c(1,2,3), "blank", rep(c(1,2,3),2))
)
library(flextable)
flextable(cancerCountWide) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks()
这让我很接近:
在空白列中添加不是很优雅,阶段下方的细线也不好。我在想必须有更好的方法来做到这一点....
有人可以建议我如何在阶段组之间添加一些填充吗?我是 flextable 的新手。因此,对于学习教程的建议也将不胜感激。
我会做:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
library(scales)
library(flextable)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(`histo` = " ") %>%
select(time, histo, `1_1`, `2_1`, `3_1`, everything())
my_header <- data.frame(
col_keys = c("time", "histo", "blank1", "1_1", "2_1", "3_1", "blank2", "1_2", "2_2", "3_2", "blank3", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", "", rep("I", 3), "", rep("II", 3), "", rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", rep(c("", "1", "2", "3"), 3))
)
flextable(cancerCountWide, col_keys = my_header$col_keys) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks() %>%
fix_border_issues()
确保 my_header
包含所有 col_keys
描述并使用 col_keys
参数获取空白列(如果 col_keys
之一在原始数据集中不存在,则fake/blank 列)。
> my_header
col_keys line2 line3
1 time Follow-up Follow-up
2 histo Histology Disease Stage
3 blank1
4 1_1 I 1
5 2_1 I 2
6 3_1 I 3
7 blank2
8 1_2 II 1
9 2_2 II 2
10 3_2 II 3
11 blank3
12 1_3 III 1
13 2_3 III 2
14 3_3 III 3
您可以在此处找到文档:https://davidgohel.github.io/flextable/