2

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...

M--
  • 25,431
  • 8
  • 61
  • 93
Alex
  • 971
  • 4
  • 15
  • 28

1 Answers1

2

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.

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Hi mate, I've just come back to this after viewing briefly over the weekend. Can you please explain how/where to implement your solution in my initial code? Sorry, I am quite new and it isn't obvious to me. – Alex Jun 16 '15 at 04:37
  • the problem is internal to the gridExtra package, so to fix it you can either edit the package source and reinstall it, or, as a one-time workaround, just copy and paste the above code in your R console. It will replace the internal definition of the `textii` function with this new one, for the duration of your session. – baptiste Jun 16 '15 at 04:44