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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容