8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

如何使用 R forestplot 包更改标签文本颜色?

chaos 2月前

25 0

作为示例,我将使用 R forestplot 的合成数据。我想根据 output_df$taxa 的发酵罐/病原体条件分别将 labeltext 的变量着色为蓝色和红色。我

作为示例,我将使用 R forestplot 的合成数据。我想根据发酵罐/病原体条件分别将 labeltext 的变量着色为蓝色和红色。 output_df$taxa 如果有人能告诉我该怎么做,我将不胜感激!

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)
帖子版权声明 1、本帖标题:如何使用 R forestplot 包更改标签文本颜色?
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由chaos在本站《r》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您可以 通过如下方式更改 颜色 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
    
    

    forest plot with colored row texts

返回
作者最近主题: