Vertical justification and heights in arrangeGrob









up vote
1
down vote

favorite












I am trying to construct a report with multiple pages. Each page has the same structure, a title that says what hospital is being shown, then a table in the upper half of the page and a plot in the lower half. I am using tableGrob to convert the table to a graphics object and arrangeGrob to put it all into a page. My code looks something like:



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

df <- iris[1:10,]
pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
gp=gpar(fontsize=15))
padding <- unit(3,"line")
tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal())
tt <- gtable_add_rows(tt, heights = grobHeight(tTitle) + padding, pos = 0)
tt <- gtable_add_grob(tt, list(tTitle), t=1, l=1, r=ncol(tt))

pg <- arrangeGrob(tt, pp,
nrow=2, as.table=FALSE)

pdf("Demo.pdf", paper = "a4", width = 8.27)
grid.draw(pg)
dev.off()


My problem is that there is lots of space at the top of the page and the bottom of the table is cut off by the plot.



How do I vertically justify the heading and table so that they move up the page, leaving enough space for the plot?










share|improve this question

























    up vote
    1
    down vote

    favorite












    I am trying to construct a report with multiple pages. Each page has the same structure, a title that says what hospital is being shown, then a table in the upper half of the page and a plot in the lower half. I am using tableGrob to convert the table to a graphics object and arrangeGrob to put it all into a page. My code looks something like:



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

    df <- iris[1:10,]
    pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

    tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
    gp=gpar(fontsize=15))
    padding <- unit(3,"line")
    tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal())
    tt <- gtable_add_rows(tt, heights = grobHeight(tTitle) + padding, pos = 0)
    tt <- gtable_add_grob(tt, list(tTitle), t=1, l=1, r=ncol(tt))

    pg <- arrangeGrob(tt, pp,
    nrow=2, as.table=FALSE)

    pdf("Demo.pdf", paper = "a4", width = 8.27)
    grid.draw(pg)
    dev.off()


    My problem is that there is lots of space at the top of the page and the bottom of the table is cut off by the plot.



    How do I vertically justify the heading and table so that they move up the page, leaving enough space for the plot?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to construct a report with multiple pages. Each page has the same structure, a title that says what hospital is being shown, then a table in the upper half of the page and a plot in the lower half. I am using tableGrob to convert the table to a graphics object and arrangeGrob to put it all into a page. My code looks something like:



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

      df <- iris[1:10,]
      pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

      tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
      gp=gpar(fontsize=15))
      padding <- unit(3,"line")
      tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal())
      tt <- gtable_add_rows(tt, heights = grobHeight(tTitle) + padding, pos = 0)
      tt <- gtable_add_grob(tt, list(tTitle), t=1, l=1, r=ncol(tt))

      pg <- arrangeGrob(tt, pp,
      nrow=2, as.table=FALSE)

      pdf("Demo.pdf", paper = "a4", width = 8.27)
      grid.draw(pg)
      dev.off()


      My problem is that there is lots of space at the top of the page and the bottom of the table is cut off by the plot.



      How do I vertically justify the heading and table so that they move up the page, leaving enough space for the plot?










      share|improve this question













      I am trying to construct a report with multiple pages. Each page has the same structure, a title that says what hospital is being shown, then a table in the upper half of the page and a plot in the lower half. I am using tableGrob to convert the table to a graphics object and arrangeGrob to put it all into a page. My code looks something like:



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

      df <- iris[1:10,]
      pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

      tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
      gp=gpar(fontsize=15))
      padding <- unit(3,"line")
      tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal())
      tt <- gtable_add_rows(tt, heights = grobHeight(tTitle) + padding, pos = 0)
      tt <- gtable_add_grob(tt, list(tTitle), t=1, l=1, r=ncol(tt))

      pg <- arrangeGrob(tt, pp,
      nrow=2, as.table=FALSE)

      pdf("Demo.pdf", paper = "a4", width = 8.27)
      grid.draw(pg)
      dev.off()


      My problem is that there is lots of space at the top of the page and the bottom of the table is cut off by the plot.



      How do I vertically justify the heading and table so that they move up the page, leaving enough space for the plot?







      r gridextra gtable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 15:40









      JenB

      7,7781936




      7,7781936






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Here's one possibility: query the height of the table (a fixed number dictated by the content), and leave the rest for the plot. Note that paper="a4" introduces its own idiosyncrasies, I'd leave that out.



          library(ggplot2)
          library(gridExtra)
          library(grid)
          library(gtable)
          library(maggritr)

          df <- iris[1:10,]
          pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

          tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
          gp=gpar(fontsize=15))
          padding <- unit(3,"line")

          tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal()) %>%
          gtable_add_rows(heights = grobHeight(tTitle) + padding, pos = 0) %>%
          gtable_add_grob(list(tTitle), t=1, l=1, r=ncol(tt)) %>%
          gtable_add_grob(rectGrob(gp=gpar(fill=NA,col="red")),
          t=1, l=1, r=ncol(tt),b=nrow(tt))

          th <- sum(tt$heights)
          pg <- arrangeGrob(tt, pp, heights = unit.c(th, unit(1,"npc")-th))

          pdf("Demo.pdf", width=21/2.54,height=27.7/2.54)
          grid.draw(pg)
          dev.off()


          Note that with this approach the ggplot will expand to fill the remaining space. If you don't want that, you should assign a fixed height, e.g.



          # 10 cm or remaining space on the page if smaller than that
          cappedheight = unit.pmin(unit(1,"npc"), unit(10, "cm"))
          # wrap plot in a grobTree to assign a viewport (centered in remaining space)
          pp1 <- grobTree(ggplotGrob(pp), vp=viewport(height=cappedheight))
          pg <- arrangeGrob(tt, pp1, heights = unit.c(th, unit(1,"npc")-th))





          share|improve this answer




















          • Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
            – JenB
            Nov 10 at 11:45










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211177%2fvertical-justification-and-heights-in-arrangegrob%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Here's one possibility: query the height of the table (a fixed number dictated by the content), and leave the rest for the plot. Note that paper="a4" introduces its own idiosyncrasies, I'd leave that out.



          library(ggplot2)
          library(gridExtra)
          library(grid)
          library(gtable)
          library(maggritr)

          df <- iris[1:10,]
          pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

          tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
          gp=gpar(fontsize=15))
          padding <- unit(3,"line")

          tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal()) %>%
          gtable_add_rows(heights = grobHeight(tTitle) + padding, pos = 0) %>%
          gtable_add_grob(list(tTitle), t=1, l=1, r=ncol(tt)) %>%
          gtable_add_grob(rectGrob(gp=gpar(fill=NA,col="red")),
          t=1, l=1, r=ncol(tt),b=nrow(tt))

          th <- sum(tt$heights)
          pg <- arrangeGrob(tt, pp, heights = unit.c(th, unit(1,"npc")-th))

          pdf("Demo.pdf", width=21/2.54,height=27.7/2.54)
          grid.draw(pg)
          dev.off()


          Note that with this approach the ggplot will expand to fill the remaining space. If you don't want that, you should assign a fixed height, e.g.



          # 10 cm or remaining space on the page if smaller than that
          cappedheight = unit.pmin(unit(1,"npc"), unit(10, "cm"))
          # wrap plot in a grobTree to assign a viewport (centered in remaining space)
          pp1 <- grobTree(ggplotGrob(pp), vp=viewport(height=cappedheight))
          pg <- arrangeGrob(tt, pp1, heights = unit.c(th, unit(1,"npc")-th))





          share|improve this answer




















          • Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
            – JenB
            Nov 10 at 11:45














          up vote
          0
          down vote













          Here's one possibility: query the height of the table (a fixed number dictated by the content), and leave the rest for the plot. Note that paper="a4" introduces its own idiosyncrasies, I'd leave that out.



          library(ggplot2)
          library(gridExtra)
          library(grid)
          library(gtable)
          library(maggritr)

          df <- iris[1:10,]
          pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

          tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
          gp=gpar(fontsize=15))
          padding <- unit(3,"line")

          tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal()) %>%
          gtable_add_rows(heights = grobHeight(tTitle) + padding, pos = 0) %>%
          gtable_add_grob(list(tTitle), t=1, l=1, r=ncol(tt)) %>%
          gtable_add_grob(rectGrob(gp=gpar(fill=NA,col="red")),
          t=1, l=1, r=ncol(tt),b=nrow(tt))

          th <- sum(tt$heights)
          pg <- arrangeGrob(tt, pp, heights = unit.c(th, unit(1,"npc")-th))

          pdf("Demo.pdf", width=21/2.54,height=27.7/2.54)
          grid.draw(pg)
          dev.off()


          Note that with this approach the ggplot will expand to fill the remaining space. If you don't want that, you should assign a fixed height, e.g.



          # 10 cm or remaining space on the page if smaller than that
          cappedheight = unit.pmin(unit(1,"npc"), unit(10, "cm"))
          # wrap plot in a grobTree to assign a viewport (centered in remaining space)
          pp1 <- grobTree(ggplotGrob(pp), vp=viewport(height=cappedheight))
          pg <- arrangeGrob(tt, pp1, heights = unit.c(th, unit(1,"npc")-th))





          share|improve this answer




















          • Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
            – JenB
            Nov 10 at 11:45












          up vote
          0
          down vote










          up vote
          0
          down vote









          Here's one possibility: query the height of the table (a fixed number dictated by the content), and leave the rest for the plot. Note that paper="a4" introduces its own idiosyncrasies, I'd leave that out.



          library(ggplot2)
          library(gridExtra)
          library(grid)
          library(gtable)
          library(maggritr)

          df <- iris[1:10,]
          pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

          tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
          gp=gpar(fontsize=15))
          padding <- unit(3,"line")

          tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal()) %>%
          gtable_add_rows(heights = grobHeight(tTitle) + padding, pos = 0) %>%
          gtable_add_grob(list(tTitle), t=1, l=1, r=ncol(tt)) %>%
          gtable_add_grob(rectGrob(gp=gpar(fill=NA,col="red")),
          t=1, l=1, r=ncol(tt),b=nrow(tt))

          th <- sum(tt$heights)
          pg <- arrangeGrob(tt, pp, heights = unit.c(th, unit(1,"npc")-th))

          pdf("Demo.pdf", width=21/2.54,height=27.7/2.54)
          grid.draw(pg)
          dev.off()


          Note that with this approach the ggplot will expand to fill the remaining space. If you don't want that, you should assign a fixed height, e.g.



          # 10 cm or remaining space on the page if smaller than that
          cappedheight = unit.pmin(unit(1,"npc"), unit(10, "cm"))
          # wrap plot in a grobTree to assign a viewport (centered in remaining space)
          pp1 <- grobTree(ggplotGrob(pp), vp=viewport(height=cappedheight))
          pg <- arrangeGrob(tt, pp1, heights = unit.c(th, unit(1,"npc")-th))





          share|improve this answer












          Here's one possibility: query the height of the table (a fixed number dictated by the content), and leave the rest for the plot. Note that paper="a4" introduces its own idiosyncrasies, I'd leave that out.



          library(ggplot2)
          library(gridExtra)
          library(grid)
          library(gtable)
          library(maggritr)

          df <- iris[1:10,]
          pp <- ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.2)

          tTitle <- textGrob(paste("Title line 1","n", "Line 2", "n", "Line 3"),
          gp=gpar(fontsize=15))
          padding <- unit(3,"line")

          tt <- tableGrob(df, rows = NULL, theme = ttheme_minimal()) %>%
          gtable_add_rows(heights = grobHeight(tTitle) + padding, pos = 0) %>%
          gtable_add_grob(list(tTitle), t=1, l=1, r=ncol(tt)) %>%
          gtable_add_grob(rectGrob(gp=gpar(fill=NA,col="red")),
          t=1, l=1, r=ncol(tt),b=nrow(tt))

          th <- sum(tt$heights)
          pg <- arrangeGrob(tt, pp, heights = unit.c(th, unit(1,"npc")-th))

          pdf("Demo.pdf", width=21/2.54,height=27.7/2.54)
          grid.draw(pg)
          dev.off()


          Note that with this approach the ggplot will expand to fill the remaining space. If you don't want that, you should assign a fixed height, e.g.



          # 10 cm or remaining space on the page if smaller than that
          cappedheight = unit.pmin(unit(1,"npc"), unit(10, "cm"))
          # wrap plot in a grobTree to assign a viewport (centered in remaining space)
          pp1 <- grobTree(ggplotGrob(pp), vp=viewport(height=cappedheight))
          pg <- arrangeGrob(tt, pp1, heights = unit.c(th, unit(1,"npc")-th))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 20:45









          user10630867

          112




          112











          • Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
            – JenB
            Nov 10 at 11:45
















          • Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
            – JenB
            Nov 10 at 11:45















          Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
          – JenB
          Nov 10 at 11:45




          Thank you. I will try this on Monday at work with the real table and accept the answer then if it fixes my problem.
          – JenB
          Nov 10 at 11:45

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211177%2fvertical-justification-and-heights-in-arrangegrob%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo