The fmt_bins()
function
Use the countrypops
dataset to create a gt table. Before even getting to the gt()
call, we use cut()
in conjunction with scales::breaks_log()
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 fmt_integer()
(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 Russia
50M–100M
15
Congo (DRC) Colombia Germany France United Kingdom Iran Italy Kenya South Korea Myanmar Thailand Turkey 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 Côte d’Ivoire Cameroon Sri Lanka Madagascar Mali Niger North Korea Syria Venezuela
10M–20M
31
Azerbaijan Burundi Belgium Benin Bolivia Chile Cuba Czech Republic 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 (Republic) Costa Rica Denmark Finland Hong Kong Hungary Ireland Israel Kyrgyzstan Laos Lebanon Liberia Libya Nicaragua Norway New Zealand Papua New Guinea Paraguay Singapore Sierra Leone El Salvador Serbia Slovakia Togo Tajikistan Turkmenistan
3M–5M
12
Bosnia & Herzegovina Eritrea Georgia Croatia Kuwait Mongolia Mauritania Oman Panama Puerto Rico Palestine Uruguay
2M–3M
14
Albania Armenia Botswana Gabon Gambia Guinea-Bissau Jamaica Lesotho Lithuania Moldova North Macedonia Namibia Qatar Slovenia
1M–2M
10
Bahrain Cyprus Djibouti Estonia Equatorial Guinea Latvia Mauritius Eswatini East Timor Trinidad & Tobago
500K–1M
12
Bhutan Comoros Cape Verde Fiji Guyana Luxembourg Macao Maldives Malta Montenegro Solomon Islands Suriname
300K–500K
6
Bahamas Belize Brunei Iceland French Polynesia Vanuatu
200K–300K
4
Barbados New Caledonia São Tomé & Príncipe Samoa
100K–200K
10
Aruba Curaçao Micronesia Grenada Guam Kiribati St. Lucia Tonga St. Vincent & Grenadines U.S. Virgin Islands
50K–100K
9
Andorra Antigua & Barbuda Bermuda Cayman Islands Dominica Faroe Islands Greenland Isle of Man Seychelles
30K–50K
12
American Samoa Gibraltar St. Kitts & Nevis Liechtenstein St. Martin Monaco Marshall Islands Northern Mariana Islands San Marino Sint Maarten Turks & Caicos Islands British Virgin Islands
10K–20K
3
Nauru Palau Tuvalu