adkane
adkane

Reputation: 1441

How can I change the colour of the column header in geom_table() in R?

I'm using the geom_table() function from the ggpmisc package to add a table legend to my figure. I want to remove the grey colour from the first row with the column headers.

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) 

enter image description here

Upvotes: 0

Views: 717

Answers (1)

juljo
juljo

Reputation: 674

You can set the theme using arguments that are passed from ggpmisc to the corresponding ttheme function from gridExtra (Description of some of the possible options). If I understand your question correctly you want the background of the first row in your table to be white. You can achieve this using the following code to build your plot:

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"))))

Result with white first row in table

Upvotes: 2

Related Questions