| tab_many {tabxplor} | R Documentation |
Many cross-tables as one, with color helpers
Description
A full-featured function to create, manipulate and format many cross-tables
as one, using colors to make the printed tab more easily readable (in R terminal or
exported to Excel with tab_xl).
Since objects of class tab are also of class tibble, you can then use all
dplyr verbs to modify the result, like select,
arrange, filter
or mutate.
Only breaks for attractions/over-representations (in green) should be given, as a vector of positive doubles, with length between 1 and 5. Breaks for aversions/under-representations (in orange/red) will simply be the opposite.
Usage
tab_many(
data,
row_vars,
col_vars,
tab_vars,
wt,
levels = "all",
na = "keep",
na_drop_all,
digits = 0,
totaltab = "line",
totaltab_name = "Ensemble",
totrow = TRUE,
totcol = "last",
total_names = "Total",
pct = "no",
diff = "tot",
comp = "tab",
chi2 = FALSE,
ci = "no",
conf_level = 0.95,
method_cell = "wilson",
method_diff = "ac",
color = "no",
subtext = "",
cleannames = NULL,
rare_to_other = FALSE,
n_min = 30,
other_level = "Others",
filter
)
tab_get_vars(tabs, vars = c("row_var", "col_vars", "tab_vars"))
is_tab(x)
set_color_style(
type = c("text", "bg"),
theme = NULL,
html_24_bit = c("blue_red", "green_red", "no"),
custom_palette = NULL
)
get_color_style(
mode = c("crayon", "color_code"),
type = NULL,
theme = NULL,
html_24_bit = NULL
)
set_color_breaks(pct_breaks, mean_breaks, contrib_breaks)
get_color_breaks(brk, type = c("positive", "all"))
Arguments
data |
A data frame. |
row_vars |
The row variable, which will be printed with one level per line. If numeric, it will be converted to factor. If more than one row_var if provided, a different table is made for each of them. |
col_vars |
<tidy-select>
One column is printed for each level of each column variable.
For numeric variables means are calculated, in a single column.
To pass many variables you may use syntax |
tab_vars |
<tidy-select>
One subtable is made for each combination of levels of the tab variables.
To pass many variables you may use syntax |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
levels |
The levels of
|
na |
The policy to adopt with missing values. It must be a single string.
|
na_drop_all |
<tidy-select>
Removes all observations with a |
digits |
The number of digits to print, as a single integer, or an integer vector
the same length as |
totaltab |
The total table, if there are subtables/groups
(i.e. when
|
totaltab_name |
The name of the total table, as a single string. |
totrow |
By default, total rows are printed.
Set to |
totcol |
The policy with total columns. Vectorised over
|
total_names |
The names of the totals, as a character vector of length one or two.
Use syntax of type |
pct |
The type of percentages to calculate :
The argument is vectorised over both |
diff |
The reference cell to calculate differences (used to print
|
comp |
The comparison level : by subtables/groups, or for the whole table.
Vectorised over
|
chi2 |
Set to |
ci |
The type of confidence intervals to calculate, passed to
By default, for percentages, with |
conf_level |
The confidence level, as a single numeric between 0 and 1. Default to 0.95 (95%). |
method_cell |
Character string specifying which method to use with percentages
for |
method_diff |
Character string specifying which method to use with percentages
for |
color |
The type of colors to print, as a single string. Vectorised over
|
subtext |
A character vector to print rows of legend under the table. |
cleannames |
Set to |
rare_to_other |
When set to |
n_min |
The count under which a level is aggregated in the "Other" level. |
other_level |
The name of the "Other" level, as a single string. |
filter |
A |
tabs |
A |
vars |
In |
x |
A object to test with |
type |
Default to |
theme |
For |
html_24_bit |
Should specific 24bits colors palettes be used for html tables ?
With light themes only. Default to |
custom_palette |
Possibility to provide a custom color styles, as a character
vector of 10 html color codes (the five first for over-represented numbers,
the five last for under-represented ones). The result is saved to
|
mode |
By default, |
pct_breaks |
If they are to be changed, the breaks used for percentages.
Default to |
mean_breaks |
If they are to be changed, the breaks used for means.
Default to |
contrib_breaks |
If they are to be changed, the breaks used for contributions to
variance. Default to |
brk |
When missing, return all color breaks. Specify to return a given color
break, among |
Value
A tibble of class tab, possibly with colored reading helpers.
When there are two row_vars or more, a list of tibble of class tab.
All non-text columns are of class fmt, storing all
the data necessary to print formats and colors. Columns with row_var and
tab_vars are of class factor : every added factor will be
considered as a tab_vars and used for grouping. To add text columns without
using them in calculations, be sure they are of class character.
A list with the variables names.
A single logical.
Set global options "tabxplor.color_style_type" and
"tabxplor.color_style_theme", used when printing tab objects.
A vector of crayon color functions, or a vector of color html codes.
Set the global option "tabxplor.color_breaks" as a list different double vectors, and also returns it invisibly.
The color breaks as a double vector, or list of double vectors.
Functions
-
tab_get_vars(): Get the variables names of a tabxplortab -
is_tab(): a test function for class tabxplor_tab -
set_color_style(): define the color style used to printtab. -
get_color_style(): get color styles as crayon functions or html codes. -
set_color_breaks(): set the breaks used to print colors -
get_color_breaks(): get the breaks currently used to print colors
Examples
# Make a summary table with many col_vars, showing only one specific level :
library(dplyr)
first_lvs <- c("Married", "$25000 or more", "Strong republican", "Protestant")
data <- forcats::gss_cat %>% mutate(across(
where(is.factor),
~ forcats::fct_relevel(., first_lvs[first_lvs %in% levels(.)])
))
tab_many(data, race, c(marital, rincome, partyid, relig, age, tvhours),
levels = "first", pct = "row", chi2 = TRUE, color = "auto")
# Can be used with map and tribble to program several tables with different parameters
# all at once, in a readable way:
library(purrr)
library(tibble)
pmap(
tribble(
~row_var, ~col_vars , ~pct , ~filter , ~subtext ,
"race" , "marital" , "row", NULL , "Source: GSS 2000-2014",
"relig" , c("race", "age"), "row", "year %in% 2000:2010", "Source: GSS 2000-2010",
NA_character_, "race" , "no" , NULL , "Source: GSS 2000-2014",
),
.f = tab_many,
data = forcats::gss_cat, color = "auto", chi2 = TRUE)
set_color_style(type = "bg")
set_color_breaks(
pct_breaks = c(0.05, 0.15, 0.3),
mean_breaks = c(1.15, 2, 4),
contrib_breaks = c(1, 2, 5)
)