You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用R语言forestplot包为labeltext按分类设置不同颜色?

实现forestplot按分类设置标签颜色

要实现按发酵菌/致病菌给标签设置蓝红两色,核心是利用forestplot的txt_gp参数,给每行标签指定对应颜色,具体步骤如下:

  1. 给数据框添加颜色标记列
    在合并后的output_df中,根据taxa所属类别(发酵菌/致病菌/表头)添加颜色列:
output_df <- output_df %>%
  mutate(
    label_color = case_when(
      summary ~ "black",  # 表头行用黑色
      taxa %in% fermenters ~ "blue",  # 发酵菌设为蓝色
      taxa %in% pathogens ~ "red",    # 致病菌设为红色
      TRUE ~ "black"       # 兜底默认黑色
    )
  )
  1. 修改forestplot的txt_gp参数
    将txt_gp里的label参数改为接受颜色向量的gpar对象,把刚才的label_color列传入:
output_df %>% 
  forestplot(labeltext = c(taxa, estimate, p_value, ci_range),
             is.summary = summary,
             xlab = "95% Confidence Interval",
             txt_gp = fpTxtGp(title = gpar(cex=1.35),
                              ticks = gpar(cex=0.75),
                              xlab = gpar(cex = 1),
                              # 关键:用label_color列指定每行文本颜色
                              label = gpar(fontfamily="", cex=1.15, col = output_df$label_color)),
             hrzl_lines = list("2" = gpar(lty = 2), "7" = gpar(lwd = 1, columns = 1:3, col = "#000044")), 
             boxsize = 0.15, 
             alpha = 0.75)

完整可运行代码

library(tibble)
library(dplyr)
library(forestplot)

fermenters <- c("Taxon_A", "Taxon_B")
pathogens <- c("Taxon_C", "Taxon_D", "Taxon_E")

Mean <- c(0.25, -0.10, 0.35, 0.05, -0.20)
Lower <- c(0.10, -0.20, 0.15, -0.05, -0.30)
Upper <- c(0.40, 0.00, 0.55, 0.15, -0.10)
names_med_out <- c("Taxon_A", "Taxon_B", "Taxon_C", "Taxon_D", "Taxon_E")
Estimate <- c(0.3, -0.15, 0.4, 0.1, -0.25)
P_value <- c("0.03", "0.15", "0.01", "0.50", "0.10")

output_base_data <- tibble(mean = Mean,
                           lower = Lower,
                           upper = Upper,
                           taxa = names_med_out,
                           estimate = as.character(Estimate),
                           p_value = as.character(P_value),
                           ci_range = paste0("[", sprintf("%.3f", Lower), ", ", sprintf("%.3f", Upper), "]"))

output_header <- tibble(taxa = "Taxon",
                        estimate = "Estimate",
                        p_value = "P-value",
                        ci_range = "CI [Lower-Upper]",
                        summary = TRUE)

output_df <- bind_rows(output_header, output_base_data)

# 添加颜色标记列
output_df <- output_df %>%
  mutate(
    label_color = case_when(
      summary ~ "black",
      taxa %in% fermenters ~ "blue",
      taxa %in% pathogens ~ "red",
      TRUE ~ "black"
    )
  )

# 绘制带颜色标签的森林图
output_df %>% 
  forestplot(labeltext = c(taxa, estimate, p_value, ci_range),
             is.summary = summary,
             xlab = "95% Confidence Interval",
             txt_gp = fpTxtGp(title = gpar(cex=1.35),
                              ticks = gpar(cex=0.75),
                              xlab = gpar(cex = 1),
                              label = gpar(fontfamily="", cex=1.15, col = output_df$label_color)),
             hrzl_lines = list("2" = gpar(lty = 2), "7" = gpar(lwd = 1, columns = 1:3, col = "#000044")), 
             boxsize = 0.15, 
             alpha = 0.75)

内容的提问来源于stack exchange,提问作者Seda Koldas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 21:53:16