森林图展示回归分析的结果

通过conda安装纯净环境的forestplot

  • conda create -n forestplot -c conda-forge r-base=4.1.3
  • conda activate forestplot
  • conda install -c conda-forge r-tidyverse=1.3.1 -y
  • conda install -c conda-forge r-irkernel -y
  • conda install -c conda-forge r-forestplot -y
  • Rscript -e “IRkernel::installspec(name=’forestplot’, displayname=’r-forestplot’)”

使用教程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require(forestplot)
f_forestplot <- function(df, xlab="XR", zero=0, lineheight=unit(10,'mm'), colgap=unit(2,'mm'), graphwidth=unit(60,'mm'), title="Forestplot"){
df_labeltext <- df[,c('VarName', 'Pvalue')]
df_labeltext[[paste0(xlab,'(95%CI)')]] <- paste0(sprintf("%0.2f", df$mean),'(',sprintf("%0.2f", df$lower),'~',sprintf("%0.2f", df$upper),')')
df_labeltext[['Pvalue']] <- sprintf('%0.1e', df_labeltext[['Pvalue']])
df_labeltext <- rbind(colnames(df_labeltext), df_labeltext)
df <- rbind(rep(NaN, ncol(df)), df)
forestplot(labeltext=as.matrix(df_labeltext[,c(1,3,2)]),
mean=df$mean,
lower=df$lower,
upper=df$upper,
zero=zero,
boxsize=0.5,
lineheight=lineheight,
colgap=colgap,
graphwidth=graphwidth,
lwd.zero=2,
lwd.ci=2,
col=fpColors(box='#458B00',
summary='#8B008B',
lines = 'black',
zero = '#7AC5CD'),
xlab=xlab,
lwd.xaxis =2,
txt_gp = fpTxtGp(ticks = gpar(cex = 0.85),
xlab = gpar(cex = 0.8),
cex = 0.9),
lty.ci="solid",
title=title,
line.margin = 1,
graph.pos=2)
}
1
2
3
4
5
6
7
8
9
df <- data.frame(
'VarName' = c('varA', 'varB', 'varC', 'varD', 'varE', 'varF', 'varG'),
'mean' = c(0.2, 0.1, 0.4, 0.6, -0.3, 0.2, -0.2),
'lower' = c(0.1, -0.1, 0.2, 0.3, -0.5, -0.1, -0.6),
'upper' = c(0.6, 0.3, 0.5, 0.7, -0.2, 0.4, -0.1),
'Pvalue' = c(0.6, 0.3, 0.5, 0.7, 0.2, 0.4, 0.1)
)
options(repr.plot.width=6, repr.plot.height=4)
f_forestplot(df)

参考1:牧羊的男孩儿


森林图展示回归分析的结果
https://b.limour.top/1907.html
Author
Limour
Posted on
July 15, 2022
Licensed under