如何移除finalfit绘制的logistic回归森林图OR及CI数值?或求替代代码
解决finalfit森林图移除OR/CI数值的方案
方案1:直接修改finalfit生成的ggplot对象
finalfit的or_plot()返回的是ggplot对象,你可以直接移除负责显示OR和CI数值的图层:
# 生成原图 TT1 <- PCCS %>% or_plot(dep1, exp1) # 移除文本图层(通常是GeomText类型) TT1_clean <- TT1 %>% remove_layer(which(purrr::map_lgl(., ~"GeomText" %in% class(.)))) # 查看清理后的图 TT1_clean
如果不确定图层位置,可先执行TT1$layers查看所有图层,找到对应文本图层的索引后直接删除,比如TT1$layers[[3]] <- NULL(需根据实际索引调整)。
方案2:用ggplot2手动绘制森林图
如果finalfit的定制性无法满足需求,可以直接基于回归结果用ggplot2手动绘图,完全控制元素显示:
# 1. 提取多因素logistic回归的OR、置信区间等结果 fit_result <- PCCS %>% finalfit(dep1, exp1) %>% extract_fit() %>% broom::tidy(exponentiate = TRUE, conf.int = TRUE) # 2. 整理绘图数据,调整自变量顺序 plot_data <- fit_result %>% mutate(term = forcats::fct_reorder(term, estimate)) # 3. 绘制不带OR/CI数值的森林图 ggplot(plot_data, aes(x = estimate, y = term)) + geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") + geom_point(size = 3, color = "#2c3e50") + geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0.2, color = "#2c3e50") + labs(x = "比值比 (OR)", y = "自变量", title = "多因素Logistic回归森林图") + theme_bw()
方案3:使用forestplot包绘制
forestplot包专门用于森林图绘制,定制性强,可轻松隐藏数值列:
# 1. 提取并整理回归结果 fit_table <- PCCS %>% finalfit(dep1, exp1) %>% select(variable, OR, `95% CI`) # 2. 拆分置信区间的上下限 fit_table <- fit_table %>% mutate( ci_low = stringr::str_extract(`95% CI`, "^\\d+\\.\\d+") %>% as.numeric(), ci_high = stringr::str_extract(`95% CI`, "\\d+\\.\\d+$") %>% as.numeric() ) # 3. 绘制不带OR/CI数值的森林图 forestplot::forestplot( labeltext = fit_table$variable, mean = fit_table$OR, lower = fit_table$ci_low, upper = fit_table$ci_high, graph.pos = 2, xlab = "比值比 (OR)", # 隐藏右侧数值显示 txt_gp = forestplot::fpTxtGp(cex = 1), col = forestplot::fpColors(box = "#3498db", line = "#2980b9") )
内容的提问来源于stack exchange,提问作者gehan harb
相关产品推荐
相关产品推荐

