r - Adding text to a grid.table plot -


i have started using grid.table function gridextra package turn tabular data png image files use on web. i've been delighted far produces good-looking output default, sort of ggplot2 tables. person asked question love see ability specify justification individual columns icing on more-ish cake.

my question whether possible add text around grid.table can give plotted tables title , footnote. seems me should feasible, don't know enough grid graphics able work out how add grobs table grob. example, code:

require(gridextra)  mydf <- data.frame(item = c('item 1','item 2','item 3'),                      value = c(10,15,20), check.names = false) grid.table(mydf,            gpar.coretext=gpar(fontsize = 16),            gpar.coltext = gpar(fontsize = 16),            gpar.rowtext = gpar(fontsize = 16),            gpar.corefill = gpar(fill = "blue", alpha = 0.5, col = na),            h.even.alpha = 0.5,            equal.width = false,            show.rownames = false,            show.vlines = true,            padding.h = unit(15, "mm"),            padding.v = unit(8, "mm")            ) 

generates plot:

enter image description here

when able following in code rather editing image application:

enter image description here

to place text close table you'll want evaluate table size first,

library(gridextra) d <- head(iris) table <- tablegrob(d)  grid.newpage() h <- grobheight(table) w <- grobwidth(table) title <- textgrob("title", y=unit(0.5,"npc") + 0.5*h,                    vjust=0, gp=gpar(fontsize=20)) footnote <- textgrob("footnote",                       x=unit(0.5,"npc") - 0.5*w,                      y=unit(0.5,"npc") - 0.5*h,                    vjust=1, hjust=0,gp=gpar( fontface="italic")) gt <- gtree(children=glist(table, title, footnote)) grid.draw(gt) 

edit (17/07/2015) gridextra >=2.0.0, approach no longer suitable. tablegrob returns gtable, can more customised.

library(gridextra) d <- head(iris) table <- tablegrob(d)  library(grid) library(gtable)  title <- textgrob("title",gp=gpar(fontsize=50)) footnote <- textgrob("footnote", x=0, hjust=0,                      gp=gpar( fontface="italic"))  padding <- unit(0.5,"line") table <- gtable_add_rows(table,                           heights = grobheight(title) + padding,                          pos = 0) table <- gtable_add_rows(table,                           heights = grobheight(footnote)+ padding) table <- gtable_add_grob(table, list(title, footnote),                          t=c(1, nrow(table)), l=c(1,2),                           r=ncol(table)) grid.newpage() grid.draw(table) 

Comments