rgrobr-rownames

Row names in tableGrob are cut off when using row.just = "left


Can anyone solve why the cutoff of row names occurs for me? It is independent of the length of the string wrap. Specifying row.just = "center" does not cut off the row names.

x <- data.frame(row.names=paste("Very very name goes in here somewhere yep it is a looooonnngggg name! phew that was a long name",1:10))

# string wrap long names    
rownames(x) <- sapply(lapply(rownames(x), strwrap, width=40), paste, collapse="\n")

# data frame    
x[,1] <- 1:10
x[,2] <- sample(1:100,10)
x[,3] <- sample(LETTERS[1:26],10)
colnames(x) <- c("Value 1", "Value 2", "Label")

# create table
main_table <- tableGrob(x,cols = colnames(x), show.colnames = TRUE, row.just = "left")

# display table (is there another way to display?
grid.arrange(main_table)

Gives me this (sorry about the zoom)

Left aligned table

whereas specifying "center" gives me this

main_table <- tableGrob(x,cols = colnames(x), show.colnames = TRUE, row.just = "center")
grid.arrange(main_table)

Center aligned table

Any ideas?

p.s. I'm not sure why the images are like that, when I click 'zoom' in the plotting window they are full tables, but saving/exporting only saves the zoomed-in version...


Solution

  • i'm guessing it's because the available width is calculated from the string width, but justification shifts the text to the right. hjust/x interactions have always confused me in grid. You can "fix" it with,

    textii <- function(d, gp=gpar(), name="row-label-",
                       just="center", parse=TRUE){
      x <- switch(just, "center"=0.5, "right"=1, "left"=0)
      parseglobal <- parse
      ##   allow the correct space to fit well in a rectangle
      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")
    

    but that's not a very good solution I'm afraid.