Sathish
Sathish

Reputation: 12713

add superscript in table using tableGrob

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:

enter image description here

Expected:

enter image description here

Upvotes: 3

Views: 1481

Answers (1)

eipi10
eipi10

Reputation: 93811

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)

enter image description here

Upvotes: 3

Related Questions