Loading... # forestploter ## install ```R library(forestploter) ``` ## format  参照以上表格,必须的列包括: 1. Exposure:包括暴露的名字、MR方法等 2. OR(95% CI):使用or列、lo_ci、up_ci列生成 3. snps、P-Value 4. se.or、lo_ci、up_ci、or:用于绘图 ## Example ```R setwd("/Users/luojy/同步/学位论文/2.MR筛选出来的各暴露VSAS结果/") library(dplyr) tmp <- readxl::read_excel("1.MR显著.xlsx", col_types = c("text","text","text","text","numeric","text","text","text","text","text","text","text","text","numeric","numeric","numeric","numeric","numeric","numeric","numeric","text","text","numeric","numeric")) lipid_res <- tmp %>% dplyr::select(filename, exposure_factor_abbr, exposure_factor_class,exposure_factor_main_class, exposure_factor_sub_class,method,nsnp, b.or, se.or, pval.or, or, lo_ci, up_ci,N) %>% dplyr::filter(exposure_factor_class == 'Lipid') %>% unique() %>% group_by(exposure_factor_abbr) %>% dplyr::filter(!(method == "TwoSampleMR" & "Inverse variance weighted" %in% method[exposure_factor_abbr == .data$exposure_factor_abbr])) %>% dplyr::mutate(method = ifelse(method == "TwoSampleMR", "Inverse variance weighted", method)) %>% ungroup() ## Lipid中,IVW下的脂质森林图,按照exposure_factor_main_class和exposure_factor_sub_class脂质类型分类 library(forestploter) tibble_insert_row <- function(df, row, pos) { if (pos > nrow(df)) { return(rbind(df, row)) } if (pos == 1) { return(rbind(row, df)) } return(rbind(df[1:(pos-1),], row, df[pos:nrow(df),])) } split_forest_data_by_singletitle <- function(data, input_string) { # 查找匹配行的索引 start_index <- which(data$Exposure == input_string) # 初始化结束索引为数据框的行数 end_index <- nrow(data) # 查找下一个同级别分类的索引作为结束索引 for (i in (start_index + 1):nrow(data)) { # 如果找到下一个主分类,则更新结束索引并停止循环 if (data$Exposure[i] != "" & substr(data$Exposure[i], 1, 2) != " ") { end_index <- i - 1 break } } # 提取匹配行及其子项 return(data[start_index:end_index, ]) } split_forest_data_by_title <- function(data, input_string) { for (i in 1:length(input_string)){ sub_data <- split_forest_data_by_singletitle(data, input_string[i]) if (i == 1){ result <- sub_data }else{ result <- rbind(result, sub_data) } } return(result) } forestplot_by_title <- function(data, title,pdf_output = NULL) { # 提取匹配行及其子项 sub_data <- split_forest_data_by_title(data, title) # 提取OR和CI ci <- sub_data %>% dplyr::select(or, lo_ci, up_ci, se.or) # 提取需要的列 sub_data <- sub_data %>% dplyr::select(Exposure, method, snps, `OR(95% CI)`, ` `, `P-Value`) # 绘制森林图 p <- forest(sub_data, est = as.numeric(ci$or), #效应值 lower = as.numeric(ci$lo_ci), #可信区间下限 upper = as.numeric(ci$up_ci), #可信区间上限 sizes = as.numeric(ci$se.or), ci_column = 5, #在那一列画森林图,要选空的那一列 ref_line = 1, arrow_lab = c("Negtive", "Posive"), xlim = c(0.8, 1.2), ticks_at = c(0.8, 1, 1.2),) if (is.null(pdf_output)){ message("No pdf output") print(p) } else { if(nrow(sub_data)<10){ height <- nrow(sub_data) * 0.5 } else if (nrow(sub_data)<5){ height <- 5 } else { height <- nrow(sub_data) * 0.3 } pdf(pdf_output, width = 12, height = height) print(p) dev.off() } } forest_data <- lipid_res %>% dplyr::arrange(exposure_factor_main_class, exposure_factor_sub_class) %>% dplyr::select(exposure_factor_main_class,method, exposure_factor_sub_class,N, or,pval.or, se.or,nsnp,exposure_factor_abbr) %>% dplyr::mutate(exposure_factor_abbr = paste0(" ",exposure_factor_abbr)) %>% dplyr::mutate(title = paste0(exposure_factor_main_class,": ",exposure_factor_sub_class)) %>% dplyr::mutate(lo_ci = or-se.or*1.96) %>% dplyr::mutate(up_ci = or+se.or*1.96) %>% dplyr::mutate(`95%CI` = paste(sprintf("%.2f (%.2f to %.2f)",or, lo_ci,up_ci))) %>% dplyr::mutate(pval.or = round(as.numeric(pval.or), 4)) %>% dplyr::select(exposure_factor_main_class, exposure_factor_sub_class, method, N , `95%CI`, or, pval.or, se.or,lo_ci,up_ci,nsnp, exposure_factor_abbr, title) tmp <- forest_data %>% dplyr::select(title) %>% unique() %>% dplyr::mutate(first_occurrences = sapply(., function(x) match(x, forest_data$title))) forest_data_tmp <- forest_data for (i in nrow(tmp):1){ forest_data_tmp <- forest_data_tmp %>% tibble_insert_row(c(rep('',11),tmp[i,1][[1]][[1]],rep('',ncol(forest_data)-11-1)), tmp[i,2][[1]][[1]]) } forest_data_tmp <- forest_data_tmp %>% dplyr::mutate(" "=paste(rep(" ",15),collapse = " ")) ci <- forest_data_tmp %>% dplyr::select(or,lo_ci, up_ci, se.or) forest_data_tmp <- forest_data_tmp %>% dplyr::rename(Exposure = exposure_factor_abbr, snps = nsnp, `OR(95% CI)` = `95%CI`, `P-Value` = pval.or) title_list <- list( list("Fatty Acyls",c("Fatty Acyls: Dicarboxylic acids","Fatty Acyls: FA","Fatty Acyls: Fatty acyl carnitines","Fatty Acyls: Heterocyclic fatty acids","Fatty Acyls: Hydroxy fatty acids","Fatty Acyls: N-acyl amino acids","Fatty Acyls: UFA")), list("GL",c("GL: DG","GL: TG")), list("PL",c("PL: LPC","PL: LPE","PL: LPI","PL: PC","PL: PE","PL: PI","PL: PS","PL: aLPC","PL: aPC","PL: aPE","PL: pLPC","PL: pLPE","PL: pPC","PL: pPE")), list("PR",c("PR: Isoprenoids")), list("SL",c("SL: Cer","SL: GM3","SL: Hex2Cer","SL: Hex3Cer","SL: HexCer","SL: Phosphonosphingolipids","SL: SM")), list("Sterols",c("Sterols: Bile acids and derivatives","Sterols: CE","Sterols: Cholesterol","Sterols: Cholesterol and derivatives","Sterols: Ergosterols and C24-methyl derivatives","Sterols: Steroids","Sterols: Taurine conjugates")) ) for (i in title_list){ pdf_file_path <- paste0(i[[1]],".pdf") p <- forestplot_by_title(forest_data_tmp,i[[2]],pdf_output = pdf_file_path) print(p) dev.off() } ``` 如图:  最后修改:2024 年 03 月 01 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏
2 条评论
文化底蕴深厚,引经据典信手拈来。
文字流畅如丝,语言优美动人,读来令人心旷神怡。