如何为R中的tableGrob定制特定的列? [英] How do I customize particular columns for a tableGrob in R?

查看:742
本文介绍了如何为R中的tableGrob定制特定的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在我的tableGrob中定制特定的列,对于这个可重复的例子,我选择了查看自定义的对齐方式。假设您有以下数据框:

  df < data.frame(Order = c(1:3),Name = c(Adam,Ben,Charlie),Score = c(4,8,9))

并且您想使用包 gridExtra 来显示表格:

  dfGrob<  -  tableGrob(df,rows = NULL)
grid.arrange(dfGrob)

您可以通过调整用于构建grob的主题来调整列的对齐方式,例如:

  tt1 < -  ttheme_default(core = list(fg_params = list(hjust = 0,x = 0.05)),
colhead = list(fg_params =列表(hjust = 0,x = 0.1))


dfGrob< - tableGrob(df,rows = NULL,theme = tt1)
grid.arrange(dfGrob)

但是,这会调整所有列的对齐。说我只想左右秩序栏,让其他人放在他们中心的理由位置,我该如何做呢?



我已经尝试过:

  tt1<  -  ttheme_default core = list(fg_params = list(hjust = c(0,0.5,0.5),x = c(0.15,0.5,0.5))),
colhead = list(fg_params = list(hjust = 1,x = 0.95)))

dfGrob< - tableGrob(df,rows = NULL,theme = tt1)
grid.arrange(dfGrob)
/ pre>

但这似乎是按行自定义的。如何调整此代码以按列自定义?

解决方案

它有点fiddly,但您可以指定所有元素的参数,

 库(网格)
库(gridExtra)

df< - data.frame(Order = c(1:3)
Name = c(Adam,Ben,Charlie),
Score = c(4,8,9))

hj< - matrix c(0,0.5,1),ncol = 3,nrow = nrow(df),byrow = TRUE)
x <矩阵(c(0,0.5,1)),ncol = 3,nrow = df),byrow = TRUE)

tt1 <-ttheme_default(core = list(fg_params = list(hjust = as.vector(hj),
x = as.vector(x)) ),
colhead = list(fg_params = list(hjust = 1,x = 0.95))


dfGrob< - tableGrob(df,rows = NULL,theme = tt1)
grid.newpage()
grid.draw(dfGrob)


I'm looking to customise particular columns in my tableGrob, for this reproducible example I have chosen to look at customising justification.

Say you have the following dataframe:

df <- data.frame(Order = c(1:3), Name = c("Adam", "Ben", "Charlie"), Score = c(4, 8, 9))

And you want to use the package gridExtra to present the table:

dfGrob <- tableGrob(df, rows = NULL)
grid.arrange(dfGrob)

You can adjust the alignment of the columns by adjusting the theme used to build the grob, for example:

tt1 <- ttheme_default(core=list(fg_params=list(hjust= 0, x=0.05)),
                  colhead=list(fg_params=list(hjust=0, x=0.1)))


dfGrob <- tableGrob(df, rows = NULL, theme = tt1)
grid.arrange(dfGrob)

However, this adjusts the justification for all columns. Say I just want to left justify the Order Column, and leave the others in their central justification position, how would I do that?

I have experimented with:

tt1 <- ttheme_default(core=list(fg_params=list(hjust= c(0, 0.5, 0.5), x=c(0.15, 0.5, 0.5))),
                  colhead=list(fg_params=list(hjust=1, x=0.95)))

dfGrob <- tableGrob(df, rows = NULL, theme = tt1)
grid.arrange(dfGrob)

But this just seems to customise by row. How do I adjust this code to customise by column instead?

解决方案

it's a bit fiddly but you can specify the parameters for all elements,

library(grid)
library(gridExtra)

df <- data.frame(Order = c(1:3),
                 Name = c("Adam", "Ben", "Charlie"), 
                 Score = c(4, 8, 9))

hj <- matrix(c(0, 0.5, 1), ncol=3, nrow=nrow(df), byrow=TRUE)
x <- matrix(c(0, 0.5, 1), ncol=3, nrow=nrow(df), byrow=TRUE)

tt1 <- ttheme_default(core=list(fg_params=list(hjust = as.vector(hj), 
                                               x = as.vector(x))),
                      colhead=list(fg_params=list(hjust=1, x=0.95)))


dfGrob <- tableGrob(df, rows = NULL, theme = tt1)
grid.newpage()
grid.draw(dfGrob)

The recycling logic defaults to column wise, because most often a table has rows of alternating colours. It should be possible to special-case the horizontal justification parameters to make this a bit more user-friendly. Feel free to submit a PR.

这篇关于如何为R中的tableGrob定制特定的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