Style cells and values in a table for publication

You are making a table for publication, and you want to adjust things like the size and color of text, or the color or border of a cell.

Step 1 - Start with a data frame that contains the values that you want to display in the arrangement that you want to display them.

Step 2 - Use gt::gt() to turn your data frame into an editable, professional looking table.

Step 3 - Call gt::tab_style() to begin styling your table. Within tab_style you can specify what to style and at which cell locations to style it.

Step 4 - Use a locations helper function, listed below, to specify the locations of the table to style.

Step 5 - Use a style helper function, listed below, to style a cell’s background, borders, or text elements.

Step 6 - Set style options within the style helper function. Each style helper function has multiple elements to modify. Below we style the text in column label cells with color, size, and weight.

data %>% 
  gt() %>% 
  tab_style(
    locations = cells_column_labels(),
    style = cell_text(
      color = "red", 
      size = "large", 
      weight = "bold"
    )
  )

locations helper functions

Use a function below to target specific cell locations within tab_style():

Function Cells targeted
gt::cells_title() table title or subtitle
gt::cells_stubhead() stubhead location
gt::cells_column_spanners() spanner column labels
gt::cells_column_labels() column labels
gt::cells_row_groups() row group labels
gt::cells_stub() row labels in the table stub
gt::cells_body() cells in the table body using the intersection of rows and columns
gt::cells_summary() summary cells using the groups argument
gt::cells_grand_summary() grand summary cells of table
gt::cells_stub_summary() summary row labels in the table stub
gt::cells_stub_grand_summary() grand summary row labels in table stub

style helper functions

Use a function below to customize style formats in tab_style():

Function Elements to style
gt::cell_fill() cell background color
gt::cell_border() cell borders color, style, and weight
gt::cell_text() cell text color, font, size, style, weight, alignment, and indentation

Example

store_sales describes the number of various Quaker oatmeal products sold at three stores during a week.

store_sales
# A tibble: 4 × 4
  product                    store8 store21 store44
  <chr>                       <int>   <int>   <int>
1 Quaker Instant Grits 12 oz     20      12       3
2 Quaker Kids Choice 14.3 oz      3       7      11
3 Quick Quaker Oats 18 oz        55      21      53
4 Quick Quaker Oats 42 oz       123      18      52

Before we publish the table, we’d like to make the column labels bold and place them on a light green background.

To do this, we load the gt package and then convert our data frame to a gt table. Then within tab_style() we target the locations to style with the helper function cells_column_labels(). Lastly, we set style to a list of helper functions to create the style we want with the specified style arguments.

library(gt)
store_sales %>% 
  gt() %>% 
  tab_style(
    locations = cells_column_labels(),
    style = list(
      cell_text(weight = "bold"),
      cell_fill(color = "green", alpha = 0.3)
      )
  ) 
product store8 store21 store44
Quaker Instant Grits 12 oz 20 12 3
Quaker Kids Choice 14.3 oz 3 7 11
Quick Quaker Oats 18 oz 55 21 53
Quick Quaker Oats 42 oz 123 18 52

Pass the style argument of tab_style() a list of functions to simultaneously style more than one element of a location.

Style formatting in SAS

tab_style() is the equivalent to SAS’s STYLE= option within the REPORT procedure which also has many customizable style options.

In SAS:

PROC REPORT DATA=data;
  STYLE(HEADER)={COLOR=RED FONTSIZE=5 FONTWEIGHT=BOLD BACKGROUNDCOLOR=GRAY};
RUN;

In R:

data %>% 
  gt() %>% 
  tab_header("Title for publication table") %>% 
  tab_style(
    style = list(
      cell_text(
        color = "red", 
        size = "large", 
        weight = "bold"
      ),
      cell_fill(color = "gray")
    ),
    locations = cells_title()
  )