Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text when using tableGrob or grid.table in R

Tags:

r

When creating a table using tableGrob or grid.table. Is there way to align the text inside the table? First column to the left, and the other columns to the right? Rather than the default "center".

Thank you!

something like this: where I want column "a" alligned to the left.

a <- c("one","two","thirty five")
b <- c(1, 2, 3)
c <- c(4, 5, 6)

data <- data.frame(a,b,c)

windows()

grid.table(
  data,
  gpar.coretext=gpar(fontsize = 12),            
  gpar.coltext = gpar(fontsize = 12),            
  gpar.rowtext = gpar(fontsize = 12),            
  gpar.corefill = 
    gpar(fill = "green", 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")            
) 
like image 468
tus Avatar asked Oct 11 '12 16:10

tus


3 Answers

To set a "transparent" background, use the ttheme_minimal with hjust to set text alignment.

theme_1 <- ttheme_minimal(core = list(fg_params = list(hjust = 0, 
                                                           x = 0.1, 
                                                    fontsize = 9)),
                          colhead = list(fg_params = list(fontsize = 12, 
                                                          fontface = "bold")))

You can then apply the theme to the tableGrob like this:

gridExtra::tableGrob(df_tbl, theme = theme_1, rows=NULL)
like image 160
Antex Avatar answered Oct 26 '22 12:10

Antex


Is this what you are looking for? There is a core.just parameter of the format() call.

require("gridExtra")

n=5
df<- data.frame(x=rnorm(n),y=rnorm(n),z=sample(letters[1:2],n,replace=T))


g1<-tableGrob(
format(df, digits = 1,
     scientific=F,big.mark = ","),
     core.just="left",
     #core.just="right",
     #col.just="right",
     gpar.coretext=gpar(fontsize=8), 
     gpar.coltext=gpar(fontsize=9, fontface='bold'), 
     show.rownames = F,
     h.even.alpha = 0,
     gpar.rowtext = gpar(col="black", cex=0.7,
                            equal.width = TRUE,
                            show.vlines = TRUE, 
                            show.hlines = TRUE,
                            separator="grey")                     
)

grid.draw(g1)
like image 43
ako Avatar answered Oct 26 '22 14:10

ako


With gridExtra v>=2.0.0, the parameters are now controlled via nested lists (themes),

library(gridExtra)
library(grid)
n=5
d <- data.frame(x=rnorm(n),y=rnorm(n),z=sample(letters[1:2],n,replace=T))

m <- format(d, digits = 1, scientific=F,big.mark = ",")

mytheme <- ttheme_default(core = list(fg_params = list(hjust=0, x=0.1, 
                                                       fontsize=8)),
                          colhead = list(fg_params = list(fontsize=9, 
                                                          fontface="bold"))
                          )
g1 <- tableGrob(m, theme = mytheme, rows=NULL)
grid.newpage()
grid.draw(g1)
like image 36
baptiste Avatar answered Oct 26 '22 13:10

baptiste