Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to change the font of the caption using gt in r?

Tags:

r

Is it possible to change the font of the caption in the gt package in R? I am working with a specific format, and I need to make the font of the caption Arial, while the rest of the table needs to be in Georgia. Here is a same of the code that I am working with:

library(gt)

DF <- tibble(character = c('a', 'b', 'c'),
             value = round(runif(3, 4, 7)))

DF %>%
gt() %>%
  tab_options(table.font.names = 'Georgia',
              column_labels.font.weight = 'bold',
              heading.title.font.size = 14,
              heading.subtitle.font.size = 14,
              table.font.color = 'steelblue',
              source_notes.font.size = 10,
              #source_notes.
              table.font.size = 14) %>%
  # Adjust title font
  tab_style(
    style = list(
      cell_text(
        align = "left",
        weight = 'bold',
      )
    ),
    locations = list(
      cells_title(groups = c("title"))
    )
  ) %>%
  # Adjust title font
  tab_style(
    style = list(
      cell_text(
        align = "left",
      )
    ),
    locations = list(
      cells_title(groups = c("subtitle"))
    )
  ) %>%
  tab_header(
    title = md("title"),
    subtitle = md("subtitle")
  ) %>%
  tab_source_note(
    source_note = md("caption")
  )```
like image 299
AENick Avatar asked Jan 18 '26 22:01

AENick


1 Answers

You can pass html in source_note in which you can specify font-family as css property.

library(gt)

DF <- tibble(character = c('a', 'b', 'c'),
             value = round(runif(3, 4, 7)))

DF %>%
  gt() %>%
  tab_options(table.font.names = 'Georgia',
              column_labels.font.weight = 'bold',
              heading.title.font.size = 14,
              heading.subtitle.font.size = 14,
              table.font.color = 'steelblue',
              source_notes.font.size = 10,
              #source_notes.
              table.font.size = 14) %>%
  # Adjust title font
  tab_style(
    style = list(
      cell_text(
        align = "left",
        weight = 'bold',
      )
    ),
    locations = list(
      cells_title(groups = c("title"))
    )
  ) %>%
  # Adjust title font
  tab_style(
    style = list(
      cell_text(
        align = "left",
      )
    ),
    locations = list(
      cells_title(groups = c("subtitle"))
    )
  ) %>%
  tab_header(
    title = md("title"),
    subtitle = md("subtitle")
  ) %>%
  tab_source_note(
    source_note = html('<p style="font-family:Arial">caption</p>')
  )

enter image description here

like image 105
Ronak Shah Avatar answered Jan 21 '26 11:01

Ronak Shah