R语言 多个子组的汇总和绘图(更多列)

nszi6y05  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(131)

我对两件事感兴趣:1)同一表中多个子组的总结; 2)基于步骤1中生成的总结的子组的点图。
例如,
如果这是我数据集

data("pbc")

我喜欢按sexstageascitesspiders生成两个治疗水平1、2的胆固醇总结(chol

table(pbc$trt)
     1    2   
     158  154

我可以像这样分开做。

library(Hmisc)
     
     summary(chol ~ sex + stage + ascites + spiders, data = subset(pbc, trt=1))
     summary(chol ~ sex + stage + ascites + spiders, data = subset(pbc, trt=2))

这将创建两个单独的摘要。
两个不同的对应图

plot(summary(chol ~ sex + stage + ascites + spiders, data = subset(pbc, trt=1)))

plot(summary(chol ~ sex + stage + ascites + spiders, data = subset(pbc, trt=2)))

我希望摘要在一个表中,两列1列表示trt=1,第2列表示trt=2
| | | 数量|胆固醇(给药组= 1)|胆固醇(给药组= 2)|
| - ------| - ------| - ------| - ------| - ------|
| 性|米|......| - -| - -|
| | f级|......| - -| - -|
并排绘图。第一张图用于trt = 1,第二张图用于trt = 2

请建议如何缩放Hmisc:::summary.公式,摘要函数,以1)并排显示子组摘要2)并排绘制摘要。谢谢。

rjee0c15

rjee0c151#

请注意,您当前的总结和图是相同的;尽管使用subsettrt的两个水平,但您发布的两个图是相同的。您可以使用filter来确定地按trt的水平进行过滤。
首先,我更喜欢使用gtsummary来处理我的表,因为你可以使用tbl_continuous来创建一个单独的表,而不是试图合并两个表;其次,你可能会遇到合并两个图的困难,因为你在Hmisc汇总对象上使用了基于R的绘图函数;甚至试图将每个图保存到一个对象也会导致NULL;从长远来看,使用ggplot并与cowplot::plot_grid组合可以更容易地重新创建每个图。

library(survival)
library(Hmisc)

# create combined summary
library(gtsummary)
library(tidyverse)

data(pbc)
df <- pbc %>%
  select(id, trt, chol, sex, stage, ascites, spiders) %>%
  mutate(across(c(sex, stage, ascites, spiders), as.factor)) %>%
  mutate(trt = factor(trt)) %>%
  mutate(chol = as.numeric(chol))

dftrt1 <- df %>% filter(trt == 1)
dftrt2 <- df %>% filter(trt == 2)

df %>%
  select(trt, chol, sex, stage, ascites, spiders) %>%
  tbl_continuous(variable = chol,
                 digits = everything() ~ 2,
                 statistic = everything() ~ "{mean}",
                 label = list(sex ~ "Sex", 
                              stage ~ "Stage", 
                              ascites ~ "Ascites", 
                              spiders ~ "Spiders"),
                 by = trt)

# create combined plot
library(cowplot)
p1 <- dftrt1 %>%
  select(-trt) %>% pivot_longer(cols = -c(id, chol)) %>% group_by(name, value) %>%
  summarise(chol = mean(chol, na.rm = TRUE)) %>%
  ggplot(aes(x = value, y = chol, fill = factor(value))) + 
  geom_point() + coord_flip() +
  facet_wrap(~name, scales = "free_y", nrow = 4, strip.position = "top") +
  theme(panel.spacing = unit(0, "lines"),
          panel.border = element_rect(fill = NA),
          strip.background = element_blank(),
          axis.title.y = element_blank(),
          legend.position = "none",
          strip.placement = "outside") +
  ggtitle("trt = 1") + theme(plot.title = element_text(hjust = 0.5))

p2 <- dftrt2 %>%
  select(-trt) %>% pivot_longer(cols = -c(id, chol)) %>% group_by(name, value) %>%
  summarise(chol = mean(chol, na.rm = TRUE)) %>%
  ggplot(aes(x = value, y = chol, fill = factor(value))) + 
  geom_point() + coord_flip() +
  facet_wrap(~name, scales = "free_y", nrow = 4, strip.position = "top") +
  theme(panel.spacing = unit(0, "lines"),
        panel.border = element_rect(fill = NA),
        strip.background = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none",
        strip.placement = "outside") +
  ggtitle("trt = 2") + theme(plot.title = element_text(hjust = 0.5))

plot_grid(p1, p2, ncol = 2)

相关问题