I have a couple of forest-plots made using the R package forestplot
. I would like to grid.arrange
them - but it seems this doesn't work easily.
Example:
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")
Ok this will draw a plot. Now suppose I want to capture that into an object plot it side by side with another plot:
fp1 <- forestplot(row_names,
test_data$coef,
test_data$low,
test_data$high,
zero = 1,
cex = 2,
lineheight = "auto",
xlab = "Lab axis txt")
The following throws an error:
> 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"
So cleary fp1 is not a grob - but how do I achieve this by other means ?
See the second example in the help pages ?forestplot
. which shows how to do this.
forestplot
doesn't seem to return the plot: look at str(fp1)
.
A couple of options is use grid
to create the plotting space (v1), or else capture the plot and then combine (v2).
v1 using grid
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, capture the plot
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)