tableGrob:在使用annotation_custom绘制在ggplot上的表格中调整表格大小(更改字体大小)

3

我遇到了一些问题,想要调整使用tableGrob()和annotation_custom()绘制在图表上的表格中的文本大小。基本上,我希望表格中的字体大小更小,以便整个表格更小。我已经检查过tableGrob()文档并尽力按照其指示操作,但肯定做错了什么,因为它会报错。

下面是一个可重现的例子:

library(ggplot2)
library(grid)
library(gridExtra)

df <- data.frame(x=seq(1,10),y=seq(11,20))
table <- data.frame(x=seq(1,3),y=seq(4,6))

ggplot(df,aes(x=x,y=y)) + geom_point() +
 annotation_custom(tableGrob(table,rows=NULL),xmin=0,xmax=3,ymin=15,ymax=20) # plot drawn successfully without text resizing

ggplot(df,aes(x=x,y=y)) + geom_point() + 
  annotation_custom(tableGrob(table,rows=NULL,gpar.coretext = gpar(col = "black", cex = 0.8)),xmin=0,xmax=3,ymin=15,ymax=20)
 # error when attempting to resize text following tableGrob documentation

当我运行第二个ggplot()命令时,出现了以下错误:

Error in gtable_table(d, name = "core", fg_fun = theme$core$fg_fun, bg_fun = theme$core$bg_fun,  : 
  unused argument (gpar.coretext = list(col = "black", cex = 0.8))

任何帮助都非常感激!

1
这个回答解决了你的问题吗?如何调整tableGrob的字体大小 - Wolfgang Arnold
我觉得文档的问题是链接到旧版本 0.9.1 的包的文档,而当前版本是 2.3.0,帮助页面在这里:https://www.rdocumentation.org/packages/gridExtra/versions/2.3/topics/tableGrob。新版本更倾向于使用主题。 - MrFlick
1个回答

3
如果您只想让表格中的所有文本变小,请在`ttheme_default`中使用`base_size`:
library(ggplot2)
library(grid)
library(gridExtra)

df <- data.frame(x=seq(1,10),y=seq(11,20))
table <- data.frame(x=seq(1,3),y=seq(4,6))

ggplot(df,aes(x=x,y=y)) + 
  geom_point() + 
  annotation_custom(tableGrob(table,rows=NULL, theme = ttheme_default(base_size = 8)),
                    xmin=0,xmax=3,ymin=15,ymax=20)

reprex 包 (v0.3.0) 于2020年03月05日创建


非常感谢,这个有效!原来我看的是旧版本的文档。非常感谢您的回复。 - Anita Pandit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接