Skip to contents

ivo_table_gt() lets you easily create a GT table using pretty fonts and colors.

Usage

ivo_table_gt(
  df,
  color = "darkgreen",
  font_name = "Arial",
  caption = NULL,
  subtitle = NULL,
  extra_header = TRUE,
  source_note = NULL,
  mask = NULL,
  missing_string = "(Missing)",
  sums = NULL
)

Arguments

df

A data frame with 1-3 columns

color

A named color or a color HEX code, used for the lines in the table. Defaults to "darkgreen".

font_name

The name of the font to be used in the table. Defaults to "Arial".

caption

An optional string containing a table title.

subtitle

An optional string containing a table subtitle. Only usable together with title.

extra_header

Should the variable name be displayed? Defaults to TRUE.

source_note

An optional string for a table source note.

mask

An optional integer to mask counts below given value.

missing_string

A string used to indicate missing values. Defaults to "(Missing)".

sums

An optional vector to add sums to "rows" and "cols".

Value

A stylized GT table.

Details

The functions ivo_table_gt() takes a data.frame with 1-3 columns. The order of the columns in the data.frame will determine where they will be displayed in the table. The first column will always be displayed at the top of the table. If there are more than one column the following 2-3 columns will be displayed to the left in order. To change how the columns are displayed in the table; change the place of the columns in the data.frame using dplyr::select().

See also

ivo_gt_theme

Author

Stefan Furne

Examples

# Generate example data
example_data <- data.frame(
    Year = sample(2020:2023, 50, replace = TRUE),
    A = sample(c("Type 1", "Type 2"), 50, replace = TRUE),
    B = sample(c("Apples", "Oranges", "Bananas"), 50, replace = TRUE),
    C = sample(c("Swedish", "Norwegian", "Chilean"), 50, replace = TRUE)
)

### 1 way tables ###
data1 <- example_data |> dplyr::select(Year)

ivo_table_gt(data1)
Year Count
2020 15
2021 12
2022 14
2023 9
ivo_table_gt(data1, extra_header = FALSE) # Remove the header
Year Count
2020 15
2021 12
2022 14
2023 9
ivo_table_gt(data1, color = "orange") # Change color on table lines
Year Count
2020 15
2021 12
2022 14
2023 9
ivo_table_gt(data1, mask = 15) # Counts below <=15 are masked
Year Count
2020 1 - 15
2021 1 - 15
2022 1 - 15
2023 1 - 15
# With pipes example_data |> dplyr::select(Year) |> ivo_table_gt()
Year Count
2020 15
2021 12
2022 14
2023 9
### 2-way tables ### data2 <- example_data |> dplyr::select(A, B) data2_swap <- example_data |> dplyr::select(B, A) # Basic tables: ivo_table_gt(data2)
B
A
Type 1 Type 2
Apples 7 7
Bananas 9 7
Oranges 11 9
ivo_table_gt(data2_swap) # Swap order of the columns
A
B
Apples Bananas Oranges
Type 1 7 9 11
Type 2 7 7 9
ivo_table_gt(data2, sums = "cols") # Add the sum of each column
B
A
Type 1 Type 2
Apples 7 7
Bananas 9 7
Oranges 11 9
Total 27 23
ivo_table_gt(data2, sums = "rows") # Add the sum of each row
B
A
Total
Type 1 Type 2
Apples 7 7 14
Bananas 9 7 16
Oranges 11 9 20
ivo_table_gt(data2, sums = c("cols", "rows")) # Add the sum of each row and column
B
A
Total
Type 1 Type 2
Apples 7 7 14
Bananas 9 7 16
Oranges 11 9 20
Total 27 23 50
ivo_table_gt(data2, caption = "Awesome table") # Add a caption
Awesome table
B
A
Type 1 Type 2
Apples 7 7
Bananas 9 7
Oranges 11 9
ivo_table_gt(data2, caption = "Awesome table", subtitle = "It's really awesome" ) # Add a subtitle for the title
Awesome table
It's really awesome
B
A
Type 1 Type 2
Apples 7 7
Bananas 9 7
Oranges 11 9
# Masked tables: ivo_table_gt(data2, mask = 7) # Counts <= 7 are masked
B
A
Type 1 Type 2
Apples 1 - 7 1 - 7
Bananas 9 1 - 7
Oranges 11 9
# Row and column sums are also masked: ivo_table_gt( data2, mask = 3, sums = c("cols", "rows"), )
B
A
Total
Type 1 Type 2
Apples 7 7 14
Bananas 9 7 16
Oranges 11 9 20
Total 27 23 50
# Add a note at the end of the table: # (colwidths must be set to the number of columns in the table) ivo_table_gt(data2, source_note = "This is a footnote.")
B
A
Type 1 Type 2
Apples 7 7
Bananas 9 7
Oranges 11 9
This is a footnote.
### 3-way tables ### data3 <- example_data |> dplyr::select(C, B, Year) ivo_table_gt(data3)
C
Chilean Norwegian Swedish
Apples
2020 1 0 4
2021 1 2 1
2023 2 1 0
2022 0 1 1
Bananas
2020 1 4 2
2021 1 0 1
2023 2 0 0
2022 0 4 1
Oranges
2020 0 1 2
2021 4 1 1
2023 2 1 1
2022 3 3 1
ivo_table_gt(data3, sums = c("cols", "rows")) # Add the sum of each column and each row
C
Total
Chilean Norwegian Swedish
Apples
2020 1 0 4 5
2021 1 2 1 4
2023 2 1 0 3
2022 0 1 1 2
Total 4 4 6 14
Bananas
2020 1 4 2 7
2021 1 0 1 2
2023 2 0 0 2
2022 0 4 1 5
Total 4 8 4 16
Oranges
2020 0 1 2 3
2021 4 1 1 6
2023 2 1 1 4
2022 3 3 1 7
Total 9 6 5 20
ivo_table_gt( data3, mask = 3, caption = "Values between 1 and 3 are masked." )
Values between 1 and 3 are masked.
C
Chilean Norwegian Swedish
Apples
2020 1 - 3 0 4
2021 1 - 3 1 - 3 1 - 3
2023 1 - 3 1 - 3 0
2022 0 1 - 3 1 - 3
Bananas
2020 1 - 3 4 1 - 3
2021 1 - 3 0 1 - 3
2023 1 - 3 0 0
2022 0 4 1 - 3
Oranges
2020 0 1 - 3 1 - 3
2021 4 1 - 3 1 - 3
2023 1 - 3 1 - 3 1 - 3
2022 1 - 3 1 - 3 1 - 3