Fuv8 Fuv8 - 2 months ago 19
R Question

Heatmap and forest plot on the same pdf file

is it possible to bind a heatmap derived from a data.frame and a forest plot derived from another data.frame (using ggplot2) in the same .pdf file so that the forest plot will be on the right side of the heatmap?

par
function will be sufficient?

Thanks a lot,

Best

F.

Answer

I can't give a complete answer without a reproducible example. However, one nice way to do it when you're using the grid-derived ggplot2 package is with viewports in grid:

First, define your plots without printing them:

heatmap <- ggplot(...
forestplot <- ggplot(...

Next, define viewports.

require(grid)
vp1 <- viewport(x=0.25, y=0.5, width=0.5, height=0.5)
vp2 <- viewport(x=0.75, y=0.5, width=0.5, height=0.5)

x and y represent the position of the center of the plot, along the x- and y-axes (from the left and bottom, respectively).

Then, use the pdf device to save the plot

pdf("my_file.pdf")
print(heatmap, vp=vp1)
print(forestplot, vp=vp2)
dev.off

Another possibility is to use grid.arrange, which will come up with a nice arrangement for you automatically:

require(gridExtra)
pdf("myfile.pdf")
grid.arrange(heatmap, forestplot)
dev.off()
Comments