Introduction to modelsummary: Customizing Table Appearance
As a data analyst or researcher, creating and presenting statistical models is an essential part of our job. One of the most critical aspects of model presentation is the table that summarizes the results. The modelsummary package in R provides a convenient way to create tables that summarize model estimates. However, by default, the appearance of these tables may not be exactly what we want.
In this article, we’ll explore how to customize the appearance of tables generated by the modelsummary package using Bootstrap 5 classes and custom themes.
Background
The modelsummary package uses the tinytable package to draw tables. The tinytable package is designed to create beautiful tables with minimal code, but one of its features requires some fine-tuning: applying custom styles.
One way to apply custom styles to tables in R is by using Bootstrap 5 classes. However, the default placement of table captions at the bottom can be inconvenient for certain types of presentations.
Using style_tt() to Customize Table Caption Placement
To change the placement of the caption, we need to use the style_tt() function from the tinytable package and apply the Bootstrap 5 class that controls the caption position. In this case, we’ll use the bootstrap_class argument with the value "table caption-top".
Here’s an example code snippet that demonstrates how to customize the table caption placement:
library(modelsummary)
library(tinytable)
# Create a model summary object
mod <- fixest::fepois(count ~ height | id, data.frame(id = rep(1:10, each = 5), year = rep(seq(2021, 2025), times = 10)))
# Create the model summary table with custom caption placement
modelsummary(mod, title = "This is my title") |
style_tt(bootstrap_class = "table caption-top")
By applying the bootstrap_class argument to the style_tt() function, we can change the position of the caption to "table caption-top".
Defining Custom Themes
For convenience and consistency across all tables, it’s a good idea to define custom themes that apply this change automatically. The tinytable package provides an example theme called " Bootstrap" which includes all the necessary styles for our purpose.
To create a custom theme, we can follow these steps:
- Load the required packages: We need both the
modelsummaryandtinytablepackages. - Define a new theme object that inherits from the existing “Bootstrap” theme. This is done by passing
inherit = TRUEto thetheme()function and then defining our own styles using functions likestyle_tt(). - Apply the custom theme to all tables: We can use the
theme()function with our custom theme name.
Here’s an example code snippet that demonstrates how to define a custom theme:
# Load required packages
library(modelsummary)
library(tinytable)
# Define a new theme object
custom_theme <- tinytable::theme(
style_tt = "table caption-top"
)
# Create model summaries with the custom theme
modelsummary(mod, title = "This is my title") |
theme(custom_theme)
By defining a custom theme and applying it to our tables, we can ensure that all models are presented with consistent table styles.
Best Practices for Customizing Model Summary Tables
When working with modelsummary and tinytable, here are some best practices to keep in mind when customizing your tables:
- Use the
style_tt()function to apply Bootstrap 5 classes directly. - Define a custom theme that applies this change automatically, if needed.
- Follow the official documentation for both
modelsummaryandtinytablefor more information on their features and usage.
By following these best practices and using the techniques described in this article, you can take your table presentation to the next level and create visually appealing models summaries with ease.
Last modified on 2025-02-18