森林图之初体验
最近小编在浏览文献的时候多次看到了森林图,心心念念想着怎样可以画出来,下面将绘制森林图的两种方法推荐给大家。
对于绘制森林图,我们需要准备的文件大体如下,主要包含基因名、p值、HR值和置信区间几个部分。
方法一:forestplot包
#相关R包加载
library(forestplot)
#数据处理
sample<-data.frame(sample,stringsAsFactors=FALSE)
tabletext1<-as.character(sample[,1])
tabletext2<-round(as.numeric(sample[,2]),5)
tabletext3<-paste(round(as.numeric(sample[,3]),3),round(as.numeric(sample[,4]),2),sep="(")
tabletext4<-paste(tabletext3,round(as.numeric(sample[,5]),2),sep="-")
tabletext5<-paste0(tabletext4,sep=")")
tabletext<-cbind(tabletext1,tabletext2,tabletext5)
#绘制森林图
forestplot(labeltext=tabletext, #文本信息
mean = round(sample[,3],3),##HR值
lower = round(sample[,4],2),##95%置信区间
upper = round(sample[,5],2),#95%置信区间
boxsize = 0.8,##大小
graph.pos=3,#图在表中的列位置
graphwidth = unit(0.4,"npc"),#图在表中的宽度比例
fn.ci_norm="fpDrawDiamondCI",#box类型选择钻石,可以更改fpDrawNormalCI;fpDrawCircleCI等
col=fpColors(box="steelblue", lines="black", zero = "black"),#颜色设置
lwd.ci=2,ci.vertices.height = 0.1,ci.vertices=TRUE,#置信区间用线宽、高、型
zero=1,#zero线横坐标
lwd.zero=2,#zero线宽
grid=T,
lwd.xaxis=2,#X轴线宽
title="Hazard Ratio",
xlab="",#X轴标题
clip=c(-Inf,3),#边界
colgap = unit(0.5,"cm"),#列间隙
new_page = TRUE#是否新页
)
最终我们将得到下面形式的森林图
除了上面这样形式的森林图,小编在浏览文献中更多的看到的是这样的森林图,
这样的森林图看上去更像是通过ggplot2包调整绘制的,于是小编进行了第二种森林图绘制方式的尝试。
方法二:ggplot2包
##加载相关R包
library(ggplot2)
#数据处理
sample<-data.frame(sample,stringsAsFactors=FALSE)
sample[,2]<-as.numeric(sample[,2])
sample[,3]<-as.numeric(sample[,3])
sample[,4]<-as.numeric(sample[,4])
sample[,5]<-as.numeric(sample[,5])
#绘制森林图
ggplot(sample,aes(x=HR,y=gene.symbol,color=P.value))+
geom_errorbarh(aes(xmax=High.95.CI,xmin=Low.95.CI),color="black",height=0,size=0.8)+#加线
geom_point(aes(x=HR,y=gene.symbol),size=4,shape=18)+###点
geom_vline(xintercept=1,linetype="dashed",size=1.2)+##垂直线
scale_x_continuous(breaks=c(0,0.5,1,1.5,2,2.5,3.0))+
scale_y_discrete(labels=c(as.character(sample[,1])))+
coord_trans(x="log2")+
scale_color_continuous(low="dodgerblue4",high="lightskyblue")+
ylab("Gene")+xlab("Hazard ratios")+
labs(color="P value",title="")+
theme(panel.grid.major =element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),axis.line = element_line(colour = "black"))+
theme(axis.text=element_text(colour="black",size=14))+
theme(legend.text=element_text(size=14))+
theme(legend.title=element_text(size=15))+
theme(axis.title=element_text(colour="black",size=14))
最终我们将得到下面形式的森林图
这就是小编对于森林图的初步探索,更多细节美化部分还需要大家多多建议呀~
更多生信分析套路,请加微信13621202201
TCGA | 小工具 | 数据库 |组装| 注释 | 基因家族 | Pvalue
基因预测 |bestorf | sci | NAR | 在线工具 | 生存分析 | 热图
生信不死 | 初学者 | circRNA | 一箭画心| 十二生肖 | circos
舞台|基因组 | 黄金测序 | 套路 | 杂谈组装 | 进化 | 测序简史