获取更多R语言知识,请关注公众号: 医学和生信笔记
医学和生信笔记,专注R语言在临床医学中的使用,R语言数据分析和可视化。主要分享R语言做医学统计学、meta分析、网络药理学、临床预测模型、机器学习、生物信息学等。
文章目录
*
– 准备数据
– 画图
– 更改主题!!
– 编辑图形
– 有亚组的森林图
前面介绍了使用 forestplot
包画森林图:
这个包很强大,熟练使用可以自由调整各种细节,不过由于细节太多、自由度太高,反而不容易上手。
今天给大家介绍另外一个可以画森林图的R包: forestploter
,帮助你快速画出好看的森林图。
准备数据
还是用上次用到过的数据,基本的画图理念是差不多的,都是把图的内容看成是表格的形式,用空白或者NA填充空的单元格。
数据准备比 forestplot
包简单一点,文字数据和画”森林”的数据,一起放在一个数据框中即可。
tabletext data.frame(
study = c("Auckland", "Block", "Doran", "Gamsu", "Morrison", "Papageorgiou", "Tauesch", "Summary"),
steroid = c("36", "1", "4", "14", "3", "1", "8", ""),
placebo = c("60", "5", "11", "20", "7", "7", "10", ""),
mean = c(0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, 0.531),
lower = c(0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, 0.386),
upper = c(0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, 0.731)
)
tabletext$HR (95% CI)
ifelse(is.na(tabletext$mean), "",
sprintf("%.2f (%.2f to %.2f)",
tabletext$mean, tabletext$lower, tabletext$upper))
tabletext$
paste(rep(" ", 8), collapse = " ")
tabletext
数据就准备好了。
画图
library(forestploter)
最基本的画图:
p forest(data = tabletext[,c(1:3,8,7)],
lower = tabletext$lower,
upper = tabletext$upper,
est = tabletext$mean,
ci_column = 4
)
print(p)
自动增加了色条,增加美感。
稍加美化:
p forest(data = tabletext[,c(1:3,8,7)],
lower = tabletext$lower,
upper = tabletext$upper,
est = tabletext$mean,
ci_column = 4,
sizes = tabletext$mean,
is_summary = c(rep(FALSE,nrow(tabletext)-1), TRUE),
ref_line = 1,
xlim = c(0,3),
ticks_at = c(0,1,2,3),
arrow_lab = c("this better","that better"),
footnote = "A simple example of forestploter"
)
print(p)
更改主题!!
这个包比较创新的地方就在于主函数 forest
只提供基本参数,更多的细节调整借助 主题进行调整。
接下来我们就使用主题对森林图进行一些美化。
tm forest_theme(base_size = 10,
ci_pch = 16,
ci_col = "#4575b4",
ci_lty = 1,
ci_lwd = 1.5,
ci_Theight = 0.2,
refline_lwd = 1,
refline_lty = "dashed",
refline_col = "grey20",
summary_fill = "#4575b4",
summary_col = "#4575b4",
footnote_cex = 0.6,
footnote_fontface = "italic",
footnote_col = "blue")
设置好之后,传递给 theme
参数即可
p1 forest(data = tabletext[,c(1:3,8,7)],
lower = tabletext$lower,
upper = tabletext$upper,
est = tabletext$mean,
ci_column = 4,
is_summary = c(rep(FALSE,nrow(tabletext)-1), TRUE),
ref_line = 1,
xlim = c(0,3),
ticks_at = c(0,1,2,3),
arrow_lab = c("this better","that better"),
footnote = "A simple example of forestploter",
theme = tm
)
print(p1)
编辑图形
除了使用主题外,该包还支持对图形进行特定的调整,比如更改某一行的颜色,控制字体,增加空行等等,主要是通过下面4个函数实现的:
edit_plot
:改变特定行或列的字体和字体颜色add_underline
:对特定的行增加边框add_text
:对特定的行或列增加文字insert_text
:在特定的行前或后增加一行并增加文字
g edit_plot(p1, row = 3, gp = gpar(col = "red", fontface = "italic"))
g edit_plot(g,
row = c(2, 5),
gp = gpar(fontface = "bold"))
g edit_plot(g, row = 6, which = "background",
gp = gpar(fill = "darkolivegreen1"))
g insert_text(g,
text = "A short title",
col = 3:4,
part = "header",
gp = gpar(fontface = "bold"))
g add_underline(g, part = "header")
g insert_text(g,
text = "This is a long text. Age and gender summarised above.\nBMI is next",
row = 7,
just = "left",
gp = gpar(cex = 0.6, col = "green", fontface = "italic"))
plot(g)
果然是很强啊!细节满满!而且非常方便,比 forestplot
包强多了!
有亚组的森林图
还是用上次推文的数据。
rm(list = ls())
df read.csv("../000files/forestplot.csv", header = T)
df$
paste(rep(" ", 26), collapse = " ")
colnames(df)[4:7] c("HR(95%CI)","mean","low","high")
df
太棒了,和之前的比较一下,你喜欢哪一个呢?
现在的:
之前的:
获取代码及示例数据请关注微信公众号: 医学和生信笔记,号内搜索 森林图即可。
获取更多R语言知识,请关注公众号: 医学和生信笔记
医学和生信笔记,专注R语言在临床医学中的使用,R语言数据分析和可视化。主要分享R语言做医学统计学、meta分析、网络药理学、临床预测模型、机器学习、生物信息学等。
Original: https://blog.csdn.net/Ayue0616/article/details/126107354
Author: 阿越就是我
Title: 用更简单的方式画森林图
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/693383/
转载文章受原作者版权保护。转载请注明原作者出处!