Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect left align using tableGrob

Tags:

r

gridextra

When I create a tableGrob with left justified strings, the final result shows the strings clipped at the end. Is a bug or I missed something?

library(gridExtra)
slices <- c(10, 12, 4, 16, 8) 
lbls <- c("US", "UK", "long string left justified but is clipped at the end", "Germany", "France")
z <- data.frame(lbls,slices)
grid.newpage()
grid.draw(tableGrob(z, core.just="left"))
like image 253
user2479764 Avatar asked Jun 12 '13 19:06

user2479764


1 Answers

It's a bug; the package author clearly doesn't understand text justification in grid.

You can sort of fix it like so,

textii <- function(d, gp=gpar(), name="row-label-",
                   just="center", parse=TRUE){
    x <- switch(just, "center"=0.5, "right"=1, "left"=0)
    parseglobal <- parse
    function(ii, parse=parseglobal){
        lab <- if(parse) parse(text=d[ii]) else d[ii]
        textGrob(x=x, label=lab, just=just, gp=gp, name=paste(name, ii, sep=""))
    }
}

assignInNamespace("textii", textii, "gridExtra")
grid.table(z, core.just="left")

and/or adjust the padding.h parameter to give more space. Sigh, what a mess.

like image 119
baptiste Avatar answered Oct 08 '22 04:10

baptiste