Skip to contents

Adds a row of horizontal color bars below a gt table from a vector of hex colors, optionally carrying an image and a line of text. The bars are attached as a source note, so they sit below everything else. It is the bottom-edge counterpart to gt_border_bars_top().

Usage

gt_border_bars_bottom(
  gt_object,
  colors,
  bar_height = 10,
  bar_width = "100%",
  bar_align = "center",
  img = NULL,
  img_width = 30,
  img_height = 30,
  img_padding = 10,
  img_align = "right",
  text = NULL,
  text_weight = "bold",
  text_color = "#FFFFFF",
  text_size = 18,
  text_align = "left",
  text_padding = 10
)

Arguments

gt_object

A gt table object to modify.

colors

Character. Hex color codes, one per bar. When img or text is supplied only the first color is used.

bar_height

Numeric. The height of the bars in pixels. Defaults to 10.

bar_width

Character. The width of the bar block, as a CSS width. Defaults to "100%".

bar_align

Character. Alignment of the block when bar_width is under "100%". One of "left", "center", "right". Defaults to "center".

img

Optional. A URL for an image to render in the bar. Defaults to NULL.

img_width

Numeric. The image width in pixels. Defaults to 30.

img_height

Numeric. The image height in pixels. Defaults to 30.

img_padding

Numeric. Padding around the image in pixels, so it does not touch the edge. Defaults to 10.

img_align

Character. The side the image padding is applied to, "left" or "right"; any other value applies no padding. Defaults to "right".

text

Optional. Text to display in the bar. Defaults to NULL.

text_weight

Character. The font weight of the text. Defaults to "bold".

text_color

Character. The text color. Defaults to "#FFFFFF".

text_size

Numeric. The font size in pixels. Defaults to 18.

text_align

Character. The side the text padding is applied to, "left" or "right"; any other value applies no padding. Defaults to "left".

text_padding

Numeric. Padding around the text in pixels. Defaults to 10.

Value

Returns a modified gt table with a row of bars below it.

Details

The bars are added with gt::tab_source_note(), and a table id is resolved or generated so scoped CSS can zero the source-note padding. With neither img nor text, each entry in colors becomes its own full-width bar stacked in a block. When img or text is supplied, a single bar is drawn in the first color as a flex row with the text at one end and the image at the other. The text font is read from the table's source-note styling and imported as a Google Font, falling back to the inherited font when none is set, so it renders in an exported table.

Examples

library(gt)

gt(head(mtcars)) %>%
  gt_border_bars_bottom(c("#1B7837", "#FFFFFF", "#B2182B"))
mpg cyl disp hp drat wt qsec vs am gear carb
21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# a single bar carrying a credit line gt(head(iris)) %>% gt_border_bars_bottom("#22223B", text = "Source: iris", bar_height = 28)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
Source: iris