Add a percent stacked barchart in place of existing data.
Source:R/gt_plt_bar_stack_extra.R
gt_plt_bar_stack_extra.Rd
The gt_plt_bar_stack_extra
is a copy paste of the function
gt_plt_bar_stack
available in the gtExtras
package where the
limitation of 3 user-specified has been risen to 4. It takes an existing
gt_tbl
object and converts the existing values into a percent stacked
barchart. The bar chart will represent either 2, 3 or 4 user-specified values
per row, and requires a list column ahead of time. The palette and labels need
to be equal length. The values must either add up to 100 i.e. as
percentage points if using position = 'fill'
, or can be raw values
with position = 'stack'
. Note that the labels can be controlled via
the fmt_fn
argument and the scales::lab_xxx
family of function.
Arguments
- gt_object
An existing gt table object of class gt_tbl
- column
The column wherein the percent stacked barchart should replace existing data. Note that the data must be represented as a list of numeric values ahead of time.
- palette
A color palette of length 2 or 3, represented either by hex colors (
#ff4343
) or named colors (red
).- labels
A vector of strings of length 2 or 3, representing the labels for the bar chart, will be colored according to the palette as well.
- position
An string indicator passed to ggplot2 indicating if the bar should be a percent of total
fill
or stacked as the raw valuesstack
.- width
An integer representing the width of the bar chart in pixels.
- fmt_fn
A specific function from
scales::lab_xxx
family. Defaults toscales::label_number
Examples
if (FALSE) {
# load library
library(gt)
library(dplyr)
# dummy dataset
ex_df <- dplyr::tibble(
x = c(
"Example 1", "Example 1",
"Example 1", "Example 1", "Example 2", "Example 2", "Example 2", "Example 2",
"Example 3", "Example 3", "Example 3", "Example 3", "Example 4", "Example 4", "Example 4",
"Example 4"
),
measure = c(
"Measure 1", "Measure 2",
"Measure 3", "Measure 4", "Measure 1", "Measure 2", "Measure 3", "Measure 4",
"Measure 1", "Measure 2", "Measure 3", "Measure 4", "Measure 1", "Measure 2",
"Measure 3", "Measure 4"
),
data = c(30, 20, 40, 10, 30, 30, 20, 20, 30, 10, 30, 30, 30, 50, 10, 10)
)
# display results
ex_df %>%
group_by(x) %>%
summarise(list_data = list(data)) %>%
gt() %>%
gt_plt_bar_stack_extra(column = list_data)
}