Search code examples
rgt

Reorder footnotes in gt


I have the following footnotes for my gt table:

  tab_footnote(
    footnote = "In cell comment",
    locations = cells_body(columns = 1, rows = 2)
  ) |>
  opt_footnote_marks(marks = "letters") |>
  tab_footnote(
    footnote = md("*Notes:* Robust standard errors are in parentheses.")
  ) |>
  tab_footnote(
    footnote = "+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001."
  )

The default display order for gt seems to be footnotes without stated marks 1st followed by footnotes with marks:

Notes: Robust standard errors are in parentheses.
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001.
^a in cell comment.

Can i reorder them such that I have this arrangement?

Notes: Robust standard errors are in parentheses.
^a in cell comment.
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001.

Solution

  • yes yes!

    library(gt)
    library(tidyverse)
    
    # Create sample data
    data <- tibble(
      Variable = c("Age", "Income", "Education"),
      Estimate = c(0.045, 0.234, 0.567),
      SE = c(0.023, 0.089, 0.156)
    )
    
    # Create gt table
    data |>
      gt() |>
      fmt_number(columns = c(Estimate, SE), decimals = 3) |>
      tab_footnote(
        footnote = md("*Notes:* Robust standard errors are in parentheses.")
      ) |>
      tab_footnote(
        footnote = "In cell comment",
        locations = cells_body(columns = 1, rows = 2)
      ) |>
      opt_footnote_marks(marks = "letters") |>
      tab_source_note(
        source_note = "+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001."
      )
    

    out