英文:
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也以较深的阴影出现。我已经使用了
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)))
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)
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)))
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)))
答案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 <- 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)
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 & formatting
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"))))
# 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("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"))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论