Using a summarized version of the pizzaplace dataset, let’s create a gt table that displays the counts and percentages of the top 3 pizzas sold by pizza category in 2015. The cols_merge_n_pct() function is used to merge the n and frac columns (and the frac column is formatted using fmt_percent()).
pizzaplace |> dplyr::count(name, type, price, sort =TRUE) |> dplyr::mutate(frac =prop.table(n)) |> dplyr::slice_max(n, n =3, by = type) |> dplyr::arrange(type) |>gt(rowname_col ="name",groupname_col ="type" ) |>fmt_currency(price) |>fmt_percent(frac) |>cols_merge_n_pct(col_n = n,col_pct = frac ) |>cols_label(n =md("*N* (%)"),price ="Price" ) |>tab_style(style =cell_text(font ="monospace"),locations =cells_stub() ) |>tab_stubhead(md("Cat. and \\nPizza Code")) |>tab_header(title ="Top 3 Pizzas Sold by Category in 2015") |>tab_options(table.width =px(512))