【问题标题】:add superscript in table using tableGrob使用 tableGrob 在表中添加上标
【发布时间】:2017-05-01 14:02:40
【问题描述】:

如何在表格中添加上标?例如,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 )

输出:

预期:

【问题讨论】:

    标签: r gridextra


    【解决方案1】:

    您可以通过创建适当的plotmath 上标字符串并在主题语句中指定parse=TRUE 来执行此操作,以便解析表 grob 中的 plotmath 表达式。有关更多详细信息和示例,请参阅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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      相关资源
      最近更新 更多