森林图(Forest Plot)用于可视化比较多个研究结果或者多个亚组变量在相同研究终点下的效应大小和置信区间,通常用于汇总和比较不同研究的结果,特别是在荟萃分析(Meta-Analysis)中。森林图提供了一种方式来呈现多个研究的数据,可以帮助我们更好地理解不同研究之间的一致性和差异性,以及整体效应的大小。forestplot一般如下图格式展示(随机数据,无实际意义):
那如何使用SAS软件绘制森林图呢?
以下图为例, 可以看出此forest可以分为5列(5部分):column1至column3及column5为文本表格,column4为最重要的图形部分;那我们思考是否可以将此图拆分为5部分,然后分别绘制出每部分内容?
有了这个想法后,(SAS GTL语句)我们就可以尝试使用layout lattice(开辟出5个绘图区域)+layout overlay(控制每个绘图区域绘制对应展示内容),绘制文本表格部分可以使用textplot或者axistable绘制,图形部分使用scatterplot+referenceline/dropline进行绘制;图形下方特殊内容(箭头及对应文本)可以使用annotated dataset(具体设置参考sashelp)导入绘制数据进行绘制,类似以下程序:
data anno_arrow;
length id function x1space y1space x2space y2space linecolor shape direction $20.;
retain function 'arrow' x1space "datavalue" y1space "layoutpercent" x2space "datavalue" y2space "layoutpercent"
linecolor "black";
linethickness=0.4; shape='barbed'; id="myid&fcol.";
x1=0.2; y1=-1.5; x2=0.9; y2=-1.5; direction='in'; output;
x1=1.1; y1=-1.5; x2=1.9; y2=-1.5; direction='out'; output;
run;
data anno_text;
length id function x1space y1space anchor textcolor $20. label $200.;
retain function 'text' x1space "datavalue" y1space "layoutpercent" anchor 'left' textcolor "black";
textstyle='normal'; textweight='normal'; width=200;
textsize=8; textfont='Arial'; id="myid&fcol.";
x1=0.2; y1=-5; label='Favours Group 01'; output;
x1=1.2; y1=-5; label='Favours Group 02'; output;
run;
data anno_;
set anno_arrow anno_text;
run;
proc sgrender data=final template=forestplot sganno=anno_;
run;
但是在实际绘图过程中会发现textplot绘制文本时有时候会出现每行中的5列内容并不在同一水平线上,发生错位情况,改使用axistable又会发现文本缩进及指定文本加粗的实现又要额外生成变量指定或者使用其他方法实现;写出来的程序会很长。
进一步思考,我们是否可以使用其他方法+SAS macro语言对绘制forestplot程序进行封装,将除类似上图箭头及下方文本以外较为固定格式的部分写进macro中,后续直接调用就行,类似上图箭头及下方文本等其他设置可以使用annotated dataset进行导入绘制设置。
为此我写了以下forestplot-macro实现以上说明内容。调用示例:
%let columnweights=%str(0.20 0.14 0.14 0.38 0.14);
%let xaxisls=%str(0 0.5 1 2);
%ForestPlot(indat=forestplot
,col_info=%str(
T=subtitle/X=0|
T=GROUP1/X=33/JUST=C|
T=GROUP2/X=33/JUST=C|
F=lowerci#oddsratio#upperci/title=Odds Ratio (95% CI)/X=50/JUST=C|
T=or_ci95/X=50/JUST=C|)
,columnweights=%str(&columnweights.)
,xaxisls=%str(&xaxisls.)
,xaxislsc=
,pad=
,refline=
,logaxis=
,fontsize=
,textfont=%str(Arial)
,width=
,colorbands=even
,bandplot=%str(LIMITLOWER=0.4/LIMITUPPER=1.5/fillcolor=blue/FILLTRANSPARENCY=0.7)
,f_xoffset=%str(0|0)
,add_annods=%str(anno_)
,debug=0);
此forestplot-macro有以下功能:
1. 绘制简单的文本列及图形列(在col_info参数进行简单设置即可),也解决了文本不能上下标及显示特殊字符的问题;
2. 显示背景条纹(用colorbands及colorbandsattrs参数设置);
3. 可以实现下图的合并header并加上下方表格线(mergeheader参数);
4. 在图形部分绘制置信区间band部分,类似文中第一个图中蓝色band(bandplot参数);
5.其他细微控制参数设置;
宏及相关使用文档见:
链接:xinweizhong/SAS-forestplot-macro: 用于绘制 forestplot 的 SAS 宏