R绘制多彩的森林图:基于ggplot2

简介: R绘制多彩的森林图:基于ggplot2

本文首发于“生信补给站”公众号  https://mp.weixin.qq.com/s/F0npfh8uwFvW80XAOPW0OA



点击蓝字 关注我们

上一周,我们介绍了一篇关于R语言绘制森林图(forest plot)的帖子(来,挑一款你喜欢的森林图),不知道没有帮助到各位朋友呢?今天给大家带来一个多彩的的森林图,这个森林图前些日子追加在了上一帖子的评论里,应该有朋友已经看到了。今天就该多彩森林图的实现做个简单介绍。


多彩的森林图



需要的R包


该森林图是使用R packageggplot2绘制的,主要用的函数有ggplot、geom_point、geom_errorbarh、geom_vline、theme和geom_text。下面我们就一步一步来实现这一多彩森林图吧!




R程序


## 加载程序包

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))

#查看数据集

View(dataset)


数据简介


对数据情况做个简单介绍

Varnames:变量名称;

OR:Odds Ratio;

Lower,Upper:95%CI下限及上限;

Factor:影响因素类型;

Sample:样本量。

基于这个数据集,我们开始绘制第一张草图:不同类型变量用不同颜色或形状表示。


画图吧!


##草图


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(' ')

# ggsave("forest1.png",dpi = 300,width = 33,height = 20,units = "cm")


上面已经完成了第一步,下面开始添加文字。

首先通过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 Value,如果是病例对照研究,我们还可以将病例和对照组暴露人数等数据添加进去。

这里灵活性很大,大家可以根据自己的需要添加相应内容。




建立注释内容


## 建立注释内容(这里也是杜撰的数据)

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)'))

成品1:添加注释内容


## 成品1:添加注释内容

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))


如果是meta分析的森林图,想把样本量大小融入到图里,该如何做呢?

我的做法是用样本量大小去定义圆点的大小。



用样本量定义圆点大小


## 成品2:用样本量定义圆点大小

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))

大功告成,最后得到的就是生动形象的多彩的森林图啦!

相关文章
|
5月前
|
数据可视化 Python
Plotly:绘制蜡烛图
Plotly:绘制蜡烛图
81 0
|
9月前
|
数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
|
9月前
R语言中绘制箱形图的替代品:蜂群图和小提琴图
R语言中绘制箱形图的替代品:蜂群图和小提琴图
跟着 Cell 学作图 | 柱状图+误差棒+蜂群图
跟着 Cell 学作图 | 柱状图+误差棒+蜂群图
229 0
ggbiplot | 带箭头的主成分分析(PCA)图绘制
ggbiplot | 带箭头的主成分分析(PCA)图绘制
400 0
|
人工智能 数据可视化
跟SCI学umap图| ggplot2 绘制umap图,坐标位置 ,颜色 ,大小还不是你说了算
跟SCI学umap图| ggplot2 绘制umap图,坐标位置 ,颜色 ,大小还不是你说了算
1214 1
如何用ggplot2绘制基因功能富集气泡图?
如何用ggplot2绘制基因功能富集气泡图?
|
算法
ggplot2|从0开始绘制PCA图
ggplot2|从0开始绘制PCA图
504 0
|
数据可视化
R|散点图+边际图(柱形图,小提琴图),颜值区UP
R|散点图+边际图(柱形图,小提琴图),颜值区UP
178 0
|
数据可视化
R|散点图+边际图(柱形图,小提琴图),颜值UP
R|散点图+边际图(柱形图,小提琴图),颜值UP
374 0