首页 > 解决方案 > 如何在 R 表的标题中写上标?

问题描述

我正在尝试将上标添加到表格的标题中。该表稍后将用作条形图的标签。因此,我想缩写一些表格标题,并在稍后的图表图例中解释这些缩写。

我使用Sathish之前提供的示例

# libraries
library(gridExtra)   # for cool table
library(grid)        # for cool table

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

# Create plotmath superscript strings
df[1,1] = paste0(df[1,1],"^",1)              # i unsuccessfully tried to change the example
colnames(df)[1] <- expression('Title'^2)     # here another unsuccessful try

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

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

grid.newpage()
grid.draw(tg_df)

这给了我 在此处输入图像描述

谢谢你的帮助。

标签: rheadergridextrasuperscript

解决方案


在tableGrob vignette中有一个例子。只需确保也解析列标题

tt <- ttheme_default(
  core = list(fg_params = list(parse=TRUE)), 
  colhead = list(fg_params = list(parse=TRUE))
)

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

grid.newpage()
grid.draw(tg_df)

在此处输入图像描述


推荐阅读