4

I created a function to create forest plots, which have labels on the y axis. I need to create a number of them to join in an external graphic program.

The plots images will have the same width but the height will change according to the number of y elements. To achieve this I first created a plot and manually set the image height (i.h1). Then I subtracted from i.h1 the upper (u.m) and bottom (b.m) margin to get the plot height. Finally I divided the plot height by the number of y labels n1 to get the one unit dimension u = (i.h1 - b.m - u.m) / n1.

To get the size of the subsequent image h.i2 I just needed to h.i2 = u * n2 + b.m + u.m.

Here's is the result on a first image with 27 elements (counting the blank spaces) and a second image with 6 elements.

The second image is way higher than what it should be! (the purple lines are for comparison).

Furthermore the internal margin between the lower element and the x axis is different. So even if I use a different unit height I would end up with the labels in the 2 images not aligned.

enter image description here

I don't understand this behavior. Any hint?

Here's the code for saving the picture:

gt <- ggplot_gtable(ggplot_build(plot)) #plot is a ggplot object
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

if (file != F) {
    w = 8.5
    h = .33 * max(data$yval) + 4 # .33 is the unit height and 4 are the margin

    dev.copy(pdf, if (file == T) sprintf("%s.pdf", gsub('\\n', '', title)) else file, width = w, height = h) # I use dev.copy because ggsave don't accept gtable plots.

    invisible(dev.off())
}
Bakaburg
  • 3,165
  • 4
  • 32
  • 64
  • 4
    A reproducible example would be helpful. For now, does [this SO answer](http://stackoverflow.com/a/13295880/496488) provide any assistance? – eipi10 Dec 07 '15 at 21:35

0 Answers0