alex_555
alex_555

Reputation: 1102

Align colhead in tableGrob to the left without clipping the colnames

I'd like to create a tableGrob and I need some advanced formatting, e.g. rotating its colhead (90 degrees). Unfortunately, the formatting leaves me with clipped colnames similar to this question: Incorrect left align using tableGrob.

As you can see in the example underneath, the clipping occurs along with the colnames not being aligned to the left, meaning directly on the first row considering the rotation. This problem occurs also when setting the rotation angle to 0.

library(gridExtra)
x <- head(iris)

grid_table <- tableGrob(x, rows=NULL, cols= colnames(x),
                    theme=ttheme_minimal(
                      base_size=font_size,
                      padding = unit(c(1.5,1.5), "mm"),
                      core=list(fg_params=list(x=0, hjust=0, fontface=1)),
                      col.just="left",
                      colhead=list(fg_params=list(x=0.3, hjust=0.3,
                                                  fontface=2, rot=90))))

grid.arrange(grid_table)

I tried to override the textii function as suggested in the question above, but I only come up with the following error: Error in bindingIsLocked(x, ns) : no binding for "textii". I'm using gridExtra version 2.3.

Any suggestions how to align the colhead to the first row and how to avoid the clipping effect?

Thank you in advance!

EDIT: The error occurs when overriding textii as follows:

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")

Upvotes: 0

Views: 412

Answers (1)

user10352446
user10352446

Reputation: 26

it's not clear to me what alignment you seek but here are three options for vertical justification of the rotated labels,

library(gridExtra)
x <- head(iris)

g1 <- tableGrob(x, rows=NULL, cols= colnames(x),
                        theme=ttheme_minimal(
                          base_size=12,
                          padding = unit(c(1.5,1.5), "mm"),
                          core=list(fg_params=list(x=0, hjust=0, fontface=1)),
                          colhead=list(fg_params=list(hjust=0, y=0, 
                                                      fontface=2, rot=90))))


g2 <- tableGrob(x, rows=NULL, cols= colnames(x),
                theme=ttheme_minimal(
                  base_size=12,
                  padding = unit(c(1.5,1.5), "mm"),
                  core=list(fg_params=list(x=0, hjust=0, fontface=1)),
                  colhead=list(fg_params=list(hjust=0.5, y=0.5, 
                                              fontface=2, rot=90))))


g3 <- tableGrob(x, rows=NULL, cols= colnames(x),
                theme=ttheme_minimal(
                  base_size=12,
                  padding = unit(c(1.5,1.5), "mm"),
                  core=list(fg_params=list(x=0, hjust=0, fontface=1)),
                  colhead=list(fg_params=list(hjust=1, y=1, 
                                              fontface=2, rot=90))))

grid.arrange(g1,g2,g3,nrow=1)

Upvotes: 1

Related Questions