作为示例,我将使用 R 森林图的合成数据。我想根据
output_df$taxa
的发酵罐/病原体条件将labeltext的变量分别着色为蓝色和红色。如果有人能给我一个想法如何去做,我将非常感激!
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 %>%
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)),
hrzl_lines = list("2" = gpar(lty = 2), "7" = gpar(lwd = 1, columns = 1:3, col = "#000044")), boxsize = 0.15, alpha = 0.75)
您可以通过像这样更改your_plot$col$text
来为
行着色(使用您的数据):
## store plot:
the_plot <- output_df %>%
forestplot(...)
## determine row colors:
row_colors <- ifelse(output_df$taxa %in% fermenters, 'blue', 'green')
row_colors[1] <- 'black' ## for title row
## assign row colors to plot object:
the_plot$col$text <- label_colors
## plot:
the_plot