R: How to Create Publication-Ready Tables with gt or kableExtra

R: How to Create Publication-Ready Tables with gt or kableExtra
Image by Editor
 

Tables are essential for presenting research results and summaries in a clear, professional way. In R, creating publication-ready tables is made easier with packages like gt and kableExtra. The gt package offers a “grammar of tables” that lets you build and style outputs step by step. Meanwhile, kableExtra extends the simple knitr::kable() function with advanced formatting options. This article explores practical strategies for using gt and kableExtra to produce tables that meet publication standards.

Example Dataset

We’ll use the classic mtcars dataset. To keep things readable, we’ll select only a few variables and rows:

 
library(dplyr)

data <- mtcars %>%
  head(6) %>%
  select(mpg, cyl, hp, wt)

Creating Tables with gt

The gt package is designed for modern, attractive tables — especially for HTML output. It works by piping your data frame into gt() and then layering formatting functions.

Formatting Numbers

The fmt_number() function formats numeric columns with options for decimals, separators, and scaling. It helps make numbers more readable in your tables.

 
library(gt)

data %>%
  gt() %>%
  tab_header(
    title = "Car Performance Summary",
    subtitle = "First 6 rows of mtcars"
  ) %>%
  fmt_number(
    columns = c(mpg, hp, wt),
    decimals = 1
  )

formatting_numbers

Styling Column Labels

The tab_style() function enables you to format headers by adjusting text weight, color, and background. This makes column names visually distinct and helps highlight table structure.

 
library(gt)

data %>%
  gt() %>%
  tab_header(title = "Performance Data") %>%
  tab_style(
    style = list(
      cell_text(weight = "bold", color = "white"),
      cell_fill(color = "steelblue")
    ),
    locations = cells_column_labels(everything())
  )

styling_columns

Conditional Formatting

The data_color() function applies color gradients to cells based on their values. This conditional formatting makes it easy to spot high or low values at a glance.

 
library(gt)
library(scales)

data %>%
  gt() %>%
  data_color(
    columns = mpg,
    fn = col_numeric(
      palette = c("lightyellow", "darkgreen"),
      domain = NULL
    )
  )

conditional_formatting

Creating Tables with kableExtra

The kableExtra package extends knitr::kable() and adds advanced styling options. It’s useful for LaTeX/PDF outputs and provides fine-grained control over rows, columns, and captions.

Adding Style

With kable_styling(), you can apply options like stripes, booktabs, and alignment. This makes tables more readable and professional with minimal code.

 
library(knitr)
library(kableExtra)

kable(data, caption = "Car Performance (Striped)") %>%
  kable_styling(full_width = FALSE, position = "center", 
                latex_options = c("striped", "hold_position"))

adding_style

Highlighting Rows

The row_spec() function emphasizes specific rows by bolding, italicizing, or changing text/background colors. It’s useful for drawing attention to important results.

 
library(knitr)
library(kableExtra)

kable(data, caption = "Car Performance (Highlighted Row)") %>%
  kable_styling(full_width = FALSE, position = "center") %>%
  row_spec(1, bold = TRUE, color = "white", background = "steelblue") %>%
  row_spec(4, italic = TRUE, background = "lightgray")

highlighting_row

Customizing Columns

Using column_spec(), you can resize columns, change text styles, or apply background colors. This is helpful when certain variables deserve emphasis or need extra space.

 
library(knitr)
library(kableExtra)

kable(data, caption = "Car Performance (Custom Columns)") %>%
  kable_styling(full_width = FALSE, position = "center") %>%
  column_spec(1, bold = TRUE, width = "3cm") %>%
  column_spec(4, background = "lightyellow", width = "5cm")

customizing_column

Adding Footnotes

The footnote() function makes it easy to include general notes or column clarifications at the bottom of your table. Footnotes improve transparency and help explain abbreviations or data sources.

 
library(knitr)
library(kableExtra)

kable(data, caption = "Car Performance (With Footnote)") %>%
  kable_styling(full_width = FALSE, position = "center") %>%
  footnote(general = "Note: First 6 rows of the mtcars dataset.")

adding_footnotes

Best Practices for Creating Publication-Ready Tables

When designing tables for reports, articles, or presentations, keep these best practices in mind:

  • Format numbers consistently: Apply consistent decimal places and units so values are easy to compare
  • Highlight strategically: Use conditional formatting or bold text sparingly to draw attention to key results without overwhelming the reader
  • Ensure readability: Test how your table looks in HTML, PDF, or Word depending on your target output
  • Document sources: Always include captions or footnotes so readers know where the data came from

Conclusion

In short, both gt and kableExtra are powerful tools in R for making high-quality tables. The gt package is straightforward and produces clean, polished HTML tables. kableExtra is more flexible and especially useful when you need advanced formatting for reports or papers. By learning both tools, you’ll be able to create publication-ready tables across output formats.

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *