1

I have a lattice figure that contains a multi-line expression() statement. I want to fine–tune the vertical space between the lines of this statement. Is there a way to do this?

Here is code that demonstrates the problem:

library(devtools)
library(grid)
library(lattice)
source_gist(2732693)  # source grid.expr()


# PANEL FUNCTIONS
myPanel1 <- function () {
  grid.text(
    label = expression(atop("This is", paste("italicized ", italic(F))),
    gp = gpar(lineheight = 5)))
}
myPanel2 <- function () {
  grid.expr(
    as.expression(
      list("This is", bquote(paste("italicized ", italic(F))))),
    gp = gpar(lineheight = 5))
}    


# DRAW THE FIGURES
xyplot(1 ~ 1, panel = myPanel1)
xyplot(1 ~ 1, panel = myPanel2)

This code offers two ways to create a multi-line expression() statement in a lattice plot. But the gp argument is ignored in both cases, and I don't see a way to adjust the vertical spacing of lines within the expression. Is there a way to do this, short of "manually" positioning the lines with a separate grid.text or grid.expr command for each line?

user697473
  • 2,165
  • 1
  • 20
  • 47

1 Answers1

1

you could try with gtable

library(gtable)
library(grid)

grid.expr <- function(labels, ..., 
                      width=NULL, heights=NULL, 
                      margin=unit(0.5,"line")){

  gl <- lapply(labels, textGrob, ...)

  if(is.null(heights))
    heights <- do.call(unit.c, lapply(gl, grobHeight)) + margin

  widths <- do.call(max, lapply(gl, grobWidth))
  gt <- gtable_matrix("table", grobs = matrix(gl,ncol=1), 
                      widths=widths, heights=heights)
  grid.draw(gt)
}

grid.newpage()
grid.expr(LETTERS[1:5], heights=unit(1:5,"line"))
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thank you. This solution works well, and it's especially nice in that it offers two distinct ways to vary the vertical distance between lines (the "heights" and "margin" arguments). – user697473 Dec 28 '14 at 21:27
  • explanation would be helpful, this is difficult to decipher – jzadra Aug 23 '17 at 20:13