繁体   English   中英

在 R flextable 中,如何在列标题之间添加分隔符

[英]In R flextable how can I add a break between column headings

我有一个列标题嵌套的列联表,我需要帮助在列标题之间添加空格。 有三种组织学,每个组织学内部都有三个阶段。 我想在三组阶段的每一组之间休息。 我走了这么远:

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之一,则它是假/空白列)。

> 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM