You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除ggtexttable生成表格的白色背景?

问题:移除ggtexttable生成表格的白色背景

我使用ggpubr包绘制表格,编写的R代码如下:

library(ggpubr)

ds12 <- c("N of individuals", "Eff N after removing Ref", "N of matched sex individuals", "N of failing sex check")
ds13 <- c(903, 893, 892, 1)
ds14 <- cbind(ds12, ds13)
colnames(ds14) <- NULL

ggtexttable(ds14, rows = NULL, theme = ttheme("mBlue", base_size = 30))

运行后得到带有白色背景的表格,希望移除白色背景,实现无背景的表格效果。


解决方案

有两种简单的方法可以实现背景透明的效果:

方法1:自定义表格主题

通过ttheme()函数的参数,将表格各区域的背景色设置为透明,同时保留原主题的字体、边框等样式:

library(ggpubr)

ds12 <- c("N of individuals", "Eff N after removing Ref", "N of matched sex individuals", "N of failing sex check")
ds13 <- c(903, 893, 892, 1)
ds14 <- cbind(ds12, ds13)
colnames(ds14) <- NULL

# 自定义透明背景的主题
custom_transparent_theme <- ttheme(
  "mBlue", 
  base_size = 30,
  core = list(fill = "transparent"),       # 表格主体内容区域背景透明
  colhead = list(fill = "transparent"),    # 表头背景透明(本例无表头,可保留)
  rowhead = list(fill = "transparent")     # 行头背景透明(本例无行头,可保留)
)

ggtexttable(ds14, rows = NULL, theme = custom_transparent_theme)

方法2:叠加ggplot主题修改

ggtexttable()返回的是ggplot对象,因此可以直接用theme()函数修改面板和绘图区域的背景:

library(ggpubr)

ds12 <- c("N of individuals", "Eff N after removing Ref", "N of matched sex individuals", "N of failing sex check")
ds13 <- c(903, 893, 892, 1)
ds14 <- cbind(ds12, ds13)
colnames(ds14) <- NULL

ggtexttable(ds14, rows = NULL, theme = ttheme("mBlue", base_size = 30)) +
  theme(
    panel.background = element_rect(fill = "transparent"),
    plot.background = element_rect(fill = "transparent", color = NA)
  )

额外提示

如果需要导出透明背景的图片,使用ggsave()时要指定bg = "transparent":

ggsave("transparent_table.png", plot = last_plot(), bg = "transparent")

内容的提问来源于stack exchange,提问作者Khaleesi95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 11:50:49