The fmt_fraction()
function
Use pizzaplace
to create a gt table. Format the f_sold
and f_income
columns to display fractions.
pizzaplace |>
dplyr::group_by(type, size) |>
dplyr::summarize(
sold = dplyr::n(),
income = sum(price),
.groups = "drop_last"
) |>
dplyr::group_by(type) |>
dplyr::mutate(
f_sold = sold / sum(sold),
f_income = income / sum(income),
) |>
dplyr::arrange(type, dplyr::desc(income)) |>
gt(rowname_col = "size") |>
tab_header(
title = "Pizzas Sold in 2015",
subtitle = "Fraction of Sell Count and Revenue by Size per Type"
) |>
fmt_integer(columns = sold) |>
fmt_currency(columns = income) |>
fmt_fraction(
columns = starts_with("f_"),
accuracy = 10,
simplify = FALSE,
layout = "diagonal"
) |>
sub_missing(missing_text = "") |>
tab_spanner(
label = "Sold",
columns = contains("sold")
) |>
tab_spanner(
label = "Revenue",
columns = contains("income")
) |>
text_transform(
locations = cells_body(),
fn = function(x) {
dplyr::case_when(
x == 0 ~ "<em>nil</em>",
x != 0 ~ x
)
}
) |>
cols_label(
sold = "Amount",
income = "Amount",
f_sold = md("_f_"),
f_income = md("_f_")
) |>
cols_align(align = "center", columns = starts_with("f")) |>
tab_options(
table.width = px(400),
row_group.as_column = TRUE
)
Pizzas Sold in 2015 |
Fraction of Sell Count and Revenue by Size per Type |
|
Sold
|
Revenue
|
Amount |
f |
Amount |
f |
chicken |
L |
4,932 |
4⁄10 |
$102,339.00 |
5⁄10 |
M |
3,894 |
4⁄10 |
$65,224.50 |
3⁄10 |
S |
2,224 |
2⁄10 |
$28,356.00 |
1⁄10 |
classic |
L |
4,057 |
3⁄10 |
$74,518.50 |
3⁄10 |
S |
6,139 |
4⁄10 |
$69,870.25 |
3⁄10 |
M |
4,112 |
3⁄10 |
$60,581.75 |
3⁄10 |
XL |
552 |
nil |
$14,076.00 |
1⁄10 |
XXL |
28 |
nil |
$1,006.60 |
nil |
supreme |
L |
4,564 |
4⁄10 |
$94,258.50 |
5⁄10 |
M |
4,046 |
3⁄10 |
$66,475.00 |
3⁄10 |
S |
3,377 |
3⁄10 |
$47,463.50 |
2⁄10 |
veggie |
L |
5,403 |
5⁄10 |
$104,202.70 |
5⁄10 |
M |
3,583 |
3⁄10 |
$57,101.00 |
3⁄10 |
S |
2,663 |
2⁄10 |
$32,386.75 |
2⁄10 |