Format the title of a table for publication

You are making a table for publication, and you want to add a title to your table.

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 - Set title in gt::tab_header() to the title for your table.

data %>% 
  gt() %>% 
  tab_header(
    title = "Title for my table"
  )

Add a subtitle to your table

Use the subtitle argument in tab_header() to add a subtitle to your table.

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 add a title and subtitle. To do this, we load the gt package and then convert our data frame to a gt table. The we use tab_header() to add the title and subtitle to our table.

store_sales %>% 
  gt() %>% 
  tab_header(
    title = "Number of Quaker Oatmeal Products Sold by Stores",
    subtitle = "Time period: one week"
  )
Number of Quaker Oatmeal Products Sold by Stores
Time period: one week
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

Style your table titles

gt::md() and gt::html() helper functions to style text for the title or subtitle as Markdown or HTML elements. Below we bold the table title:

store_sales %>% 
  gt() %>% 
  tab_header(
    title = md("**Number of Quaker Oatmeal Products Sold by Stores**"),
    subtitle = md("according to the `store_sales` dataset")
  )

Add title to SAS tables

To add a title to a SAS table we use the TITLE option within procedures such as PRINT or TABULATE.

In SAS:

PROC PRINT data = publish_data;
  TITLE1 "Title for table";
  TITLE2 "subtitle to explain"
RUN;

In R:

publish_data %>% 
  gt() %>% 
  tab_header(
    title = "Title for table",
    subtitle = "subtitle to explain"
  )