制作森林图-简单明了

3 篇文章 0 订阅
1 篇文章 0 订阅

1、加载ggplot2包和建立数据库

install.packages("ggplot2")#没有ggplot2的话执行此步骤,下载该程序包
library("ggplot2")
dataset <- data.frame(
  
  Varnames = c("ART","WBC","CPR","DTA","EPC","FFT","GEO","HBC","PTT","JOK"),
  
  OR = c(0.9, 2, 0.3, 0.4, 0.5, 1.3, 2.5, 1.6, 1.9, 1.1),
  
  Lower=c(0.75, 1.79, 0.18, 0.2, 0.38, 1.15, 2.41, 1.41, 1.66, 0.97),
  
  Upper=c(1.05, 2.21, 0.42, 0.6, 0.62, 1.45, 2.59, 1.79, 2.14, 1.23),
  
  Factor = c('Not sig.', 'Risk', 'Protective', 'Protective', 'Protective', 'Risk', 'Risk', 'Risk', 'Risk', 'Not sig.'),
  
  Sample=c(450, 420, 390, 400, 470, 390, 400, 388, 480, 460))

#其中,Varnames是变量名称;OR是Odds Ratio;Lower,Upper是95%置信区间的下限和上限;Factor是影响因素类型;Sample是样本量。
View(dataset)#查看数据

2、初步草图代码及图形

p <- ggplot(dataset, aes(OR, Varnames, col=Factor)) # 不同形状shape= Factor

p + geom_point(size=3.6) +
  
  geom_errorbarh(aes(xmax =Upper, xmin = Lower), height = 0.4) +
  
  scale_x_continuous(limits= c(0.1, 2.6), breaks= seq(0, 2.5, 0.5)) +
  
  geom_vline(aes(xintercept = 1)) +
  
  xlab('Odds Ratio ') + ylab(' ')

草图

3、添加文字,建立注释内容,

首先通过data.frame建立包含P Value和各变量对应的P值的数据框,在通过geom_text添加到图中

##需要添加的位置、内容
annotation <- data.frame(x=c(2.89, 2.89, 2.89, 2.89, 2.89,2.89, 2.89, 2.89, 2.89, 2.89, 2.89),
                         y=c(10.47, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1),
                         label=c('P Value', '<0.001', '<0.001', '0.075','<0.001', '<0.001', '<0.001', '<0.001', '<0.001', '<0.001', '0.650'))

## 添加内容的代码

p <- ggplot(dataset, aes(OR, Varnames))

p + geom_point(size=3.6, aes(col=Factor)) +
  
  geom_errorbarh(aes(xmax = Upper, xmin = Lower), height = 0.4)+
  
  geom_vline(aes(xintercept = 1))+theme(legend.position = "top")+
  
  scale_x_continuous(limits=c(0.1, 2.9), breaks = seq(0, 2.5, 0.5))+
  
  geom_text(data=annotation,aes(x=x,y=y,label=label))+
  
  xlab('Odds Ratio ') + ylab(" ")

添加图例及P值后的森林图

4、建立注释内容,这里也是杜撰的数据;

##需要添加的注释内容
annotation <- data.frame(
  
  x=c(-1.4, -0.9, -0.35, 1, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, 2.87, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -1.4, -0.9, -0.35, -0.35, -0.35, -0.35, -0.35, -0.35, -0.35, -0.35, -0.35, -0.35),
  
  y = c(10.47, 10.47, 10.47, 10.47, 10.47, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1),
  
  label=c('Event in Case', 'Event in Control', 'OR (95% CI)', 'Odds  Ratio','P Value', '<0.001', '<0.001', '0.075', '<0.001', '<0.001', '<0.001', '<0.001', '<0.001', '<0.001', '0.650', '17 (20%)',
'13 (16%)', '23 (23%)', '18 (18%)', '30 (28%)', '25 (24%)', '10 (20%)', '8 (16%)', '31 (27%)',
 '25 (19%)','17 (20%)','13 (16%)','23 (23%)','18 (18%)','30 (28%)', '25 (24%)', '24 (28%)',
'20 (24%)', '17 (24%)', '12 (20%)', '2.00 (1.79-2.21)', '1.90 (1.66-2.14)', '1.10 (0.97-1.23)','1.6 0(1.41-1.79)', '2.50 (2.41-2.59)', '1.3 (1.15-1.45)', '0.5 (0.38-0.62)', '0.40 (0.20-0.60)','0.30 (0.18-0.42)', '0.90 (0.75-1.05)'))
##添加注释内容的代码
p <- ggplot(dataset, aes(OR, Varnames))

p + geom_point(size=3.6, aes(col=Factor)) +
  
  geom_errorbarh(aes(xmax =Upper, xmin = Lower),height = 0.4)+
  
  geom_vline(aes(xintercept=1))+
  
  scale_x_continuous(limits=c(-1.5, 2.87), breaks=seq(0, 2.5, 0.5)) +
  
  xlab(' ') + ylab(" ") + theme_bw() + theme(legend.position ="top") +
  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        
        plot.background = element_rect(fill ='skyblue', color ='red'),
        
        axis.text=element_text(size=10, face = "bold"),legend.text=element_text(size=11))+
  
  geom_text(data=annotation,aes(x=x,y=y,label=label))

添加注释内容后的森林图

5、融入样本量,用样本量的大小去定义圆点的大小

p + geom_point(aes(size = Sample, col = Factor)) +
  
  geom_errorbarh(aes(xmax = Upper, xmin = Lower), height = 0.4)+
  
  geom_vline(aes(xintercept = 1))+
  
  scale_x_continuous(limits = c(-1.5, 2.87), breaks = seq(0,2.5, 0.5)) +
  
  xlab(' ')+ylab(" ") + theme_bw() + theme(legend.position ="top") +
  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        
        plot.background = element_rect(fill ='skyblue', color ='red'),
        
        axis.text=element_text(size=10, face = "bold"),legend.text=element_text(size=11))+
  
  geom_text(data=annotation, aes(x=x, y=y, label=label))

融入样本量的森林图

6、大功告成!!!

  • 7
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白胖子的CFD

看各位大佬心情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值