add superscript in table using tableGrob

僤鯓⒐⒋嵵緔 提交于 2020-01-15 07:32:25

问题


How to add superscript in table? For instance, the column b of df would indicate the duplicate index as superscript.

I could think of introducing the values of column b as expression, but there may be better way of doing it.

Data:

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

Code:

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

Output:

Expected:


回答1:


You can do this by creating the appropriate 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 for additional details and examples.

# 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)



来源:https://stackoverflow.com/questions/43714570/add-superscript-in-table-using-tablegrob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!