制作森林图-简单明了
生活随笔
收集整理的這篇文章主要介紹了
制作森林图-简单明了
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、加載ggplot2包和建立數(shù)據(jù)庫
install.packages("ggplot2")#沒有g(shù)gplot2的話執(zhí)行此步驟,下載該程序包 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%置信區(qū)間的下限和上限;Factor是影響因素類型;Sample是樣本量。 View(dataset)#查看數(shù)據(jù)2、初步草圖代碼及圖形
p <- ggplot(dataset, aes(OR, Varnames, col=Factor)) # 不同形狀shape= Factorp + 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、添加文字,建立注釋內(nèi)容,
首先通過data.frame建立包含P Value和各變量對應(yīng)的P值的數(shù)據(jù)框,在通過geom_text添加到圖中
##需要添加的位置、內(nèi)容 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'))## 添加內(nèi)容的代碼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(" ")4、建立注釋內(nèi)容,這里也是杜撰的數(shù)據(jù);
##需要添加的注釋內(nèi)容 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)')) ##添加注釋內(nèi)容的代碼 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、大功告成!!!
總結(jié)
以上是生活随笔為你收集整理的制作森林图-简单明了的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机辅助翻译软件差异,两款计算机辅助翻
- 下一篇: 利用Grapher Script 重复绘