简体   繁体   中英

Align text when using tableGrob or grid.table in 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")            
) 

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)

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM