The {gt} package – yes, you guessed it, gt stands for The Grammar of Tables – has been around for a while but I have never used it thus far. I have started seeing it used more often in the #rstats world and decided to give it a try.
There are several excellent walk-throughs out there so make sure you also look at them since they have nuggets not contained here. In particular, look at Manasi’s work here, Thomas’ post here, and Malcolm’s work here. The {gt}
package’s vignettes and tutorials can be found here. The core building blocks of {gt}
are shown below:
The usual piped workflow can be used where a table object is created and then passed on to {gt}
. You could save the table object before running gt(my_tbl)
, as is shown in the second code chunk.
library(tidyverse)
library(gt)
tibble(
name = names(islands),
size = islands
%>%
) arrange(desc(size)) %>%
slice(1:10) %>%
mutate(share = (size / sum(size))) %>%
gt()
name | size | share |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
tibble(
name = names(islands),
size = islands
%>%
) arrange(desc(size)) %>%
slice(1:10) %>%
mutate(share = (size / sum(size))) -> my_tbl
gt(my_tbl)
name | size | share |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
The fun begins with adding a title and subtitle, assembling the table header, and then adding notes to the foot of the table. I am also shrinkng the table’s size to avoid overwhelming the page with a series of large tables.
gt(my_tbl) %>%
tab_header(
title = "Ten Largest Landmasses in the World",
subtitle = "(Arranged in descending order of size)"
%>%
) tab_source_note(
source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
%>%
) tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("name", "size", "share"))
)
Ten Largest Landmasses in the World | ||
(Arranged in descending order of size) | ||
name | size | share |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
Source: The World Almanac and Book of Facts, 1975, page 406. | ||
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. |
Footnotes can be easily embedded as well:
gt(my_tbl) %>%
tab_header(
title = "Ten Largest Landmasses in the World",
subtitle = "(Arranged in descending order of size)"
%>%
) tab_source_note(
source_note = md("**Source:** The World Almanac and Book of Facts, 1975, page 406.")
%>%
) tab_source_note(
source_note = md("**Reference:** McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
%>%
) tab_footnote(
footnote = md("These are also referred to as *The Americas*."),
cells_body(columns = vars(name),
rows = 3:4
)%>%
) tab_footnote(
footnote = md("This is the smallest."),
cells_body(columns = vars(name),
rows = 10
)%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("name", "size", "share"))
)
Ten Largest Landmasses in the World | ||
(Arranged in descending order of size) | ||
name | size | share |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America1 | 9390 | 0.161013752 |
South America1 | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo2 | 280 | 0.004801262 |
Source: The World Almanac and Book of Facts, 1975, page 406. | ||
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. | ||
1 These are also referred to as The Americas. | ||
2 This is the smallest. |
The stub is the area to the left of the table that we normally associate with rownames, and where we might even want to have labels for groups of rows. The rowname_col
command is forcing the name
column to be the stub.
%>%
my_tbl gt(rowname_col = "name") %>%
tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("name", "size", "share"))
)
size | share | |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
The same result would have occurred if we had called the name
column rowname
instead; this is the default stub {gt}
looks for; see below for an example.
%>%
my_tbl rename(rowname = name) %>%
gt() %>%
tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("size", "share"))
)
size | share | |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
We can add a stub-heading now.
%>%
my_tbl rename(rowname = name) %>%
gt() %>%
tab_stubhead(label = "Landmass") %>%
tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("size", "share"))
)
Landmass | size | share |
---|---|---|
Asia | 16988 | 0.291299427 |
Africa | 11506 | 0.197297575 |
North America | 9390 | 0.161013752 |
South America | 6795 | 0.116516341 |
Antarctica | 5500 | 0.094310504 |
Europe | 3745 | 0.064216880 |
Australia | 2968 | 0.050893378 |
Greenland | 840 | 0.014403786 |
New Guinea | 306 | 0.005247094 |
Borneo | 280 | 0.004801262 |
Note the absence of any separator for size
values, and of course share
appearing as a proportion with nine decimals rather than as a percentage.
%>%
my_tbl rename(rowname = name) %>%
gt() %>%
tab_stubhead(label = "Landmass") %>%
fmt_number(
columns = vars(size),
decimals = 0
%>%
) fmt_percent(
columns = vars(share),
decimals = 1
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars("size", "share"))
)
Landmass | size | share |
---|---|---|
Asia | 16,988 | 29.1% |
Africa | 11,506 | 19.7% |
North America | 9,390 | 16.1% |
South America | 6,795 | 11.7% |
Antarctica | 5,500 | 9.4% |
Europe | 3,745 | 6.4% |
Australia | 2,968 | 5.1% |
Greenland | 840 | 1.4% |
New Guinea | 306 | 0.5% |
Borneo | 280 | 0.5% |
Other formatting options are available and could be used as shown below, with data on the highest paid cricketers in 2020 available here.
read_csv(
::here("data", "cricket.csv")
here-> cricket
)
gt(cricket) %>%
tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsement Earnings",
"Overall Earnings", "Sponsorship Deals")
) )
Country | Cricketers | Base Salary | Endorsement Earnings | Overall Earnings | Sponsorship Deals |
---|---|---|---|---|---|
India | Virat Kohli | 4.00 | 24.000 | 28.000 | Audi, Puma, Flipkart, Google |
India | MS Dhoni | 3.50 | 10.500 | 14.000 | PepsiCo, RedBus, Colgate |
India | Rohit Sharma | 3.00 | 7.000 | 10.000 | Adidas, Cadbury, La Liga |
England | Ben Stokes | 3.00 | 5.000 | 8.000 | New Balance, Red Bull, Rajasthan Royals |
India | Hardik Pandya | 3.00 | 5.000 | 8.000 | Star Sports, Gulf Oil, Gillete |
Australia | Steve Smith | 2.50 | 5.000 | 7.500 | Koala |
India | Jasprit Bumrah | 1.75 | 2.500 | 4.250 | Cultsport, Dream11, Estrolo |
South Africa | AB de Villiers | 2.50 | 0.500 | 3.000 | Montblanc, Puma, Audi, MRF |
Australia | Pat Cummins | 2.80 | 0.200 | 3.000 | Off field investements |
India | Suresh Raina | 2.50 | 0.500 | 3.000 | Intex, Asics, Pure Play |
England | Jos Buttler | 2.50 | 0.350 | 2.850 | Kookabura, Adidas |
West Indies | Kieron Pollard | 2.20 | 0.400 | 2.600 | Mumbai Indians |
India | Bhuvneshwar Kumar | 1.97 | 0.200 | 2.170 | Asics, Nutramantra |
Australia | Shane Watson | 1.80 | 0.350 | 2.150 | Set Wet, Himalya |
England | Jofra Archer | 1.75 | 0.300 | 2.050 | Adidas |
Australia | Aaron Finch | 1.35 | 0.350 | 1.700 | Gray Nicolls |
West Indies | Andre Russell | 1.65 | 0.050 | 1.700 | Foska Oats |
Pakistan | Babar Azam | 0.95 | 0.350 | 1.300 | Karachi Kings, Gatorade |
South Africa | Faf du Plessis | 1.45 | 0.200 | 1.650 | Chennai Super Kings |
South Africa | Quinton de Kock | 1.00 | 0.085 | 1.085 | GM |
Pakistan | Mohammad Amir | 0.75 | 0.300 | 1.050 | Fair Menz |
For example, say we want to format the salaries and earnings as the currency units, they are.
%>%
cricket gt() %>%
fmt_currency(
columns = vars(`Base Salary`, `Endorsement Earnings`, `Overall Earnings`),
decimals = 2,
pattern = "{x} M"
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsement Earnings",
"Overall Earnings", "Sponsorship Deals")
) )
Country | Cricketers | Base Salary | Endorsement Earnings | Overall Earnings | Sponsorship Deals |
---|---|---|---|---|---|
India | Virat Kohli | $4.00 M | $24.00 M | $28.00 M | Audi, Puma, Flipkart, Google |
India | MS Dhoni | $3.50 M | $10.50 M | $14.00 M | PepsiCo, RedBus, Colgate |
India | Rohit Sharma | $3.00 M | $7.00 M | $10.00 M | Adidas, Cadbury, La Liga |
England | Ben Stokes | $3.00 M | $5.00 M | $8.00 M | New Balance, Red Bull, Rajasthan Royals |
India | Hardik Pandya | $3.00 M | $5.00 M | $8.00 M | Star Sports, Gulf Oil, Gillete |
Australia | Steve Smith | $2.50 M | $5.00 M | $7.50 M | Koala |
India | Jasprit Bumrah | $1.75 M | $2.50 M | $4.25 M | Cultsport, Dream11, Estrolo |
South Africa | AB de Villiers | $2.50 M | $0.50 M | $3.00 M | Montblanc, Puma, Audi, MRF |
Australia | Pat Cummins | $2.80 M | $0.20 M | $3.00 M | Off field investements |
India | Suresh Raina | $2.50 M | $0.50 M | $3.00 M | Intex, Asics, Pure Play |
England | Jos Buttler | $2.50 M | $0.35 M | $2.85 M | Kookabura, Adidas |
West Indies | Kieron Pollard | $2.20 M | $0.40 M | $2.60 M | Mumbai Indians |
India | Bhuvneshwar Kumar | $1.97 M | $0.20 M | $2.17 M | Asics, Nutramantra |
Australia | Shane Watson | $1.80 M | $0.35 M | $2.15 M | Set Wet, Himalya |
England | Jofra Archer | $1.75 M | $0.30 M | $2.05 M | Adidas |
Australia | Aaron Finch | $1.35 M | $0.35 M | $1.70 M | Gray Nicolls |
West Indies | Andre Russell | $1.65 M | $0.05 M | $1.70 M | Foska Oats |
Pakistan | Babar Azam | $0.95 M | $0.35 M | $1.30 M | Karachi Kings, Gatorade |
South Africa | Faf du Plessis | $1.45 M | $0.20 M | $1.65 M | Chennai Super Kings |
South Africa | Quinton de Kock | $1.00 M | $0.09 M | $1.08 M | GM |
Pakistan | Mohammad Amir | $0.75 M | $0.30 M | $1.05 M | Fair Menz |
Assume we would like to add a group label for the rows. The tab_row_group()
command will allow you to bundle specific rows into a commonly-named group.
%>%
cricket gt() %>%
fmt_currency(
columns = vars(`Base Salary`, `Endorsement Earnings`, `Overall Earnings`),
decimals = 2,
pattern = "{x} M"
%>%
) tab_row_group(
group = "Pakistan",
rows = c(18, 21)
%>%
) tab_row_group(
group = "West Indies",
rows = c(12, 17)
%>%
) tab_row_group(
group = "South Africa",
rows = c(8, 19, 20)
%>%
) tab_row_group(
group = "England",
rows = c(4, 11, 15)
%>%
) tab_row_group(
group = "Australia",
rows = c(6, 9, 14, 16)
%>%
) tab_row_group(
group = "India",
rows = c(1:3, 5, 7, 10, 13)
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsement Earnings",
"Overall Earnings", "Sponsorship Deals")
) )
Country | Cricketers | Base Salary | Endorsement Earnings | Overall Earnings | Sponsorship Deals |
---|---|---|---|---|---|
India | |||||
India | Virat Kohli | $4.00 M | $24.00 M | $28.00 M | Audi, Puma, Flipkart, Google |
India | MS Dhoni | $3.50 M | $10.50 M | $14.00 M | PepsiCo, RedBus, Colgate |
India | Rohit Sharma | $3.00 M | $7.00 M | $10.00 M | Adidas, Cadbury, La Liga |
India | Hardik Pandya | $3.00 M | $5.00 M | $8.00 M | Star Sports, Gulf Oil, Gillete |
India | Jasprit Bumrah | $1.75 M | $2.50 M | $4.25 M | Cultsport, Dream11, Estrolo |
India | Suresh Raina | $2.50 M | $0.50 M | $3.00 M | Intex, Asics, Pure Play |
India | Bhuvneshwar Kumar | $1.97 M | $0.20 M | $2.17 M | Asics, Nutramantra |
Australia | |||||
Australia | Steve Smith | $2.50 M | $5.00 M | $7.50 M | Koala |
Australia | Pat Cummins | $2.80 M | $0.20 M | $3.00 M | Off field investements |
Australia | Shane Watson | $1.80 M | $0.35 M | $2.15 M | Set Wet, Himalya |
Australia | Aaron Finch | $1.35 M | $0.35 M | $1.70 M | Gray Nicolls |
England | |||||
England | Ben Stokes | $3.00 M | $5.00 M | $8.00 M | New Balance, Red Bull, Rajasthan Royals |
England | Jos Buttler | $2.50 M | $0.35 M | $2.85 M | Kookabura, Adidas |
England | Jofra Archer | $1.75 M | $0.30 M | $2.05 M | Adidas |
South Africa | |||||
South Africa | AB de Villiers | $2.50 M | $0.50 M | $3.00 M | Montblanc, Puma, Audi, MRF |
South Africa | Faf du Plessis | $1.45 M | $0.20 M | $1.65 M | Chennai Super Kings |
South Africa | Quinton de Kock | $1.00 M | $0.09 M | $1.08 M | GM |
West Indies | |||||
West Indies | Kieron Pollard | $2.20 M | $0.40 M | $2.60 M | Mumbai Indians |
West Indies | Andre Russell | $1.65 M | $0.05 M | $1.70 M | Foska Oats |
Pakistan | |||||
Pakistan | Babar Azam | $0.95 M | $0.35 M | $1.30 M | Karachi Kings, Gatorade |
Pakistan | Mohammad Amir | $0.75 M | $0.30 M | $1.05 M | Fair Menz |
Note that by default the last tab_row_group()
will be rendered first, which is not what I expected to see. I am sure there is good reason for this and likely need to dig into the {gt}
manual. The bigger picture here, though, at least for me, is the realization that I might as well rely on group_by()
operations to automatically generate tab_row_group
settings. How? See below; seems far more efficient.
%>%
cricket group_by(Country) %>%
arrange(desc(`Overall Earnings`)) %>%
gt() %>%
fmt_currency(
columns = vars(`Base Salary`, `Endorsement Earnings`, `Overall Earnings`),
decimals = 2,
pattern = "{x} M"
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsement Earnings",
"Overall Earnings", "Sponsorship Deals")
) )
Cricketers | Base Salary | Endorsement Earnings | Overall Earnings | Sponsorship Deals |
---|---|---|---|---|
India | ||||
Virat Kohli | $4.00 M | $24.00 M | $28.00 M | Audi, Puma, Flipkart, Google |
MS Dhoni | $3.50 M | $10.50 M | $14.00 M | PepsiCo, RedBus, Colgate |
Rohit Sharma | $3.00 M | $7.00 M | $10.00 M | Adidas, Cadbury, La Liga |
Hardik Pandya | $3.00 M | $5.00 M | $8.00 M | Star Sports, Gulf Oil, Gillete |
Jasprit Bumrah | $1.75 M | $2.50 M | $4.25 M | Cultsport, Dream11, Estrolo |
Suresh Raina | $2.50 M | $0.50 M | $3.00 M | Intex, Asics, Pure Play |
Bhuvneshwar Kumar | $1.97 M | $0.20 M | $2.17 M | Asics, Nutramantra |
England | ||||
Ben Stokes | $3.00 M | $5.00 M | $8.00 M | New Balance, Red Bull, Rajasthan Royals |
Jos Buttler | $2.50 M | $0.35 M | $2.85 M | Kookabura, Adidas |
Jofra Archer | $1.75 M | $0.30 M | $2.05 M | Adidas |
Australia | ||||
Steve Smith | $2.50 M | $5.00 M | $7.50 M | Koala |
Pat Cummins | $2.80 M | $0.20 M | $3.00 M | Off field investements |
Shane Watson | $1.80 M | $0.35 M | $2.15 M | Set Wet, Himalya |
Aaron Finch | $1.35 M | $0.35 M | $1.70 M | Gray Nicolls |
South Africa | ||||
AB de Villiers | $2.50 M | $0.50 M | $3.00 M | Montblanc, Puma, Audi, MRF |
Faf du Plessis | $1.45 M | $0.20 M | $1.65 M | Chennai Super Kings |
Quinton de Kock | $1.00 M | $0.09 M | $1.08 M | GM |
West Indies | ||||
Kieron Pollard | $2.20 M | $0.40 M | $2.60 M | Mumbai Indians |
Andre Russell | $1.65 M | $0.05 M | $1.70 M | Foska Oats |
Pakistan | ||||
Babar Azam | $0.95 M | $0.35 M | $1.30 M | Karachi Kings, Gatorade |
Mohammad Amir | $0.75 M | $0.30 M | $1.05 M | Fair Menz |
Notice we have two columns that both refer to earnings. Usually, we would have user a super-column with an Earnings
label and then had the subordinate columns only show column names of Base
and Overall
, respectively. How do we do that here? With the tab_spanner()
command.
%>%
cricket group_by(Country) %>%
rename(
Endorsements = `Endorsement Earnings`,
Overall = `Overall Earnings`
%>%
) arrange(desc(Overall)) %>%
gt() %>%
fmt_currency(
columns = vars(`Base Salary`, Endorsements, Overall),
decimals = 2,
pattern = "{x} M"
%>%
) tab_spanner(
label = "Earnings",
columns = vars(Endorsements, Overall)
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(
columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsements",
"Overall", "Sponsorship Deals"
)
) )
Cricketers | Base Salary | Earnings | Sponsorship Deals | |
---|---|---|---|---|
Endorsements | Overall | |||
India | ||||
Virat Kohli | $4.00 M | $24.00 M | $28.00 M | Audi, Puma, Flipkart, Google |
MS Dhoni | $3.50 M | $10.50 M | $14.00 M | PepsiCo, RedBus, Colgate |
Rohit Sharma | $3.00 M | $7.00 M | $10.00 M | Adidas, Cadbury, La Liga |
Hardik Pandya | $3.00 M | $5.00 M | $8.00 M | Star Sports, Gulf Oil, Gillete |
Jasprit Bumrah | $1.75 M | $2.50 M | $4.25 M | Cultsport, Dream11, Estrolo |
Suresh Raina | $2.50 M | $0.50 M | $3.00 M | Intex, Asics, Pure Play |
Bhuvneshwar Kumar | $1.97 M | $0.20 M | $2.17 M | Asics, Nutramantra |
England | ||||
Ben Stokes | $3.00 M | $5.00 M | $8.00 M | New Balance, Red Bull, Rajasthan Royals |
Jos Buttler | $2.50 M | $0.35 M | $2.85 M | Kookabura, Adidas |
Jofra Archer | $1.75 M | $0.30 M | $2.05 M | Adidas |
Australia | ||||
Steve Smith | $2.50 M | $5.00 M | $7.50 M | Koala |
Pat Cummins | $2.80 M | $0.20 M | $3.00 M | Off field investements |
Shane Watson | $1.80 M | $0.35 M | $2.15 M | Set Wet, Himalya |
Aaron Finch | $1.35 M | $0.35 M | $1.70 M | Gray Nicolls |
South Africa | ||||
AB de Villiers | $2.50 M | $0.50 M | $3.00 M | Montblanc, Puma, Audi, MRF |
Faf du Plessis | $1.45 M | $0.20 M | $1.65 M | Chennai Super Kings |
Quinton de Kock | $1.00 M | $0.09 M | $1.08 M | GM |
West Indies | ||||
Kieron Pollard | $2.20 M | $0.40 M | $2.60 M | Mumbai Indians |
Andre Russell | $1.65 M | $0.05 M | $1.70 M | Foska Oats |
Pakistan | ||||
Babar Azam | $0.95 M | $0.35 M | $1.30 M | Karachi Kings, Gatorade |
Mohammad Amir | $0.75 M | $0.30 M | $1.05 M | Fair Menz |
I would like to modify some of the column names; is that possible? Absolutely, with cols_label()
%>%
cricket group_by(Country) %>%
rename(
Endorsements = `Endorsement Earnings`,
Overall = `Overall Earnings`
%>%
) arrange(desc(Overall)) %>%
gt() %>%
fmt_currency(
columns = vars(`Base Salary`, Endorsements, Overall),
decimals = 2,
pattern = "{x} M"
%>%
) tab_spanner(
label = "Earnings",
columns = vars(Endorsements, Overall)
%>%
) cols_label(
`Base Salary` = "Salary",
`Sponsorship Deals` = "Sponsors",
Cricketers = "Criketer"
%>%
) tab_options(
column_labels.font.size = "small",
table.font.size = "small",
data_row.padding = px(3)
%>%
) tab_style(
style = cell_text(size = "small"),
locations = cells_body(
columns = vars(
"Country", "Cricketers", "Base Salary", "Endorsements",
"Overall", "Sponsorship Deals"
)
) )
Criketer | Salary | Earnings | Sponsors | |
---|---|---|---|---|
Endorsements | Overall | |||
India | ||||
Virat Kohli | $4.00 M | $24.00 M | $28.00 M | Audi, Puma, Flipkart, Google |
MS Dhoni | $3.50 M | $10.50 M | $14.00 M | PepsiCo, RedBus, Colgate |
Rohit Sharma | $3.00 M | $7.00 M | $10.00 M | Adidas, Cadbury, La Liga |
Hardik Pandya | $3.00 M | $5.00 M | $8.00 M | Star Sports, Gulf Oil, Gillete |
Jasprit Bumrah | $1.75 M | $2.50 M | $4.25 M | Cultsport, Dream11, Estrolo |
Suresh Raina | $2.50 M | $0.50 M | $3.00 M | Intex, Asics, Pure Play |
Bhuvneshwar Kumar | $1.97 M | $0.20 M | $2.17 M | Asics, Nutramantra |
England | ||||
Ben Stokes | $3.00 M | $5.00 M | $8.00 M | New Balance, Red Bull, Rajasthan Royals |
Jos Buttler | $2.50 M | $0.35 M | $2.85 M | Kookabura, Adidas |
Jofra Archer | $1.75 M | $0.30 M | $2.05 M | Adidas |
Australia | ||||
Steve Smith | $2.50 M | $5.00 M | $7.50 M | Koala |
Pat Cummins | $2.80 M | $0.20 M | $3.00 M | Off field investements |
Shane Watson | $1.80 M | $0.35 M | $2.15 M | Set Wet, Himalya |
Aaron Finch | $1.35 M | $0.35 M | $1.70 M | Gray Nicolls |
South Africa | ||||
AB de Villiers | $2.50 M | $0.50 M | $3.00 M | Montblanc, Puma, Audi, MRF |
Faf du Plessis | $1.45 M | $0.20 M | $1.65 M | Chennai Super Kings |
Quinton de Kock | $1.00 M | $0.09 M | $1.08 M | GM |
West Indies | ||||
Kieron Pollard | $2.20 M | $0.40 M | $2.60 M | Mumbai Indians |
Andre Russell | $1.65 M | $0.05 M | $1.70 M | Foska Oats |
Pakistan | ||||
Babar Azam | $0.95 M | $0.35 M | $1.30 M | Karachi Kings, Gatorade |
Mohammad Amir | $0.75 M | $0.30 M | $1.05 M | Fair Menz |
We will see more formatting options in the follow-up post but if want to keep moving, it would be good to see some of the cell-coloring features Thomas highlights.
Text and figures are licensed under Creative Commons Attribution CC BY-SA 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Ruhil (2021, Feb. 7). From an Attican Hollow: {gt} Tables (1/2). Retrieved from https://aniruhil.org/posts/2021-02-07-gt-tables/
BibTeX citation
@misc{ruhil2021{gt}, author = {Ruhil, Ani}, title = {From an Attican Hollow: {gt} Tables (1/2)}, url = {https://aniruhil.org/posts/2021-02-07-gt-tables/}, year = {2021} }