R语言绘制高级森林图(1)

本文展示了如何利用R包plotforest来创建孟德尔随机化研究中的森林图。首先,对数据进行处理,然后定义图形的主题样式,包括文字大小、可信区间的颜色和形状等。接着,调用forest函数绘制图形,并设置了估计值、可信区间、参考线以及汇总菱形的颜色和样式。最后,通过添加文本和边框改进了图形的可读性。
摘要由CSDN通过智能技术生成

使用R包plotforestr,以绘制孟德尔随机化中的森林图为例。

#导入符合要求的数据
new_colnames <- paste0(colnames(`IVW(fixed-effect)`), "_2")
colnames(`IVW(fixed-effect)`) <- new_colnames
dt<-cbind(IVW,`IVW(fixed-effect)`)
dt$` `<-paste(rep(" ", dim(dt)[1]), collapse = " ")
dt$`  `<-paste(rep(" ", dim(dt)[1]), collapse = " ")
colnames(dt)

设置基本主题

tm <- forest_theme(base_size = 8, # 文字大小
                   #可信区间
                   ci_pch = 10,#点形状
                   ci_col = "red", #颜色
                   ci_lty = 1,#线条类型
                   ci_lwd = 4,#线宽度
                   ci_Theight = 0.2, #两端加短竖线
                   # 参考线
                   refline_lwd = 1,#宽度
                   refline_lty = "dashed",#类型
                   refline_col = "black",#颜色
                   # 汇总菱形
                   summary_fill = "blue",#填充色
                   summary_col = "blue",#边框色
                   # 脚注
                   footnote_cex = 0.6,#大小
                   footnote_fontface = "italic",#字体
                   footnote_col = "black")#颜色

绘制图形

p <-forest(dt[,c(3,4,31,15,18,19,32,30)],
           est = list(dt$or,
                      dt$or_2),
           lower = list(dt$or_lci95,
                        dt$or_lci95_2),
           upper = list(dt$or_uci95,
                        dt$or_uci95_2),
           ci_column = c(3, 7),
           ref_line = 1,
           xlim = c(0.01,100),x_trans ="log10",
           ticks_at = c(0.01,0.1,1,10,100),
           #vert_line = c(1,2),
           #nudge_y = 0.2,
           theme = tm)
p
#顶部插入信息
p1<-insert_text(p,text =c("IVW","IVW(fixed-effect)"),
                col = c(2,6),
                part = "header",
                gp = gpar(fontface =
                           "bold"))
p1
#在标题下方加一条下划线
p2 <-add_border(p1, 
                row =c(1),
                col=c(1:3,5:7),
                part = "header")
p2
#为标题的两行添加下划线
p3 <-add_border(p2, 
                row =c(0,2),
                col=1:8,
                part = "header")
p3

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值