生成漂亮的 table 后,有没有办法在 gtsummary 中重新调整变量?
Is there a way to relevel a variable in gtsummary after generating the beautiful table?
我正在尝试在创建 table 后重新调整 gtsummary 对象中特定变量的级别。默认情况下,参考级别位于顶部(正如预期的那样),但有时您会想要特定的顺序。为此 table 我希望 agecat“61-83”被推下。我用 modify_table 试过了,但我明白了
relevel 错误(agecat,ref =“0-20”):未找到对象 'agecat'
library(gtsummary)
library(dplyr)
data(trial)
trial <- trial %>%
mutate(agecat = case_when(between(age, 0, 20) ~ "0-20",
between(age, 21, 40) ~ "21-40",
between(age, 41, 60) ~ "41-60",
between(age, 61, 83) ~ "61-83"),
agecat = as.factor(agecat),
agecat = relevel(agecat, ref = "61-83"))
glm(response ~ trt + grade + agecat,
data = trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE)
有两种方法可以解决这个问题:
- gtsummary table 的打印本质上是一个数据框,带有使其美观的格式选项。您可以使用
modify_table_body()
和 arrange()
函数对基础数据框进行排序。
library(gtsummary)
library(tidyverse)
packageVersion("gtsummary")
trial2 <-
trial %>%
mutate(
agecat = case_when(
between(age, 0, 20) ~ "0-20",
between(age, 21, 40) ~ "21-40",
between(age, 41, 60) ~ "41-60",
between(age, 61, 83) ~ "61-83"
) %>%
factor(),
)
glm(response ~ trt + grade + agecat,
data = trial2 %>% mutate(agecat = relevel(agecat, ref = "61-83")),
family = binomial) %>%
tbl_regression(exponentiate = TRUE) %>%
modify_table_body(
~ .x %>%
arrange(variable, desc(row_type == "label"), label == "61-83")
)
- 第二种方法是使用
contrasts()
指定应引用哪个组。 tbl_regression()
保留顺序。
contrasts(trial2$agecat) <-
contr.treatment(levels(trial2$agecat),
base = which(levels(trial2$agecat) == "61-83")
)
glm(response ~ trt + grade + agecat,
data = trial2,
family = binomial) %>%
tbl_regression(exponentiate = TRUE)
我正在尝试在创建 table 后重新调整 gtsummary 对象中特定变量的级别。默认情况下,参考级别位于顶部(正如预期的那样),但有时您会想要特定的顺序。为此 table 我希望 agecat“61-83”被推下。我用 modify_table 试过了,但我明白了 relevel 错误(agecat,ref =“0-20”):未找到对象 'agecat'
library(gtsummary)
library(dplyr)
data(trial)
trial <- trial %>%
mutate(agecat = case_when(between(age, 0, 20) ~ "0-20",
between(age, 21, 40) ~ "21-40",
between(age, 41, 60) ~ "41-60",
between(age, 61, 83) ~ "61-83"),
agecat = as.factor(agecat),
agecat = relevel(agecat, ref = "61-83"))
glm(response ~ trt + grade + agecat,
data = trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE)
有两种方法可以解决这个问题:
- gtsummary table 的打印本质上是一个数据框,带有使其美观的格式选项。您可以使用
modify_table_body()
和arrange()
函数对基础数据框进行排序。
library(gtsummary)
library(tidyverse)
packageVersion("gtsummary")
trial2 <-
trial %>%
mutate(
agecat = case_when(
between(age, 0, 20) ~ "0-20",
between(age, 21, 40) ~ "21-40",
between(age, 41, 60) ~ "41-60",
between(age, 61, 83) ~ "61-83"
) %>%
factor(),
)
glm(response ~ trt + grade + agecat,
data = trial2 %>% mutate(agecat = relevel(agecat, ref = "61-83")),
family = binomial) %>%
tbl_regression(exponentiate = TRUE) %>%
modify_table_body(
~ .x %>%
arrange(variable, desc(row_type == "label"), label == "61-83")
)
- 第二种方法是使用
contrasts()
指定应引用哪个组。tbl_regression()
保留顺序。
contrasts(trial2$agecat) <-
contr.treatment(levels(trial2$agecat),
base = which(levels(trial2$agecat) == "61-83")
)
glm(response ~ trt + grade + agecat,
data = trial2,
family = binomial) %>%
tbl_regression(exponentiate = TRUE)