使用 tableGrob 在 table 中添加上标

add superscript in table using tableGrob

如何在table中添加上标?例如,df 的列 b 会将重复索引指示为上标。

我可以考虑引入列 b 的值作为表达式,但可能有更好的方法。

数据:

df <- data.frame( a = 1:6, b = rep( letters[1:3], each = 2 ) )

代码:

library( 'gridExtra' )
library( 'grid' )
tg_df <- tableGrob( d = df )
grid.draw( tg_df )

输出:

预期:

您可以通过创建适当的 plotmath superscript strings and specifying parse=TRUE in a theme statement in order to parse the plotmath expression in the table grob. See the vignette 来获取更多详细信息和示例。

# Create plotmath superscript strings
df$b = paste0(df$b,"^",rep(1:2,3))

# Define theme to parse plotmath expressions
tt = ttheme_default(core=list(fg_params=list(parse=TRUE)))

tg_df <- tableGrob(d = df, theme=tt)
grid.draw(tg_df)