r - 如何在R表的表头中写入上标?

标签 r header gridextra superscript

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

我使用example之前由 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)

这给了我 enter image description here

感谢您的帮助。

最佳答案

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)

enter image description here

关于r - 如何在R表的表头中写入上标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60099152/

相关文章:

r - GridExtra:将文本向右对齐

R dplyr 滚动总和

r - 通过 knitr、gWidgetsWWW 使用时出现 data.table 错误

c++ - CMake和QT多重定义

c++ - 系统头文件重新定义宏: how do I tell gcc to ignore?

objective-c - 更改 NSTableView 标题的高度

r - 具有共享数据的多个 ggplot2 图表

r - 我如何摆脱 arrangeGrob 中的随机背景网格

r - 删除R的一列中具有非数字字符的行

r - 为多个变量分配相同的值