R語言森林圖繪制-forestplot

####森林圖繪制 forestplot

#### A practice for forest plot

##set your working directory

workdir <- "D:/R"

setwd(workdir)

datafile <- file.path(workdir,"ForestPlotData.csv")

data <- read.csv(datafile, stringsAsFactors=FALSE)

data[1:5,1:5]

##? 定義亞組

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

## Combine the count and percent column,以括號相連

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

##

library(forestplot)

png(filename = "Forestplot.png",width=960, height=640)

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

? ? ? ? ? title="Hazard Ratio",

? ? ? ? ? ##定義x軸

? ? ? ? ? xlab="? ? <---PCI Better---? ? ---Medical Therapy Better--->",

? ? ? ? ? ##設(shè)置透明條位置

? ? ? ? ? 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")),

? ? ? ? ? txt_gp=fpTxtGp(label=gpar(cex=1.25),

? ? ? ? ? ? ? ? ? ? ? ? ? ticks=gpar(cex=1.1),

? ? ? ? ? ? ? ? ? ? ? ? ? xlab=gpar(cex = 1.2),

? ? ? ? ? ? ? ? ? ? ? ? ? title=gpar(cex = 1.2)),

? ? ? ? ? ##線條顏色設(shè)置

? ? ? ? ? col=fpColors(box="#1c61b6", lines="#1c61b6", zero = "gray50"),

? ? ? ? ? zero=1, cex=0.9, lineheight = "auto", boxsize=0.5, colgap=unit(6,"mm"),

? ? ? ? ? lwd.ci=2, ci.vertices=TRUE, ci.vertices.height = 0.4)

dev.off()


#### 參考資料https://www.r-bloggers.com/forest-plot-with-horizontal-bands/

####廣而告之

說一個事,鑒于簡書平臺在信息傳播方面有不足之處,應(yīng)粉絲要求,白介素2的個人微信平臺已經(jīng)開啟,繼續(xù)聊臨床與科研的故事,R語言,數(shù)據(jù)挖掘,文獻(xiàn)閱讀等內(nèi)容。當(dāng)然也不要期望過高,微信平臺目前的定位是作為自己的讀書筆記,如果對大家有幫助最好。如果感興趣, 可以掃碼關(guān)注下。

![qrcode_for_gh_9eaa04438675_258.jpg](https://upload-images.jianshu.io/upload_images/15721061-115d3581833d931d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容