我使用R包forestplot
制作了几个森林图。我想grid.arrange
他们 - 但似乎这并不容易。
示例:
library(forestplot)
# A basic example, create some fake data
row_names <- list(list("test = 1", expression(test >= 2)))
test_data <- data.frame(coef=c(1.59, 1.24),
low=c(1.4, 0.78),
high=c(1.8, 1.55))
forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt")
好的,这将绘制一个情节。现在假设我想把它捕获到一个对象中,将它与另一个图并排绘制:
fp1 <- forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt")
以下引发错误:
> grid.arrange(fp1, fp1)
Hit <Return> to see next plot:
Error in gList(list(path = "GRID.VP.7537", name = "arrange.1-1-1-1", n = 2L, :
only 'grobs' allowed in "gList"
如此谨慎的fp1不是一个grob - 但我如何通过其他方式实现这一目标?
答案 0 :(得分:2)
请参阅帮助页?forestplot
中的第二个示例。这表明了如何做到这一点。
forestplot
似乎没有返回情节:看str(fp1)
。
有两个选项是使用grid
创建绘图空间(v1),或者捕获绘图然后合并(v2)。
v1使用网格
library(grid)
library(forestplot)
# Create 2 rows by one columns viewport
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 1)))
# Plot in viewport position 1x1
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt")
upViewport(1)
# Plot in viewport position 2x1
pushViewport(viewport(layout.pos.row=2, layout.pos.col=1))
forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt",
new_page = FALSE)
upViewport(1)
v2,捕捉情节
fp1 <- grid.grabExpr(print(forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt")))
gridExtra::grid.arrange(fp1, fp1)