生信绘图那些年画图的简便操作good code

R语言绘图包08--森林图的绘制:forestplot

2022-09-14  本文已影响0人  Hayley笔记

R语言绘图包系列:


1. 基本用法和参数
forestplot(...)
# S3 method for data.frame
forestplot(x, mean, lower, upper, labeltext, is.summary, ...)

# S3 method for default
forestplot(
  labeltext,
  mean,
  lower,
  upper,
  align,
  is.summary = FALSE,
  graph.pos = "right",
  hrzl_lines,
  clip = c(-Inf, Inf),
  xlab = "",
  zero = ifelse(xlog, 1, 0),
  graphwidth = "auto",
  colgap,
  lineheight = "auto",
  line.margin,
  col = fpColors(),
  txt_gp = fpTxtGp(),
  xlog = FALSE,
  xticks,
  xticks.digits = 2,
  grid = FALSE,
  lwd.xaxis,
  lwd.zero,
  lwd.ci,
  lty.ci = 1,
  ci.vertices,
  ci.vertices.height = 0.1,
  boxsize,
  mar = unit(rep(5, times = 4), "mm"),
  title,
  legend,
  legend_args = fpLegend(),
  new_page = getOption("forestplot_new_page", TRUE),
  fn.ci_norm = fpDrawNormalCI,
  fn.ci_sum = fpDrawSummaryCI,
  fn.legend,
  shapes_gp = fpShapesGp(),
  ...
)

# S3 method for gforge_forestplot
print(x, ...)

# S3 method for gforge_forestplot
plot(x, y, ...)

# S3 method for grouped_df
forestplot(x, labeltext, mean, lower, upper, legend, is.summary, ...)
参数 用法
labeltext 主要是以矩阵或者list形式将数据导入函数,最好以矩阵,因为数据一般都是矩阵的。
mean 均值
lower 95%置信区间下限
upper 95%置信区间上限
align 每列文字的对齐方式,偶尔会用到。如:align=c("l","c","c") l:左对齐r:右对齐c:居中对齐
is.summary 主要的功能是让表格的每一行字体出现差异,从而区分表头。其值主要用TRUE/FALSE进行差异化分配。
graph.pos 定位森林图所在的位置。通过数字来确定为第几列。
hrzl_lines 以list形式设置表中线条的类型、影响范围。Eg:“3”=gpar(lwd=1,columns=1:4,col=’red’)意思就是第3行的线条,宽度为1,线段延伸至第四列。Col指的颜色。
clip x轴的最大最小范围
xlab x轴的标题
zero 森林图中基准线的位置(无效线的横坐标)
graphwidth 森林图在表中的宽度如:graphwidth = unit(.4,"npc")
colgap 列与列之间的间隙宽度,默认是 6 mm,需要用 unit 的形式
lineheight 行的高度,可以是数字,也可以是 unit 的形式
line.margin 行与行之间的间隙的宽度
col 森林图横线以及点的颜色。box:box(点估计值)的颜色line:穿过方块的横线的颜色zero:中间那条基准线的颜色summary:summary中菱形的颜色hrz_lines:表中第一条横线的颜色eg:col=fpcolors(box=’royblue’,line=’darkblue’, summary=’royblue’, hrz_lines=’red’)
txt_gp 设置表格中文本的格式:用gpar进行赋值,其中cex为文本字体大小,ticks为坐标轴大小,xlab为坐标轴文字字体大小。label:表格主体文字的格式ticks:森林图下方的坐标轴的刻度文字格式xlab:定义的x轴标题格式title:标题文字的格式eg:txt_gp=fpTxtGp(label=gpar(cex=1.25), ticks=gpar(cex=1.1), xlab=gpar(cex = 1.2), title=gpar(cex = 1.2))
xticks 横坐标刻度根据需要可随意设置,如:xticks = c(0.5, 1,1.5, 2)
lwd.xaxis X轴线宽
lwd.zero 无效线的宽度
lwd.ci 置信区间线条的宽度(粗细)
lty.ci 置信区间的线条类型
ci.vertices 森林图可信区间两端添加小竖线(TRUE)
ci.vertices.height 设置森林图可信区间两端的小竖线高度,默认是10%行高
boxsize box(点估计值)的大小
mar 图形页边距,如:mar=unit(rep(1.25, times = 4), "cm")
title 添加标题
legend 当同时显示多个置信区间时,需要添加图例
new_page 是否新页
fn.ci_norm box(点估计值)的形状,默认是方块。如:fn.ci_norm="fpDrawDiamondCI":box 类型选择钻石

参考:forestplot: Draws a forest plot

