美文网首页R语言 生信R语言学习
R语言可视化(三十八九):森林图绘制

R语言可视化(三十八九):森林图绘制

作者: Davey1220 | 来源:发表于2020-12-07 20:18 被阅读0次

39. 森林图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/39forest/")

使用survminer包中的ggforest函数绘制森林图

require("survival")
## Loading required package: survival
library(survminer)
## Warning: package 'survminer' was built under R version 3.6.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.3
## Loading required package: ggpubr
## Loading required package: magrittr

# 查看内置示例数据
head(colon)
##   id study      rx sex age obstruct perfor adhere nodes status differ
## 1  1     1 Lev+5FU   1  43        0      0      0     5      1      2
## 2  1     1 Lev+5FU   1  43        0      0      0     5      1      2
## 3  2     1 Lev+5FU   1  63        0      0      0     1      0      2
## 4  2     1 Lev+5FU   1  63        0      0      0     1      0      2
## 5  3     1     Obs   0  71        0      0      1     7      1      2
## 6  3     1     Obs   0  71        0      0      1     7      1      2
##   extent surg node4 time etype
## 1      3    0     1 1521     2
## 2      3    0     1  968     1
## 3      3    0     0 3087     2
## 4      3    0     0 3087     1
## 5      2    0     1  963     2
## 6      2    0     1  542     1

# 构建COX回归比例风险模型
model <- coxph( Surv(time, status) ~ sex + rx + adhere,
                data = colon )
# 查看cox回归模型结果
model
## Call:
## coxph(formula = Surv(time, status) ~ sex + rx + adhere, data = colon)
## 
##               coef exp(coef) se(coef)      z        p
## sex       -0.04615   0.95490  0.06609 -0.698 0.484994
## rxLev     -0.02724   0.97313  0.07690 -0.354 0.723211
## rxLev+5FU -0.43723   0.64582  0.08395 -5.208 1.91e-07
## adhere     0.29355   1.34118  0.08696  3.376 0.000736
## 
## Likelihood ratio test=46.51  on 4 df, p=1.925e-09
## n= 1858, number of events= 920

# 使用ggforest()函数绘制基础森林图
ggforest(model)
image.png
# 将数据集中的变量设置成因子,添加标签进行分组
colon <- within(colon, {
  sex <- factor(sex, labels = c("female", "male"))
  differ <- factor(differ, labels = c("well", "moderate", "poor"))
  extent <- factor(extent, labels = c("submuc.", "muscle", "serosa", "contig."))
})
head(colon)
##   id study      rx    sex age obstruct perfor adhere nodes status   differ
## 1  1     1 Lev+5FU   male  43        0      0      0     5      1 moderate
## 2  1     1 Lev+5FU   male  43        0      0      0     5      1 moderate
## 3  2     1 Lev+5FU   male  63        0      0      0     1      0 moderate
## 4  2     1 Lev+5FU   male  63        0      0      0     1      0 moderate
## 5  3     1     Obs female  71        0      0      1     7      1 moderate
## 6  3     1     Obs female  71        0      0      1     7      1 moderate
##   extent surg node4 time etype
## 1 serosa    0     1 1521     2
## 2 serosa    0     1  968     1
## 3 serosa    0     0 3087     2
## 4 serosa    0     0 3087     1
## 5 muscle    0     1  963     2
## 6 muscle    0     1  542     1

# 使用coxph()函数进行COX回归分析
bigmodel <- coxph(Surv(time, status) ~ sex + rx + adhere + differ + extent + node4,
                  data = colon )
bigmodel
## Call:
## coxph(formula = Surv(time, status) ~ sex + rx + adhere + differ + 
##     extent + node4, data = colon)
## 
##                    coef exp(coef) se(coef)      z        p
## sexmale        -0.03226   0.96825  0.06719 -0.480  0.63111
## rxLev          -0.04495   0.95605  0.07847 -0.573  0.56681
## rxLev+5FU      -0.45153   0.63665  0.08467 -5.333 9.65e-08
## adhere          0.18409   1.20212  0.09079  2.028  0.04259
## differmoderate -0.06258   0.93934  0.11625 -0.538  0.59037
## differpoor      0.27941   1.32235  0.13422  2.082  0.03737
## extentmuscle    0.21074   1.23459  0.35588  0.592  0.55374
## extentserosa    0.74471   2.10583  0.33736  2.207  0.02728
## extentcontig.   1.08395   2.95634  0.36664  2.956  0.00311
## node4           0.83820   2.31219  0.06940 12.078  < 2e-16
## 
## Likelihood ratio test=246  on 10 df, p=< 2.2e-16
## n= 1812, number of events= 899 
##    (46 observations deleted due to missingness)

ggforest(bigmodel,
         main = "Hazard ratio", # 设置标题
         cpositions = c(0.08, 0.2, 0.35), # 设置前三列的相对距离
         fontsize = 0.8, # 设置字体大小
         refLabel = "reference",
         noDigits = 2) #设置保留小数点位数
