更多關於R語言,ggplot2繪圖,生信分析的內容,敬請關注小號,給您干貨。
上一篇簡單的介紹了COX生存分析結果繪制森林圖Forest plot(森林圖) | Cox生存分析可視化,本文將介紹根據數據集合的基本信息以及點估計值(置信區間區間)的結果直接繪制森林圖的方法。
其中點估計值(置信區間)的結果可以是COX也可以是logistic回歸等其他方法的結果,適用范圍更廣。
#載入R包
library(forestplot)
#數據來源:https://www.r-bloggers.com/forest-plot-with-horizontal-bands/
data <- read.csv("ForestPlotData.csv", stringsAsFactors=FALSE)
#查看數據
head(data)
二 繪制森林圖
2.1 簡單森林圖
對數據進行部分修改,方便行名和列名字輸出
## 構建tabletext,更改列名稱,展示更多信息
np <- ifelse(!is.na(data$Count), paste(data$Count," (",data$Percent,")",sep=""), NA)
## The rest of the columns in the table.
tabletext <- cbind(c("Subgroup","\n",data$Variable),
c("No. of Patients (%)","\n",np),
c("4-Yr Cum. Event Rate\n PCI","\n",data$PCI.Group),
c("4-Yr Cum. Event Rate\n Medical Therapy","\n",data$Medical.Therapy.Group),
c("P Value","\n",data$P.Value))
##繪制森林圖
forestplot(labeltext=tabletext, graph.pos=3,
mean=c(NA,NA,data$Point.Estimate),
lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High),
boxsize=0.5)
如上圖所示基本信息OK了,但是可以在以下幾個方面進行優化:
-
添加線條,區分Subgroup
-
更改箱線圖的寬度,顏色和大小
-
更改字體大小,更易區分
-
添加標題和橫坐標軸標示
2.2 優化森林圖
## 定義亞組,方便后面線條區分
subgps <- c(4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33)
data$Variable[subgps] <- paste(" ",data$Variable[subgps])
forestplot(labeltext=tabletext,
graph.pos=3, #為Pvalue箱線圖所在的位置
mean=c(NA,NA,data$Point.Estimate),
lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High),
#定義標題
title="Hazard Ratio Plot",
##定義x軸
xlab=" <---PCI Better--- ---Medical Therapy Better--->",
##根據亞組的位置,設置線型,寬度造成“區塊感”
hrzl_lines=list("3" = gpar(lwd=1, col="#99999922"),
"7" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
"15" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
"23" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
"31" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922")),
#fpTxtGp函數中的cex參數設置各個組件的大小
txt_gp=fpTxtGp(label=gpar(cex=1.25),
ticks=gpar(cex=1.1),
xlab=gpar(cex = 1.2),
title=gpar(cex = 1.2)),
##fpColors函數設置顏色
col=fpColors(box="#1c61b6", lines="#1c61b6", zero = "gray50"),
#箱線圖中基准線的位置
zero=1,
cex=0.9, lineheight = "auto",
colgap=unit(8,"mm"),
#箱子大小,線的寬度
lwd.ci