2. 森林图的绘制
library(forestplot)
rs_forest <- read.csv('demo.csv',header = FALSE)
rs_forest
#              V1           V2       V3              V4   V5   V6   V7
# 1         Group Case/Control        p       OR(95%CI)   NA   NA   NA
# 2           Age                                         NA   NA   NA
# 3           <50      127/211 2.43E-11 4.24(2.84~6.66) 4.24 2.84 6.66
# 4         50-59      115/244 4.14E-12   5.48(3.5~9.2) 5.48 3.50 9.20
# 5          >=60      132/237 1.25E-08 2.74(1.96~3.93) 2.74 1.96 3.93
# 6           Sex                                         NA   NA   NA
# 7          Male      323/318 1.00E-15  3.46(2.71~4.5) 3.46 2.71 4.50
# 8        Female       51/374 3.14E-09 4.21(2.69~7.01) 4.21 2.69 7.01
# 9       Smoking                                         NA   NA   NA
# 10          Yes      122/171 2.06E-07 2.56(1.82~3.71) 2.56 1.82 3.71
# 11           No      252/521 1.00E-15 4.16(3.17~5.59) 4.16 3.17 5.59
# 12 Hypertension                                         NA   NA   NA
# 13          Yes      193/207 3.31E-12  3.14(2.31~4.4) 3.14 2.31 4.40
# 14           No      181/485 1.00E-15 3.96(2.95~5.45) 3.96 2.95 5.45
# 15     Drinking                                         NA   NA   NA
# 16          Yes       83/111 2.44E-04 2.42(1.55~4.01) 2.42 1.55 4.01
# 17           No      291/581 1.00E-15 3.83(3.01~4.96) 3.83 3.01 4.96
演示数据集demo的格式
forestplot(labeltext = as.matrix(rs_forest[,c(1:4)]),
           mean = rs_forest$V5, 
           lower = rs_forest$V6, 
           upper = rs_forest$V7, 
           is.summary=c(T,T,F,F,F,T,F,F,T,F,F,T,F,F,T,F,F),
           zero = 1, 
           boxsize = 0.5, 
           lineheight = unit(6,'mm'),
           graphwidth = unit(35,'mm'),
           colgap = unit(2,'mm'),
           lwd.zero = 2,
           lwd.ci = 2,
           col=fpColors(box='#ef233c',summary="#9e2a2b",lines = 'blue1',zero = 'grey'),
           xlab="Odds Ratio",
           lwd.xaxis=2,
           lty.ci = "solid", ci.vertices.height = 0.15,clip=c(1,13),xticks.digits = 6,
           graph.pos = 4)
forestplot(labeltext = as.matrix(rs_forest[,c(1:4)]),
           mean = rs_forest$V5, 
           lower = rs_forest$V6, 
           upper = rs_forest$V7, 
           is.summary=c(T,T,F,F,F,T,F,F,T,F,F,T,F,F,T,F,F),
           graph.pos=4, #为Pvalue箱线图所在的位置
           #定义标题
           title="Hazard Ratio Plot",
           ##定义x轴
           xlab="    <---PCI Better---   ---Medical Therapy Better--->",
           ##根据亚组的位置,设置线型,宽度造成“区块感”
           #hrzl_lines=list("2" = gpar(lwd=1, lineend="butt", columns=c(1:5),col="#99999922"),
           #                "18" = gpar(lwd=1, lineend="butt", columns=c(1:5),col="#99999922")),
           #fpTxtGp函数中的cex参数设置各个组件的大小
           txt_gp=fpTxtGp(label=gpar(cex=1.25),
                          ticks=gpar(cex=1.1),
                          xlab=gpar(cex = 1.2),
                          title=gpar(cex = 1.2)),
           ##fpColors函数设置颜色
           col=fpColors(box="#1c61b6", lines="#1c61b6", zero = "gray50"),
           #箱线图中基准线的位置
           zero=1,
           cex=0.9, lineheight = "auto",
           colgap=unit(8,"mm"),
           #箱子大小,线的宽度
           lwd.ci=2, boxsize=0.5,
           #箱线图两端添加小竖线,高度
           ci.vertices=TRUE, ci.vertices.height = 0.4)
3. 森林图结果解读

(1)森林图中横短线与中线相交表示无统计学意义;
(2)95% CI上下限均>1,即在森林图中,其95% CI横线不与无效竖线相交,且该横线落在无效线右侧时,说明该指标大于竖线代表的结局;
(3)95% CI上下限均<1,即在森林图中,其95% CI横线不与无效竖线相交,且该横线落在无效线左侧时,说明该指标小于于竖线代表的结局。

上一篇下一篇

猜你喜欢

热点阅读