首页 > 解决方案 > 如何更改 R 中 geom_table() 中列标题的颜色?

问题描述

我正在使用包中的geom_table()函数ggpmisc将表格图例添加到我的图中。我想从带有列标题的第一行中删除灰色。

library(ggpmisc)
library(tidyverse)

mtcars %>%
  group_by(cyl) %>%
  summarize(wt = mean(wt), mpg = mean(mpg)) %>%
  ungroup() %>%
  mutate(wt = sprintf("%.2f", wt),
         mpg = sprintf("%.1f", mpg)) -> tb

df <- tibble(x = 5.45, y = 34, tb = list(tb))

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table(data = df, aes(x = x, y = y, label = tb),
             table.theme = ttheme_gtbw) 

在此处输入图像描述

标签: rggplot2plot

解决方案


您可以使用从(一些可能选项的描述ggpmisc) 传递给相应ttheme函数的参数来设置主题。如果我正确理解您的问题,您希望表格中第一行的背景为白色。您可以使用以下代码来构建您的绘图来实现此目的:gridExtra

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
    geom_point() +
    geom_table(data = df, aes(x = x, y = y, label = tb),
               table.theme = ttheme_gtbw(colhead = list(bg_params = list(fill = "white"))))

表中第一行白色的结果


推荐阅读