image.png

使用forestplot包绘制森林图

# 安装并加载所需的R包
#install.packages("forestplot")
library(forestplot)
## Warning: package 'forestplot' was built under R version 3.6.3
## Loading required package: grid
## Loading required package: checkmate
## Warning: package 'checkmate' was built under R version 3.6.1

# 构建示例数据
cochrane_from_rmeta <- data.frame(
    mean  = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531), 
    lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
    upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731))

tabletext <-cbind(
  c("", "Study", "Auckland", "Block", 
    "Doran", "Gamsu", "Morrison", "Papageorgiou", 
    "Tauesch", NA, "Summary"),
  c("Deaths", "(steroid)", "36", "1", 
    "4", "14", "3", "1", 
    "8", NA, NA),
  c("Deaths", "(placebo)", "60", "5", 
    "11", "20", "7", "7", 
    "10", NA, NA),
  c("", "OR", "0.58", "0.16", 
    "0.25", "0.70", "0.35", "0.14", 
    "1.02", NA, "0.53"))

# 查看示例数据
head(cochrane_from_rmeta)
##    mean lower upper
## 1    NA    NA    NA
## 2    NA    NA    NA
## 3 0.578 0.372 0.898
## 4 0.165 0.018 1.517
## 5 0.246 0.072 0.833
## 6 0.700 0.333 1.474

head(tabletext)
##      [,1]       [,2]        [,3]        [,4]  
## [1,] ""         "Deaths"    "Deaths"    ""    
## [2,] "Study"    "(steroid)" "(placebo)" "OR"  
## [3,] "Auckland" "36"        "60"        "0.58"
## [4,] "Block"    "1"         "5"         "0.16"
## [5,] "Doran"    "4"         "11"        "0.25"
## [6,] "Gamsu"    "14"        "20"        "0.70"

# 使用forestplot()函数绘制基础森林图
forestplot(labeltext = tabletext, 
           mean = cochrane_from_rmeta$mean,
           lower = cochrane_from_rmeta$lower ,
           upper = cochrane_from_rmeta$upper)
image.png
# 添加一些参数美化森林图
forestplot(tabletext, 
           cochrane_from_rmeta,
           # 添加水平线
           hrzl_lines = list("1" = gpar(lty=2, lwd=2, col="black"), 
                             "3" = gpar(lty=2, lwd=2, col="black"),
                             "11" = gpar(lwd=1, columns=1:4, col = "red")),
           align = "c", # 设置左边表格中字体的对齐方式
           zero = 1, # 设置zero line的位置
           title="Hazard Ratio Plot", # 设置标题
           new_page = TRUE,
           is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE), #A vector indicating by TRUE/FALSE if the value is a summary value which means that it will have a different font-style
           clip=c(0.2,2.5), #Lower and upper limits for clipping confidence intervals to arrows
           xlog=TRUE,
           xticks.digits = 2,
           col=fpColors(box="royalblue",line="darkblue", 
                        summary="royalblue", hrz_lines = "#444444"),
           vertices = TRUE)
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] forestplot_1.10   checkmate_1.9.4   survminer_0.4.8   ggpubr_0.2.1     
## [5] magrittr_1.5      ggplot2_3.3.2     survival_2.44-1.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5        pillar_1.4.2      compiler_3.6.0   
##  [4] tools_3.6.0       digest_0.6.20     nlme_3.1-139     
##  [7] evaluate_0.14     tibble_2.1.3      lifecycle_0.2.0  
## [10] gtable_0.3.0      lattice_0.20-38   pkgconfig_2.0.2  
## [13] rlang_0.4.7       Matrix_1.2-17     yaml_2.2.0       
## [16] xfun_0.8          gridExtra_2.3     withr_2.1.2      
## [19] stringr_1.4.0     dplyr_1.0.2       knitr_1.23       
## [22] survMisc_0.5.5    generics_0.0.2    vctrs_0.3.2      
## [25] cowplot_0.9.4     tidyselect_1.1.0  data.table_1.12.2
## [28] glue_1.4.2        KMsurv_0.1-5      R6_2.4.0         
## [31] km.ci_0.5-2       rmarkdown_1.13    tidyr_1.1.2      
## [34] purrr_0.3.2       backports_1.1.4   scales_1.0.0     
## [37] htmltools_0.3.6   splines_3.6.0     xtable_1.8-4     
## [40] colorspace_1.4-1  ggsignif_0.5.0    labeling_0.3     
## [43] stringi_1.4.3     munsell_0.5.0     broom_0.5.2      
## [46] crayon_1.3.4      zoo_1.8-6

相关文章

网友评论

    本文标题:R语言可视化(三十八九):森林图绘制

    本文链接:https://www.haomeiwen.com/subject/ejotgktx.html