cccfran
cccfran

Reputation: 93

ggplot2: how to align grobs to sides using arrangeGrob()?

I would like to split the title into two parts that are left/right-aligned respectively. The following code creates two textgrobs and uses arrangeGrob() to arrange the plot. But it seems there is no options in arrangeGrob() to align grobs. How can I align grobs1 to the left of the plot and and grobs2 to the right of the plot? Thanks.

    p <- ggplot()

    grobs1 <- grobTree(
      gp = gpar(fontsize = 14), 
      textGrob(label = "Left", name = "title1",
               x = unit(2.5, "lines"), y = unit(0, "lines"), 
               hjust = 0, vjust = 0),
      textGrob(label = " left", name = "title2",
               x = grobWidth("title1") + unit(2.5, "lines"), y = unit(0, "lines"),
               hjust = 0, vjust = 0, gp = gpar(col = "red", fontface = "bold"))
      )

    grobs2 <- grobTree(
      gp = gpar(fontsize = 14), 
      textGrob(label = "Right", name = "title1",
               x = unit(0, "lines"), 
               y = unit(0, "lines"),
               hjust = 0, vjust = 0),
      textGrob(label = " right", name = "title2",
               x = grobWidth("title1") +unit(0, "lines"), 
               y = unit(0, "lines"),
               hjust = 0, vjust = 0, gp = gpar(col = "red", fontface = "bold"))
    )

    gg <- arrangeGrob(p, top = arrangeGrob(grobs1, grobs2, ncol = 2), padding = unit(1, "line"))
    grid.newpage()
    grid.draw(gg)

Upvotes: 1

Views: 4230

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Two arguments of textGrob function could be helpful to move the text horizontally: just and hjust. You could try to adjust those values to get what you are after. By the way, I assume that you used the gridExtra package. I have only made some changes in just and hjust of your original code. I might have gone too far to push the right text to the right with hjust = -5, and just = "right" could be sufficient.

library(gridExtra)
library(grid)
library(ggplot2)

p <- ggplot()
    grobs1 <- grobTree(
    gp = gpar(fontsize = 14), 
    textGrob(label = "Left", name = "title1",
             x = unit(2.5, "lines"), y = unit(0, "lines"), 
             just = "left", hjust = 0, vjust = 0),
    textGrob(label = " left", name = "title2",
             x = grobWidth("title1") + unit(2.5, "lines"), y = unit(0, "lines"),
             just = "left", hjust = 0, vjust = 0, gp = gpar(col = "red", fontface = "bold"))
  )

  grobs2 <- grobTree(
    gp = gpar(fontsize = 14), 
    textGrob(label = "Right", name = "title1",
             x = unit(0, "lines"), 
             y = unit(0, "lines"),
            just = "right", 
            hjust = -5, 
            vjust = 0),
    textGrob(label = " right", name = "title2",
             x = grobWidth("title1") +unit(0, "lines"), 
             y = unit(0, "lines"),
             just = "right", 
             hjust = -5, 
             vjust = 0, gp = gpar(col = "red", fontface = "bold"))
  )

  gg <- arrangeGrob(p, top = arrangeGrob(grobs1, grobs2, ncol = 2), padding = unit(1, "line"))
  grid.newpage()
  grid.draw(gg)

enter image description here

Upvotes: 4

Related Questions