The individual alignments of columns (which includes the column labels and
all of their data cells) can be modified. We have the option to align text to
the left, the center, and the right. In a less explicit manner, we can
allow gt to automatically choose the alignment of each column based on
the data type (with the auto option).
Usage
cols_align(
data,
align = c("auto", "left", "center", "right"),
columns = everything()
)Arguments
- data
The gt table or gt group data object
obj:<gt_tbl>// requiredThis is the gt table object that is commonly created through use of the
gt()function.OR
obj:<gt_group>// requiredThis is the gt group object that is commonly created through use of the
gt_group()function.- align
Alignment type
singl-kw:[auto|left|center|right]// default:"auto"This can be any of
"center","left", or"right"for center-, left-, or right-alignment. Alternatively, the"auto"option (the default), will automatically align values in columns according to the data type (see the Details section for specifics on which alignments are applied).- columns
Columns to target
<column-targeting expression>// default:everything()The columns for which the alignment should be applied. Can either be a series of column names provided in
c(), a vector of column indices, or a select helper function (e.g.starts_with(),ends_with(),contains(),matches(),num_range(), andeverything()). By default this is set toeverything()which means that the chosen alignment affects all columns.
Details
When you create a gt table object using gt(), automatic alignment of
column labels and their data cells is performed. By default, left-alignment
is applied to columns of class character, Date, or POSIXct;
center-alignment is for columns of class logical, factor, or list; and
right-alignment is used for the numeric and integer columns.
Examples
Let's use countrypops to create a small gt table. We can change the
alignment of the population column with cols_align(). In this example,
the label and body cells of population will be aligned to the left.
countrypops |>
dplyr::select(-contains("code")) |>
dplyr::filter(
country_name == "San Marino",
year %in% 2017:2021
) |>
gt(
rowname_col = "year",
groupname_col = "country_name"
) |>
cols_align(
align = "left",
columns = population
)
See also
Other column modification functions:
cols_add(),
cols_align_decimal(),
cols_hide(),
cols_label(),
cols_label_with(),
cols_merge(),
cols_merge_n_pct(),
cols_merge_range(),
cols_merge_uncert(),
cols_move(),
cols_move_to_end(),
cols_move_to_start(),
cols_nanoplot(),
cols_unhide(),
cols_units(),
cols_width()