rmodelsummary

modelsummary: put title on top


Consider the following example:

id <- rep(seq(1,10), each=5)
year <- rep(seq(2021, 2025), times=10)
df <- data.frame(id=id, year=year)
df[["weight"]] <- rnorm(nrow(df), 100, 10)
df[["height"]] <- 2*df$weight+rnorm(nrow(df), 0, 5)
df[["count"]] <- rpois(nrow(df), df$height+rnorm(nrow(df), 0, 5))
mod <- fixest::fepois(count ~ height | id, df)
modelsummary::modelsummary(mod, title="This is my title")

I want the title of the table on top of it:

enter image description here


Solution

  • Some background:

    1. modelsummary uses the tinytable package to draw tables.
    2. tinytable uses Bootstrap 5 to style tables.
    3. Bootstrap 5 places the caption at the bottom, but you can apply a table class to change that.

    You can apply table classes using the style_tt() function from the tinytable package to change the position of the caption. This is document here: https://vincentarelbundock.github.io/tinytable/vignettes/custom.html#bootstrap-classes

    In practice, it will look like this:

    library(modelsummary)
    library(tinytable)
    modelsummary(models, title = "Hello world!") |>
      style_tt(bootstrap_class = "table caption-top")
    

    For convenience, you can define a custom theme to apply this change to all your tables automatically. See the documentation here:

    https://vincentarelbundock.github.io/tinytable/vignettes/theme.html#custom-themes