The fmt_bins()
function
Use the countrypops
dataset to create a gt table. Before even getting to the gt()
call, we use the cut()
function in conjunction with the scales::breaks_log()
function to create some highly customized bins. Consequently each country’s population in the 2021 year is assigned to a bin. These bins have a characteristic type of formatting that can be used as input to fmt_bins()
, and using that formatting function allows us to customize the presentation of those ranges. For instance, here we are formatting the left and right values of the ranges with the fmt_integer()
function (using formula syntax).
countrypops |>
dplyr:: filter (year == 2021 ) |>
dplyr:: select (country_code_2, population) |>
dplyr:: mutate (population_class = cut (
population,
breaks = scales:: breaks_log (n = 20 )(population)
)
) |>
dplyr:: group_by (population_class) |>
dplyr:: summarize (
count = dplyr:: n (),
countries = paste0 (country_code_2, collapse = "," )
) |>
dplyr:: arrange (desc (population_class)) |>
gt () |>
fmt_flag (columns = countries) |>
fmt_bins (
columns = population_class,
fmt = ~ fmt_integer (., suffixing = TRUE )
) |>
cols_label (
population_class = "Population Range" ,
count = "" ,
countries = "Countries"
) |>
cols_width (
population_class ~ px (150 ),
count ~ px (50 )
) |>
tab_style (
style = cell_text (style = "italic" ),
locations = cells_body (columns = count)
)
Population Range
Countries
1B–2B
2
China India
300M–500M
1
United States
200M–300M
4
Brazil Indonesia Nigeria Pakistan
100M–200M
7
Bangladesh Egypt Ethiopia Japan Mexico Philippines Russian Federation
50M–100M
15
Congo (Democratic Republic) Colombia Germany France United Kingdom Iran (Islamic Republic) Italy Kenya Korea, Rep. Myanmar Thailand Turkiye Tanzania Vietnam South Africa
30M–50M
20
Afghanistan Angola Argentina Canada Algeria Spain Ghana Iraq Morocco Mozambique Malaysia Nepal Peru Poland Saudi Arabia Sudan Uganda Ukraine Uzbekistan Yemen
20M–30M
11
Australia Burkina Faso Cote d'Ivoire Cameroon Sri Lanka Madagascar Mali Niger Korea, Dem. People's Rep. Syrian Arab Republic Venezuela, RB
10M–20M
31
Azerbaijan Burundi Belgium Benin Bolivia Chile Cuba Czechia Dominican Republic Ecuador Guinea Greece Guatemala Honduras Haiti Jordan Kazakhstan Cambodia Malawi Netherlands Portugal Romania Rwanda Senegal Somalia South Sudan Sweden Chad Tunisia Zambia Zimbabwe
5M–10M
32
United Arab Emirates Austria Bulgaria Belarus Central African Republic Switzerland Congo Costa Rica Denmark Finland Hong Kong Hungary Ireland Israel Kyrgyz Republic Lao PDR Lebanon Liberia Libya Nicaragua Norway New Zealand Papua New Guinea Paraguay Singapore Sierra Leone El Salvador Serbia Slovak Republic Togo Tajikistan Turkmenistan
3M–5M
12
Bosnia and Herzegovina Eritrea Georgia Croatia Kuwait Mongolia Mauritania Oman Panama Puerto Rico West Bank and Gaza Uruguay
2M–3M
14
Albania Armenia Botswana Gabon Gambia, The Guinea-Bissau Jamaica Lesotho Lithuania Moldova North Macedonia Namibia Qatar Slovenia
1M–2M
10
Bahrain Cyprus Djibouti Estonia Equatorial Guinea Latvia Mauritius Eswatini Timor-Leste Trinidad and Tobago
500K–1M
12
Bhutan Comoros Cabo Verde Fiji Guyana Luxembourg Macao Maldives Malta Montenegro Solomon Islands Suriname
300K–500K
6
Bahamas Belize Brunei Darussalam Iceland French Polynesia Vanuatu
200K–300K
4
Barbados New Caledonia Sao Tome and Principe Samoa
100K–200K
10
Aruba Curacao Micronesia (Federated States) Grenada Guam Kiribati St. Lucia Tonga St. Vincent and the Grenadines Virgin Islands (U.S.)
50K–100K
9
Andorra Antigua and Barbuda Bermuda Cayman Islands Dominica Faroe Islands Greenland Isle of Man Seychelles
30K–50K
12
American Samoa Gibraltar St. Kitts and Nevis Liechtenstein St. Martin (French part) Monaco Marshall Islands Northern Mariana Islands San Marino Sint Maarten (Dutch part) Turks and Caicos Islands British Virgin Islands
10K–20K
3
Nauru Palau Tuvalu