Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add superscript in table using tableGrob

Tags:

r

gridextra

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

like image 531
Sathish Avatar asked Jul 15 '26 20:07

Sathish


1 Answers

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

like image 96
eipi10 Avatar answered Jul 17 '26 15:07

eipi10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!