用 “grid.table” 遮蔽表格的第一列。

huangapple go评论71阅读模式
英文:

Shade the first column of the table with "grid.table"

问题

I want values 7, 8, 9 and 10 to also appear shaded darker in the table below. I have used

我希望下表中的数值7、8、9和10也以较深的阴影出现。我已经使用了

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

myt <- ttheme_default(
colhead = list(fg_params=list(col="black"),
bg_params=list(fill="grey")),
rowhead = list(fg_params=list(col="black", fontface="bold",hjust=0.5, x=0.5),
bg_params=list(fill="grey")))

grid.table(cbind("X\Y"=rownames(public)[1:4], public[1:4,1:4]),rows=NULL)

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

NEW
Now, I can't manage to make the first column bold

现在,我无法使第一列变粗

myt <- ttheme_default(
colhead = list(fg_params=list(col="black"),
bg_params=list(fill="grey")),
rowhead = list(fg_params=list(col="black", fontface="bold",hjust=0.5, x=0.5),
bg_params=list(fill="grey")))

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

rs = tableGrob(cbind("X\Y"=rownames(public)[1:4]), rows=NULL, theme=ttheme_default(core=list(bg_params=list(fill="grey"))))

grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

Perfect with these codes:

使用以下代码完美解决:

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

rs = tableGrob(cbind("X\Y"=rownames(public)[1:4]),
rows=NULL,
theme=ttheme_default(core=list(bg_params=list(fill="grey"),
fg_params=list(fontface="bold"))))

grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

英文:

I want values 7, 8, 9 and 10 to also appear shaded darker in the table below. I have used

grid.table(public)

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

myt &lt;- ttheme_default(
        colhead = list(fg_params=list(col=&quot;black&quot;),
                     bg_params=list(fill=&quot;grey&quot;)),
         rowhead = list(fg_params=list(col=&quot;black&quot;, fontface=&quot;bold&quot;,hjust=0.5, x=0.5),
                       bg_params=list(fill=&quot;grey&quot;)))



grid.table(cbind(&quot;X\\Y&quot;=rownames(public)[1:4], public[1:4,1:4]),rows=NULL)

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

NEW
Now, I can't manage to make the first column bold

 myt &lt;- ttheme_default(
            colhead = list(fg_params=list(col=&quot;black&quot;),
                         bg_params=list(fill=&quot;grey&quot;)),
             rowhead = list(fg_params=list(col=&quot;black&quot;, fontface=&quot;bold&quot;,hjust=0.5, x=0.5),
                           bg_params=list(fill=&quot;grey&quot;)))
    
    
    
    
    grid::grid.newpage()
    grid.table(public[1:4,1:4], theme=myt)
    
    
    rs = tableGrob(cbind(&quot;X\\Y&quot;=rownames(public)[1:4]), rows=NULL, theme=ttheme_default(core=list(bg_params=list(fill=&quot;grey&quot;))))
    
    grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

用 “grid.table” 遮蔽表格的第一列。

用 “grid.table” 遮蔽表格的第一列。

Perfect with these codes:

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

rs = tableGrob(cbind(&quot;X\\Y&quot;=rownames(public)[1:4]), 
               rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill=&quot;grey&quot;), 
                                              fg_params=list(fontface=&quot;bold&quot;))))

grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

答案1

得分: 1

以下是翻译好的部分:

这些数字是行名称(您可以使用 grid.table(iris[1:4,1:4])) 查看它们),因此您需要更改相关的主题元素,即 rowhead 参数。

例如,

library(gridExtra)

# 新主题参数
myt <- ttheme_default(
         rowhead = list(fg_params=list(col="white"),
                        bg_params=list(fill="red")))

grid::grid.newpage()
grid.table(iris[1:4,1:4], theme=myt)

详细信息可以在包文档中找到。


来自评论:

是否可以在左上角放置 X\Y,并保留第一列的阴影?

有几种方法可以做到这一点。首先,获取行名称并形成一个 tableGrob,然后按您的喜好进行格式化,然后将其与数据连接到 tableGrob(请注意,现在使用 rows=NULL 抑制了行名称)。例如,

# 行名称和格式化
rs = tableGrob(data.frame("x\\y"=rownames(iris)[1:4], check.names=FALSE), rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill="grey70"))))
               
# 结合并绘制
grid::grid.draw(
         gtable_combine(rs, tableGrob(iris[1:4, 1:4], rows=NULL))
         ) 

来自评论:现在,我无法使第一列加粗。我不知道在哪里放置 fontface="bold"

rs = tableGrob(data.frame("x\\y"=rownames(iris)[1:4], check.names=FALSE), 
               rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill="grey70"), 
                                              fg_params=list(fontface="bold"))))
英文:

Those numbers are the row names (you can see them with grid.table(iris[1:4,1:4])), and so you need to change the relevant theme elements, which are the rowhead parameters.

For example,

library(gridExtra)

# New theme paramters
myt &lt;- ttheme_default(
         rowhead = list(fg_params=list(col=&quot;white&quot;),
                        bg_params=list(fill=&quot;red&quot;)))

grid::grid.newpage()
grid.table(iris[1:4,1:4], theme=myt)

Details can be found in the package vignette


From comments;

Is it possible to put in the upper left square, X\Y and keep the shading in the first column?

There are a few ways to do this. First, grab the row names and form a tableGrob and format this how you would like, the second join to the tableGrob with the data (note that the rows names are now suppressed with rows=NULL). e.g.

# row names &amp; formatting
rs = tableGrob(data.frame(&quot;x\\y&quot;=rownames(iris)[1:4], check.names=FALSE), rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill=&quot;grey70&quot;))))

# combine and draw
grid::grid.draw(
         gtable_combine(rs, tableGrob(iris[1:4, 1:4], rows=NULL))
         ) 

From comments: Now, I cannot make the first column bold. I don't know where to put fontface="bold".

rs = tableGrob(data.frame(&quot;x\\y&quot;=rownames(iris)[1:4], check.names=FALSE), 
               rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill=&quot;grey70&quot;), 
                                              fg_params=list(fontface=&quot;bold&quot;))))

huangapple
  • 本文由 发表于 2023年4月19日 16:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定