4

I have recently started using tableGrob and gridextra to combine multiple plots and tables. i want mt tableGrob to have a footnote and title.

The following link answers that nicely: Adding text to a grid.table plot

But in the above code the footnote gets truncated it its too long. Can someone please suggest an alternative so that the footnote automatically wraps to the next line once it had reached the end of the table? If it can wrap in the middle of the word that is fine as well.

test <- data.frame(boo = c(20,1), do = c(2,10), no = c(3,5),co = c('ed','jeff'))

t1 <- tableGrob(test)

tw <- convertWidth(unit(grobWidth(t1),'npc'),
                   "in", valueOnly = T)

title <- textGrob("Title is long too or is it??",gp=gpar(fontsize=15))
footnote <- textGrob("footnote is pretty longgg but not unusually longgggggggggkjwd jwkldn", x=0, hjust=0,
                     gp=gpar( fontface="italic"))

padding <- unit(0.5,"line") 

t1 <- gtable_add_rows(t1, 
                        heights = grobHeight(title) + padding,
                        pos = 0)
t1 <- gtable_add_rows(t1, 
                        heights = grobHeight(footnote)+ padding)
t1 <- gtable_add_grob(t1, list(title, footnote),
                        t=c(1, nrow(t1)), l=c(1,1), 
                        r=ncol(t1))

grid.arrange(t1)

Long footnote tableGrob

I want this to work when I have a plot and a table in grid arrange as well. Please help.

I tried using strwrap and setting the width to grobWidth but it did not work for me.

Community
  • 1
  • 1
StatMan
  • 135
  • 8

1 Answers1

4

the RGraphics book/package offers a possible solution,

splitString <- function (text, width) {
  strings <- strsplit(text, " ")[[1]]
  newstring <- strings[1]
  linewidth <- stringWidth(newstring)
  gapwidth <- stringWidth(" ")
  availwidth <- convertWidth(width, "in", valueOnly = TRUE)
  for (i in 2:length(strings)) {
    width <- stringWidth(strings[i])
    if (convertWidth(linewidth + gapwidth + width, "in", 
                     valueOnly = TRUE) < availwidth) {
      sep <- " "
      linewidth <- linewidth + gapwidth + width
    }
    else {
      sep <- "\n"
      linewidth <- width
    }
    newstring <- paste(newstring, strings[i], sep = sep)
  }
  newstring
}


tit <- "Title is long too or is it??"
foot <- "footnote is pretty longgg but not unusually longgggggggggkjwd jwkldn"
footnote <- textGrob(splitString(foot, sum(t1$widths)))
title <- textGrob(splitString(tit, sum(t1$widths)))
t1 <- gtable_add_rows(t1, heights = grobHeight(footnote))
t1 <- gtable_add_rows(t1, heights = grobHeight(title), 0)
t1 <- gtable_add_grob(t1, list(title, footnote),
                      t=c(1, nrow(t1)), l=1, r=ncol(t1))

grid.draw(t1)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • This works great Baptiste.Couple of things that don't work well: 1) Is there any way the footnote could break from the middle of the word? 2) Can the width automatically detect the font size of the footnote as well inorder to use the width of the table? For example, when I use a fontsize of 7 or 10 would make a difference on and if it's too long it would just be cropped at the end of the table? It would be great if the wrapping could be adjusted according to the footnote fontsize. This might be asking for too much! :) But the above solution worked great! – StatMan Nov 18 '15 at 04:56