Fuv8 Fuv8 - 11 months ago 70
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?

function will be sufficient?

Thanks a lot,



Answer Source

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.

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

print(heatmap, vp=vp1)
print(forestplot, vp=vp2)

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

grid.arrange(heatmap, forestplot)