Type: Package
Title: Easy Estimation and Vizualisation of Indicators from Data with Complex Design
Version: 0.5.1
Description: Many functions to easily vizualise and estimate indicators such as proportions, means, medians and continuous/discrete distributions from complex survey data. The package also estimates confidence intervals for all indicators, compares different groups and computes different statistical tests.
License: GPL-3
URL: https://jgires.github.io/fonctionr/, https://github.com/jgires/fonctionr/
BugReports: https://github.com/jgires/fonctionr/issues
Depends: R (≥ 4.1.0)
Imports: doParallel, dplyr, forcats, foreach, ggnewscale, ggplot2, ggtext, grDevices, rlang, scales, showtext, srvyr, stringr, survey, sysfonts, tibble, tidyr
Suggests: broom, colorspace, knitr, kableExtra, laeken, MetBrewer, openxlsx, patchwork, PrettyCols, rmarkdown
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.3.3
NeedsCompilation: no
Packaged: 2026-03-21 12:40:34 UTC; 00104504
Author: Joel Gires [aut, cre, cph], Francois Ghesquiere [aut]
Maintainer: Joel Gires <joel.gires@vivalis.brussels>
Repository: CRAN
Date/Publication: 2026-03-25 21:00:16 UTC

central_group

Description

Function to compare means or medians among different groups based on complex survey data. It produces a list containing a table, including the confidence intervals of the indicators, a ready-to-be published ggplot graphic and a statistical test.

In case of mean comparison, the statistical test is a Wald test (using survey::regTermTest()). In case of median comparison the statistical test is a Kruskal Wallis test (using survey::svyranktest()). The confidence intervals and the statistical test are taking into account the complex survey design. In case of facets, the statistical test is computed on the total means or medians between facets (and not within facets). In case of second group (group.fill), no statistical test is computed.

Exporting the results to an Excell file is possible.

Usage

central_group(
  data,
  group,
  quanti_exp,
  type,
  group.fill = NULL,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.group = TRUE,
  na.rm.facet = TRUE,
  total = TRUE,
  reorder = FALSE,
  show_ci = TRUE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  total_name = NULL,
  digits = 0,
  unit = "",
  dec = NULL,
  col = NULL,
  pal = "OBSS_Spring",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  wrap_width_leg = 25,
  legend_ncol = 4,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  legend_lab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

median_group(..., type = "median")

mean_group(..., type = "mean")

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

group

A variable defining groups to be compared.

quanti_exp

An expression defining the quantitative variable from which the mean/median is computed. Notice that if any observations with NA in at least one of the variable in quanti_exp are excluded for the computation of the indicators.

type

"mean" to compute mean by group ; "median" to compute median by group.

group.fill

A variable defining a second variable of groups to be compared.

facet

A variable defining the faceting group.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.group

TRUE if you want to remove observations with NA on the group and the group.fill variables. FALSE if you want to create a group with the NA values for the group variable and a group.fill with the NA values for the group.fill variable. Default is TRUE.

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

total

TRUE if you want to compute a total, FALSE if you don't. The default is TRUE.

reorder

TRUE if you want to reorder the groups according to the mean/median. NA value, if na.rm.group = FALSE, is not included in the reorder. In case of facets, the groups are reordered based on each median group. Default is FALSE.

show_ci

TRUE if you want to show the error bars on the graphic. FALSE if you don't want to show the error bars. Default is TRUE.

show_n

TRUE if you want to show on the graphic the number of observations in the sample in each group. FALSE if you don't want to show this number. Default is FALSE.

show_value

TRUE if you want to show the mean/median in each group on the graphic. FALSE if you don't want to show the mean/median. Default is TRUE.

show_labs

TRUE if you want to show axes and legend (in case of a group.fill) labels. FALSE if you don't want to show any labels on axes and legend. Default is TRUE.

total_name

Name of the total displayed on the graphic. Default is "Total" in French and in English and "Totaal" in Dutch.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is none ("").

dec

Decimal mark displayed on the graphic. Default depends on lang: "," for fr and nl ; "." for en.

col

Color of the bars if there is no group.fill. col must be a R color or an hexadecimal color code. Default color used depends on type : "deeppink3" for mean and "mediumorchid3" for median. The colors of total and NA group (in case of na.rm.group = FALSE) are always "grey40" and "grey". If there is a group.fill, col has no effect and pal argument should be used instead.

pal

Colors of the bars if there is a group.fill. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr. The color of missing values for group.fill (in case of na.rm.group = FALSE) and for the total are always "grey" and "grey40". If there is no group.fill, pal has no effect and col argument should be used instead.

direction

Direction of the palette color. Default is 1. The opposite direction is -1. If there is no group.fill, this argument has no effect.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::desaturate function from colorspace package for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::desaturate for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::desaturate for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars. dodge doesn't affect the spaces between second groups (group.fill). There is always no space between second groups.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the groups. Default is 25.

wrap_width_leg

Number of characters before going to the line for the labels of the group.fill. Default is 25.

legend_ncol

Number of columns in the legend. Default is 4.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays, for type = "mean", "Moyenne :" (if lang = "fr"), "Mean:" (if lang = "en") or "Gemiddelde:" (if lang = "nl"), or, for type = "median", "Médiane :" (if lang = "fr"), "Median:" (if lang = "en") or "Mediaan:" (if lang = "nl"), followed by the quanti_exp argument. To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to the y variable in the data. Default (ylab = NULL) displays the name of the group variable. To show no Y label, use ylab = "".

legend_lab

Legend (fill) label on the graphic. If legend_lab = NULL, legend label on the graphic will be group.fill. To show no legend label, use legend_lab = "".

caption

Caption of the graphic. This caption goes under de default caption showing the result of the statistical test. There is no way of not showing the result of the chi-square test as a caption.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes three (without group.fill) or two sheets (with a group.fill): the table, the graphic and the statistical test result.

Value

A list that contains a table, a ggplot graphic and, in most cases, a statistical test.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Creation of age categories
eusilc$age_cat <- cut(eusilc$age,
breaks = 6,
include.lowest = TRUE)

# Calculation of income means by age category with fonctionr, taking sample design into account
eusilc_mean <- mean_group(
  eusilc,
  group = age_cat,
  quanti_exp = eqIncome / 12,
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "Mean of equivalised income in household by age of individuals",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_mean$graph

# Results in table format
eusilc_mean$tab

distrib_continuous

Description

Function to describe the distribution of a continuous variable from complex survey data. It produces a list containing a density table (dens), a central value table (tab), a quantile table (quant) and a ready-to-be published ggplot graphic (graph).

The density table contains x-y coordinates to draw a density curve. The central value table contains the median or the mean of the continuous variable, with its confidence interval, the sample size and the estimation of the total, with its confidence interval. The quantile table contains quantiles and their confidence intervals. The quantiles and the limits are used as thicks on the X axe of the graphic. The confidence intervals are taking into account the complex survey design.

Exporting those results to an Excell file is possible.

Usage

distrib_continuous(
  data,
  quanti_exp,
  type = "median",
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.facet = TRUE,
  quantiles = seq(0.1, 0.9, 0.1),
  bw = 1,
  resolution = 1024,
  limits = NULL,
  show_mid_line = TRUE,
  show_ci_lines = TRUE,
  show_ci_area = FALSE,
  show_quant_lines = FALSE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  digits = 0,
  unit = "",
  dec = NULL,
  pal = NULL,
  col_density = c("#00708C", "mediumturquoise"),
  color = NULL,
  col_border = NA,
  font = "Roboto",
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

distrib_c(...)

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

quanti_exp

An expression defining the quantitatie variable the variable to be described. Notice that any observations with NA in at least one of the variable in quanti_exp are excluded for the computation of the density and of the indicators.

type

Type of central value : "mean" to compute mean as the central value ; "median" to compute median as the central value.

facet

Not yet implemented.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.facet

Not yet implemented.

quantiles

Quantiles computed. Default are deciles.

bw

The smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. Default is 1.

resolution

Resolution of the density curve. Default is 1024.

limits

Limits of the X axe of the graphic. Does not apply to the computation of indicators (median/mean and quantiles). Default is NULL to show the entire distribution on the graphic.

show_mid_line

TRUE if you want to show the mean or median (depending on type) as a line on the graphic. FALSE if you do not want to show it. Default is TRUE.

show_ci_lines

TRUE if you want to show confidence interval of the mean or median (depending on type) as dotted lines on the graphic. FALSE if you do not want to show it as lines. Default is TRUE.

show_ci_area

TRUE if you want to show confidence interval of the mean or median (depending on type) as a coloured area on the graphic. FALSE if you do not want to show it as an area. Default is FALSE.

show_quant_lines

TRUE if you want to show quantiles as lines on the graphic. FALSE if you do not want to show them as lines. Default is FALSE.

show_n

TRUE if you want to show on the graphic the number of individuals in the sample in each quantile. FALSE if you do not want to show the numbers. Default is FALSE.

show_value

TRUE if you want to show the value of the mean/median (depending on type) on the graphic. FALSE if you do not want to show the mean/median. Default is TRUE.

show_labs

TRUE if you want to show axes labels. FALSE if you do not want to show any labels on axes. Default is TRUE.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is none ("").

dec

Decimal mark shown on the graphic. Depends on lang: "," for fr and nl ; "." for en.

pal

For compatibility with older versions.

col_density

Color of the density area. It may be one color or a vector with several colors. Colors should be R color or an hexadecimal color code. In case of one color, the density is monocolor. In case of a vector, the quantile areas are painted in continuous colors going from the last color in the vector (center quantile) to the first color (first and last quantiles). In case of an even quantile area numbers (e.g. deciles, quartiles) the last color of the vector is only applied to the highcenter quantile area to avoid two continuous quantile areas having the same color.

color

Not currently used except for compatibility with old versions.

col_border

Color of the density line. Color should be one R color or one hexadecimal color code. Default (NULL) does not draw the density line.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. If xlab = NULL, X label on the graphic will be quanti_exp.

ylab

Y label on the graphic. If ylab = NULL, Y label on the graphic will be "Densité" (if lang = "fr"), "Density" (if lang = "en") or "Densiteit" (if lang = "nl").

caption

Caption of the graphic.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes four sheets: the central value table, the quantile table, the density table and the graphic.

Value

A list that contains a density table (dens), a central value table (tab), a quantile table (quant) and a ggplot graphic (graph).

Examples

# Loading of data
data(eusilc, package = "laeken")

# Computation, taking sample design into account
eusilc_dist_c <- distrib_c(
  eusilc,
  quanti_exp = eqIncome,
  strata = db040,
  ids = db030,
  weight = rb050,
  limits = c(0, 50000),
  title = "Distribution of eq. income",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_dist_c$graph

# Results in table format
eusilc_dist_c$tab

distrib_discrete

Description

Function to describe the distribution of a discrete variable from complex survey data.

It produces a list containing a table, including the confidence intervals of the indicators, a ready-to-be published ggplot graphic and, if proportions for H0 are specified, a Chi-Square statistical test (using survey::svygofchisq()). The confidence intervals and the statistical test are taking into account the complex survey design. In case of facets, no statistical test is (yet) computed.

Exporting those results to an Excell file is possible.

Usage

distrib_discrete(
  data,
  quali_var,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.facet = TRUE,
  na.rm.var = TRUE,
  probs = NULL,
  prop_method = "beta",
  reorder = FALSE,
  show_ci = TRUE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  scale = 100,
  digits = 0,
  unit = "%",
  dec = NULL,
  col = "sienna2",
  pal = NULL,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

distrib_d(...)

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

quali_var

The discrete variable to be described.

facet

A variable defining the faceting group.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

na.rm.var

TRUE if you want to remove observations with NA on the discrete variable. FALSE if you want to create a modality with NA values for the discrete variable. Default is TRUE.

probs

Vector of probabilities for H0 of the statistical test, in the correct order (will be rescaled to sum to 1). If probs = NULL, no statistical test is performed. Default is NULL.

prop_method

Type of proportion method used to compute confidence intervals. See survey::svyciprop() for details. Default is beta method.

reorder

TRUE if you want to reorder the groups according to the proportion. NA value, if na.rm.var = FALSE, is not included in the reorder. In case of facets, the categories are reordered based on each median category Default is FALSE.

show_ci

TRUE if you want to show the error bars on the graphic. FALSE if you don't want to show the error bars. Default is TRUE.

show_n

TRUE if you want to show on the graphic the number of observations in the sample in each category. FALSE if you don't want to show this number. Default is FALSE.

show_value

TRUE if you want to show the proportions in each category on the graphic. FALSE if you don't want to show the proportion. Default is TRUE.

show_labs

TRUE if you want to show axes labels. FALSE if you do not want to show any label on axes. Default is TRUE.

scale

Denominator of the proportion. Default is 100 to interprets numbers as percentages.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is "%".

dec

Decimal mark displayed on the graphic. Default depends on lang: "," for fr and nl ; "." for en.

col

Color of the bars. col must be a R color or an hexadecimal color code. Default is "sienna2". The color of NA category (in case of na.rm.var = FALSE) is always "grey".

pal

Argument kept for compatibility with old versions.

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the categories. Default is 25.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays "Distribution (total : 100 pourcent)" (if lang = "fr"), "Distribution (total: 100 percent)" (if lang = "en") or "Distributie (totaal : 100 procent)" (if lang = "nl"). To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the Y label on the graphic, after the ggplot2::coord_flip(), and not to the Y variable in the data. Default (ylab = NULL) displays the name of the discrete variable (quali_var). To show no Y label, use ylab = "".

caption

Caption of the graphic. This caption goes under de default caption showing the result of the statistical test (if any).

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes two or three sheets : the table, the graphic and the statistical test (if probs is not NULL).

Value

A list that contains a table, a ggplot graphic and, if probs is not NULL, a statistical test.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Computation, taking sample design into account
eusilc_dist_group_d <- distrib_d(
  eusilc,
  pl030_rec,
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "Distribution of socio-economic status",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_dist_group_d$graph

# Results in table format
eusilc_dist_group_d$tab

distrib_group_continuous

Description

Function to compare the distribution of a continuous variable between groups from complex survey data. It produces a list containing a density table (dens), a central value table (tab), a quantile table (quant), a ready-to-be published ggplot graphic (graph), a box-plot table (moustache) and a statistical test (test).

The density table contains x-y coordinates to draw density curve for each group. The central value table contains, for each group, the median or the mean of the continuous variable, with their confidence intervals, the sample size and the estimations of the totals, with their confidence intervals. The quantile table contains, for each group, quantiles and their confidence intervals. The box-plot table contains the X coordinates to draw the moustache, for each group.

In case of mean comparison, the statistical test is a Wald test (using survey::regTermTest()). In case of median comparison the statistical test is a Kruskal Wallis test (using survey::svyranktest()). The confidence intervals are taking into account the complex survey design.

Exporting those results to an Excell file is possible.

Usage

distrib_group_continuous(
  data,
  group,
  quanti_exp,
  type = "median",
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.group = TRUE,
  na.rm.facet = TRUE,
  quantiles = seq(0.1, 0.9, 0.1),
  moustache_probs = c(0.95, 0.8, 0.5),
  bw = 1,
  resolution = 512,
  height = 0.8,
  limits = NULL,
  reorder = FALSE,
  show_mid_point = TRUE,
  show_mid_line = FALSE,
  show_ci_errorbar = TRUE,
  show_ci_lines = FALSE,
  show_ci_area = FALSE,
  show_quant_lines = FALSE,
  show_moustache = TRUE,
  show_value = TRUE,
  show_labs = TRUE,
  digits = 0,
  unit = "",
  dec = NULL,
  pal = NULL,
  col_density = "#e0dfe0",
  pal_moustache = NULL,
  col_moustache = c("#EB9BA0", "#FAD7B1"),
  color = NULL,
  col_border = NA,
  alpha = 1,
  font = "Roboto",
  wrap_width_y = 25,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

distrib_group_c(...)

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

group

A variable defining groups to be compared.

quanti_exp

An expression defining the quantitatie variable the variable to be described and compared between groups. Notice that any observations with NA in at least one of the variable in quanti_exp are excluded for the computation of the densities and of the indicators.

type

Type of central value : "mean" to compute mean as the central value by group ; "median" to compute median as the central value by group.

facet

Not yet implemented.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.group

TRUE if you want to remove observations with NA on the group variable. FALSE if you want to create a group with the NA values for the group variable. Default is TRUE.

na.rm.facet

Not yet implemented.

quantiles

Quantiles computed in the distributions. Default are deciles.

moustache_probs

A vector defining the proportions of the population used to draw the boxplot. Default is c(0.95, 0.8, 0.5) to draw a boxplot with three groups containing respectively 50 percent, 80 percent and 95 percent of the population around to the median.

bw

The smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. Default is 1.

resolution

Resolution of the density curve. Default is 512.

height

Height of the curves. Default is 0.8. Values higher than 1 may cause curves to overlap.

limits

Limits of the x axe of the graphic. Does not apply to the computation. Default is NULL to show the entire distribution on the graphic. If the limits are shorter than the boxplot, some part of some boxplot will not be drawn.

reorder

TRUE if you want to reorder the groups according to the mean/median (depending on type). Unlike other functions, NA values, if na.rm.group = FALSE, is included in the reorder.

show_mid_point

TRUE if you want to show the mean or median (depending on type) as a point on the graphic. FALSE if you do not want to. Default is TRUE.

show_mid_line

TRUE if you want to show the mean or median (depending on type) as a line on the graphic. FALSE if you do not want to. Default is FALSE.

show_ci_errorbar

TRUE if you want to show confidence interval of the mean or median (depending on type) as an error bar on the graphic. FALSE if you do not want to show it as lines. Default is TRUE.

show_ci_lines

TRUE if you want to show confidence interval of the mean or median (depending on type) as lines on the graphic. FALSE if you do not want to show it as lines. Default is FALSE.

show_ci_area

TRUE if you want to show confidence interval of the mean or median (depending on type) as a coloured area on the graphic. FALSE if you do not want to show it as an area. Default is FALSE.

show_quant_lines

TRUE if you want to show quantiles as lines on the graphic. FALSE if you do not want to show them as lines. Default is FALSE.

show_moustache

TRUE if you want to show the boxplot on the graphic. FALSE if you do not want to show it. Default is TRUE.

show_value

TRUE if you want to show the value of mean/median of each group on the graphic. FALSE if you do not want to show the mean/median. Default is TRUE.

show_labs

TRUE if you want to show axes labels. FALSE if you do not want to show any labels on axes. Default is TRUE.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is none ("").

dec

Decimal mark shown on the graphic. Depends on lang: "," for fr and nl ; "." for en.

pal

For compatibility with older versions.

col_density

Color of the density area. It may be one color or a vector with several colors. Colors should be R color or an hexadecimal color code. In case of one color, the density is monocolor. In case of a vector, the quantile areas are painted in continuous colors going from the last color in the vector (center quantile) to the first color (first and last quantiles). In case of an even quantile area numbers (e.g. deciles, quartiles) the last color of the vector is only applied to the highcenter quantile area to avoid two continuous quantile areas having the same color.

pal_moustache

For compatibility with old versions.

col_moustache

Color of the moustache. Can be one or several colors to create a palette. In case of a vector, the different areas of the box-plot are painted in continuous colors going from the first color in the vector (center of the bo-plot) to the last color (extern area of the box-plot).

color

For compatibility with older versions.

col_border

Color of the density line. Color should be one R color or one hexadecimal color code. Default (NULL) does not draw the density line.

alpha

Transparence of the density areas. Default is 1. It applies only to col_density.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the groups. Default is 25.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. If xlab = NULL, X label on the graphic will be quanti_exp.

ylab

Y label on the graphic. Default (ylab = NULL) displays the name of the group variable. To show no Y label, use ylab = "".

caption

Caption of the graphic. This caption goes under de default caption showing the result of the Chi-Square test. There is no way of not showing the result of the statistical test as a caption.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes five sheets: the central values table, the quantile table, the densities table, the graphic and the statistical test result.

Value

A list that contains a density table (dens), a central values table (tab), a quantile table (quant), a ggplot graphic (graph), boxplot table (moustache) and a statistical test (test).

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Computation, taking sample design into account
eusilc_dist_g_c <- distrib_group_c(
  eusilc,
  group = pl030_rec,
  quanti_exp = eqIncome,
  strata = db040,
  ids = db030,
  weight = rb050,
  limits = c(0, 50000),
  resolution = 128,
  title = "Distribution of eq. income",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_dist_g_c$graph

# Results in table format
eusilc_dist_g_c$tab

distrib_group_discrete

Description

Function to compare the distribution of a discrete variable between different groups based on complex survey data.

It produces a list containing a table, including the confidence intervals of the indicators, a ready-to-be published ggplot graphic and a Chi-Square statistical test (using survey::svychisq()). The confidence intervals and the statistical test are taking into account the complex survey design. In case of facets, no statistical test is (yet) computed.

Exporting those results to an Excell file is possible.

Usage

distrib_group_discrete(
  data,
  group,
  quali_var,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.group = TRUE,
  na.rm.facet = TRUE,
  na.rm.var = TRUE,
  total = TRUE,
  prop_method = "beta",
  reorder = FALSE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  total_name = NULL,
  scale = 100,
  digits = 0,
  unit = "",
  dec = NULL,
  pal = "OBSS",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  wrap_width_leg = 25,
  legend_ncol = 4,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  legend_lab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

distrib_group_d(...)

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

group

A variable defining groups to be compared.

quali_var

The discrete variable described among the different groups.

facet

A variable defining the faceting group.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.group

TRUE if you want to remove observations with NA on the group. FALSE if you want to create a group with the NA values for the group variable. Default is TRUE.

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

na.rm.var

TRUE if you want to remove observations with NA on the discrete variable. FALSE if you want to create a modality with NA values for the discrete variable. Default is TRUE.

total

TRUE if you want to compute a total, FALSE if you don't. The default is TRUE.

prop_method

Type of proportion method used to compute confidence intervals. See survey::svyciprop() for details. Default is beta method.

reorder

TRUE if you want to reorder the groups according to the proportion of the first level of quali_var. NA group, if na.rm.group = FALSE, is not included in the reorder. In case of facets, the groups are reordered based on each median group. Default is FALSE.

show_n

TRUE if you want to show on the graphic the number of observations in the sample in each category (of quali_var) of each group. FALSE if you don't want to show this number. Default is FALSE.

show_value

TRUE if you want to show the proportion in each category of each group on the graphic. FALSE if you do not want to show the proportions. Proportions of 2 percent or less are never showed on the graphic. Default is TRUE.

show_labs

TRUE if you want to show axes and legend labels. FALSE if you don't want to show any labels on axes and legend. Default is TRUE.

total_name

Name of the total displayed on the graphic. Default is "Total" in French and in English and "Totaal" in Dutch.

scale

Denominator of the proportions. Default is 100 to interpret numbers as percentages.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit showed in the graphic. Default (unit = "") shows not unit on values and percent on the X axe.

dec

Decimal mark shown on the graphic. Depends on lang: "," for fr and nl ; "." for en.

pal

Colors of the bars. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr. The color of NA category (in case of na.rm.var = FALSE) is always "grey".

direction

Direction of the palette color. Default is 1. The opposite direction is -1.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. See colorspace::desaturate() for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). See colorspace::lighten() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). See colorspace::darken() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the groups. Default is 25.

wrap_width_leg

Number of characters before going to the line for the labels of quali_var. Default is 25.

legend_ncol

Number of columns in the legend. Default is 4.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays "Distribution : " (if lang = "fr"), "Distribution: " (if lang = "en") or "Distributie: " (if lang = "nl"), followed by the name of the discrete variable (quali_var). To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to the y variable in the data. Default (ylab = NULL) displays the name of the group variable. To show no Y label, use ylab = "".

legend_lab

Legend (fill) label on the graphic. Default (legend_lab = NULL) displays the name of the discrete variable (quali_var). To show no legend label, use legend_lab = "".

caption

Caption of the graphic. This caption goes under de default caption showing the result of the Chi-Square test. There is no way of not showing the result of the chi-square test as a caption.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes three (without facets) or two sheets (with facets): the table, the graphic and the Chi-Square statistical test result.

Value

A list that contains a table, a ggplot graphic and, in most cases, a Chi-square statistical test.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Computation, taking sample design into account
eusilc_dist_d <- distrib_group_d(
  eusilc,
  group = pb220a,
  quali_var = pl030_rec,
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "Distribution of socio-economic status according to nationality",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_dist_d$graph

# Results in table format
eusilc_dist_d$tab

esth_graph

Description

Function to construct a graphic following the aestetics of the other functions of functionr from a table. This function was created to align results generated outside fonctionr with the outputs of fonctionr.

Usage

esth_graph(
  tab,
  var,
  value,
  error_low = NULL,
  error_upp = NULL,
  facet = NULL,
  n_var = NULL,
  pvalue = NULL,
  reorder = FALSE,
  show_value = TRUE,
  name_total = NULL,
  scale = 1,
  digits = 2,
  unit = "",
  dec = ",",
  pal = NULL,
  col = "indianred4",
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  caption = NULL,
  theme = "fonctionr",
  coef_font = 1
)

Arguments

tab

dataframe with the indicators to be ploted.

var

The variable in tab with the labels of the indicator to be ploted.

value

The variable in tab with the values of the indicator to be ploted.

error_low

The variable in tab with the lower bound of the confidence interval. If either error_low or error_upp is NULL error bars are not shown on the graphic.

error_upp

The variable in tab with the upper bound of the confidence interval. If either error_low or error_upp is NULL error bars are not shown on the graphic.

facet

A variable in tab defining the faceting group, if applicable. Default is NULL.

n_var

The variable in tab containing the number of observations for each indicator ploted. Default (NULL) does not show the numbers of observations on the plot.

pvalue

The p-value to show in the caption. It can be a numeric value or the pvalue object from a statistical test.

reorder

TRUE if you want to reorder var according to value. FALSE if you do not want to reorder. NA and total labels in var are not included in the reorder. Default is FALSE.

show_value

TRUE if you want to show the values on the graphic. FALSE if you do not want to show them. Default is TRUE.

name_total

Name of the var label that may contain the total. When indicated, it is displayed separately (bold name and value color is 'grey40') on the graph.

scale

Denominator of the indicator. Default is 1 to not modify indicators.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

The unit displayed on the grphaic. Default is no unit ("").

dec

Decimal mark shown on the graphic. Default is ",".

pal

For compatibility with old versions.

col

Color of the bars. col must be a R color or an hexadecimal color code. Default is "indianred4". The color of NA and total are always "grey" and "grey40".

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of var Default is 25.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to var in tab.

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to value in tab.

caption

Caption of the graphic.

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

Value

A ggplot graphic.

Examples

# Making fictional dataframe
data_test <- data.frame(
  Indicators = c(
    "Variable 1",
    "Variable 2",
    "Variable 3",
    "Variable 4",
    "Variable 5",
    "Tot"
  ),
  Estimates = c(1.52, 1.63, 2.34, 4.15, 1.32, 2.13),
  IC_low = c(1.32, 1.4, 1.98, 4, 14.2, 26),
  IC_upp = c(1.73, 1.81, 22.4, 47.44, 1.45, 2.34),
  sample_size = c(215, 300, 129, 212, 189, 1045)
)

# Using dataframe to make a plot
plot_test <- esth_graph(data_test,
  var = Indicators,
  value = Estimates,
  error_low = IC_low,
  error_upp = IC_upp,
  n_var = sample_size,
  pvalue = .001,
  reorder = TRUE,
  show_value = TRUE,
  name_total = "Tot",
  scale = 1,
  digits = 1,
  unit = "%",
  dec = ".",
  col = "green4",
  dodge = 0.8,
  font = "Montserrat",
  wrap_width_y = 25,
  title = "Plot",
  subtitle = "Using fake data",
  xlab = "Proportion (in %)",
  ylab = "Indicators",
  caption = "Source: fictional own calculation",
  theme = "IWEPS"
)

# Result is a ggplot
plot_test

fonctionr_options

Description

Function to set global options for fonctionr. The arguments defined in the options are only active if the user has not manually specified a value for those arguments within the various functions. Arguments may be shared by multiple functions (if they have the same name) or specific to certain functions.

Usage

fonctionr_options(
  na.rm.group = NULL,
  na.rm.facet = NULL,
  na.prop = NULL,
  na.vars = NULL,
  na.rm.var = NULL,
  probs = NULL,
  total = NULL,
  prop_method = NULL,
  quantiles = NULL,
  moustache_probs = NULL,
  bw = NULL,
  resolution = NULL,
  height = NULL,
  limits = NULL,
  reorder = NULL,
  position = NULL,
  show_ci = NULL,
  show_mid_point = NULL,
  show_mid_line = NULL,
  show_ci_errorbar = NULL,
  show_ci_lines = NULL,
  show_ci_area = NULL,
  show_quant_lines = NULL,
  show_moustache = NULL,
  show_n = NULL,
  show_value = NULL,
  show_labs = NULL,
  total_name = NULL,
  scale = NULL,
  digits = NULL,
  unit = NULL,
  dec = NULL,
  col = NULL,
  pal = NULL,
  direction = NULL,
  desaturate = NULL,
  lighten = NULL,
  darken = NULL,
  col_density = NULL,
  col_moustache = NULL,
  col_border = NULL,
  alpha = NULL,
  dodge = NULL,
  font = NULL,
  wrap_width_y = NULL,
  wrap_width_leg = NULL,
  legend_ncol = NULL,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  legend_lab = NULL,
  caption = NULL,
  lang = NULL,
  theme = NULL,
  coef_font = NULL,
  parallel = NULL,
  erase_all = FALSE
)

Arguments

na.rm.group

na.rm.group argument.

na.rm.facet

na.rm.facet argument.

na.prop

na.prop argument.

na.vars

na.vars argument.

na.rm.var

na.rm.var argument.

probs

probs argument.

total

total argument.

prop_method

prop_method argument.

quantiles

quantiles argument.

moustache_probs

moustache_probs argument.

bw

bw argument.

resolution

resolution argument.

height

height argument.

limits

limits argument.

reorder

reorder argument.

position

position argument.

show_ci

show_ci argument.

show_mid_point

show_mid_point argument.

show_mid_line

show_mid_line argument.

show_ci_errorbar

show_ci_errorbar argument.

show_ci_lines

show_ci_lines argument.

show_ci_area

show_ci_area argument.

show_quant_lines

show_quant_lines argument.

show_moustache

show_moustache argument.

show_n

show_n argument.

show_value

show_value argument.

show_labs

show_labs argument.

total_name

total_name argument.

scale

scale argument.

digits

digits argument.

unit

unit argument.

dec

dec argument.

col

col argument.

pal

pal argument.

direction

direction argument.

desaturate

desaturate argument.

lighten

lighten argument.

darken

darken argument.

col_density

col_density argument.

col_moustache

col_moustache argument.

col_border

col_border argument.

alpha

alpha argument.

dodge

dodge argument.

font

font argument.

wrap_width_y

wrap_width_y argument.

wrap_width_leg

wrap_width_leg argument.

legend_ncol

legend_ncol argument.

title

title argument.

subtitle

subtitle argument.

xlab

xlab argument.

ylab

ylab argument.

legend_lab

legend_lab argument.

caption

caption argument.

lang

lang argument.

theme

theme argument.

coef_font

coef_font argument.

parallel

parallel argument.

erase_all

TRUE erases all the options. Default is FALSE.

Value

No return value, called for side effects.

Examples

# We set global settings
fonctionr_options(coef_font = 1.5, col = "magenta", caption = "Beautiful caption")

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Computation, taking sample design into account
eusilc_prop <- prop_group(
  eusilc,
  group = pl030_rec,
  prop_exp = py090n > 0,
  weight = rb050,
  title = "% of ind. receiving unemployment benefits in their hh",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_prop$graph

# We set back settings to default
fonctionr_options(erase_all = TRUE)

load_and_active_fonts

Description

Function to load and activate fonctionr's built-in fonts. Available fonts, included in the package itself, are "Roboto", "Montserrat", "Gotham Narrow", and "Euclid Circular A". Default is "Roboto".

Usage

load_and_active_fonts()

Value

No return value, called for side effects.

Examples

# Loading of fonts from fonctionr. You can now use it in fonctionr !
load_and_active_fonts()

make_surface

Description

Function that represents the values of a group variable as areas proportional to those values.

Usage

make_surface(
  tab,
  var,
  value,
  error_low = NULL,
  error_upp = NULL,
  facet = NULL,
  pvalue = NULL,
  reorder = FALSE,
  compare = FALSE,
  space = NULL,
  position = "mid",
  show_ci = TRUE,
  name_total = "Total",
  digits = 0,
  unit = NULL,
  col = NULL,
  pal = "OBSS_Autumn",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  size_text = 3.88,
  bg = "#f8f5f5",
  linewidth_ci = 0.5,
  ratio = 3/2,
  font = "Roboto",
  wrap_width_lab = 20,
  title = NULL,
  subtitle = NULL,
  hjust.title = 0,
  caption = NULL,
  coef_font = 1
)

Arguments

tab

dataframe with the variables to be ploted.

var

The variable in tab with the labels of the indicator to be ploted.

value

The variable in tab with the values of the indicator to be ploted.

error_low

The variable in tab that is the lower bound of the confidence interval. If either error_low or error_upp is NULL error rectangles are not shown on the graphic.

error_upp

The variable in tab that is the upper bound of the confidence interval. If either error_low or error_upp is NULL error rectangles are not shown on the graphic.

facet

A variable in tab defining the faceting group, if applicable. Default is NULL.

pvalue

The p-value to show in the caption. It can be a numeric value or the pvalue object from a statistical test.

reorder

TRUE if you want to reorder the values. NA label in var is not included in the reorder.

compare

TRUE to display a rectangle representing the smallest value. When facets are enabled, this is the smallest value per facet category.

space

The space between the rectangles. The unit is that of the indicator.

position

The position of the rectangles: "mid" for center alignment, "bottom" for bottom alignment.

show_ci

TRUE if you want to show the CI on the graphic. The bounds of the confidence intervals are displayed as dotted rectangles around the result. FALSE if you do not want to show them. Default is TRUE.

name_total

Name of the var label that may contain the total. When indicated, it is not displayed on the graph.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

The unit showd on the plot. Default is none ("").

col

Color of the rectangles if the user wants a monocolor graph. col must be a R color or an hexadecimal color code. As pal has a priority over col, if the user wants to use col, he must not use simultaneously the pal argument (even pal = NULL).

pal

Colors of the rectangles if the user wants the rectangles to have different colors. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr. pal has a priority over col.

direction

Direction of the palette color. Default is 1. The opposite direction is -1.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::desaturate() for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::lighten() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::darken() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

size_text

Text size displayed in rectangles . Default is 3.88 (as in ggplot2).

bg

Color of the background. bg must be a R color or an hexadecimal color code.

linewidth_ci

Line width of the dotted confidence intervals lines. It affects also the lenghts of the dots and spaces bteween dots. Default is 0.5 to have confidence lines two times thiner than the lines of the indicators.

ratio

Ratio between the length and the width of the rectangles. 1 produces squares ; greater than 1 produces vertical rectangles and smaller than 1 produces horizontal rectangles. Default is 3/2.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_lab

Number of characters before going to the line for the labels of the categories of var. Default is 20.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

hjust.title

Horizontal alignment of title & subtitle. It should take a numeric value. Default (0) leads to left alignment, 1 leads to right alignment and 0.5 leads to centered alignement.

caption

Caption of the graphic.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

Value

A ggplot graphic.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Calculation of income means by age category with fonctionr, taking sample design into account
eusilc_mean <- mean_group(
  eusilc,
  group = pl030_rec,
  quanti_exp = py010n + py050n + py090n + py100n + py110n + py120n + py130n + py140n,
  filter_exp = !pl030_rec %in% c("Student", "Fulfilling domestic tasks") & db040 == "Tyrol",
  weights = rb050
)

# Displaying results with make_surface()
eusilc_mean$tab |>
  make_surface(
    var = pl030_rec,
    value = mean,
    error_low = mean_low,
    error_upp = mean_upp,
    reorder = TRUE,
    wrap_width_lab = 15,
    unit = "€",
    title = "Equivalised income in household by socio-economic status",
    subtitle = "Example with austrian SILC data from 'laeken' package"
)

many_val

Description

Function to compute the proportions of a set of several binary variables or means or medians of a set of quantitative variables, based on complex survey data.

It produces a list containing a table, including the confidence intervals of the indicators and a ready-to-be published ggplot graphic. The confidence intervals are taking into account the complex survey design.

Exporting the results to an Excell file is possible.

Usage

many_val(
  data,
  list_vars,
  type,
  list_vars_lab = NULL,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.facet = TRUE,
  na.vars = "rm",
  prop_method = "beta",
  reorder = FALSE,
  show_ci = TRUE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  scale = NULL,
  digits = 0,
  unit = NULL,
  dec = NULL,
  col = NULL,
  pal = "OBSS_alt3",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

many_prop(..., type = "prop")

many_median(..., type = "median")

many_mean(..., type = "mean")

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

list_vars

A vector containing the names of the dummy/quantitative variables on which to compute the proportions/means/medians.

type

"prop" to compute proportions ; "mean" to compute means ; "median" to compute medians.

list_vars_lab

A vector containing the labels of the dummy/quantitative variables to be displayed on the graphic and in the table of result. Default uses the variable names in list_vars.

facet

A variable defining the faceting group.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

na.vars

The treatment of NA values in variables (list_vars). "rm" removes NA seperately in each individual variable, "rm.all" removes every individual that has at least one NA in one variable. Default is "rm".

prop_method

Type of proportion method used to compute confidence intervals. See survey::svyciprop() for details. Default is beta method. This argument is only used in case of type = "prop".

reorder

TRUE if you want to reorder the variables according to the proportions/means/medians. Default is FALSE.

show_ci

TRUE if you want to show the error bars on the graphic. FALSE if you don't want to show the error bars. Default is TRUE.

show_n

TRUE if you want to show on the graphic the number of observations in the sample for each variable. The number can varie if na.vars = "rm". FALSE if you do not want to show this number. Default is FALSE.

show_value

TRUE if you want to show the proportions/means/median for each variable on the graphic. FALSE if you do not want to show the proportions/means/medians. Default is TRUE.

show_labs

TRUE if you want to show axes labels. FALSE if you do not want to show any labels on axes. Default is TRUE.

scale

Denominator of the proportions. Default is 100 to interpret numbers as percentages. This argument is only used in case of type = "prop".

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is percent for type = "prop" and no unit for type = "mean" or "median".

dec

Decimal mark displayed on the graphic. Default depends on lang: "," for fr and nl ; "." for en.

col

Color of the bars if the user wants a monocolor graph. col must be a R color or an hexadecimal color code. As pal has a priority over col, if the user wants to use col, he must not use simultaneously the pal argument (even pal = NULL).

pal

Colors of the bars if the user wants the bars to have different colors. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr. pal has a priority over col.

direction

Direction of the palette color. Default is 1. The opposite direction is -1.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::desaturate() for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::lighten() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. It affects only the palette (pal) and not the monocolor (col). See colorspace::darken() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the groups. Default is 25.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays, for type = prop, "Proportion :" (if lang = "fr"), "Proportion:" (if lang = "en") or "Aandeel:" (if lang = "nl"), or, for type = "mean", "Moyenne :" (if lang = "fr"), "Mean:" (if lang = "en") or "Gemiddelde:" (if lang = "nl"), or, for type = "median", "Médiane :" (if lang = "fr"), "Median:" (if lang = "en") or "Mediaan:" (if lang = "nl"), followed by the labels of the variables (list_vars_lab). To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to the y variable in the data. Default (ylab = NULL) displays no Y label.

caption

Caption of the graphic.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes two sheets: the table and the graphic.

Value

A list that contains a table and a ggplot graphic.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding variables
eusilc$worker <- 0
eusilc$worker[eusilc$pl030 == "1"]<-1
eusilc$worker[eusilc$pl030 == "2"]<-1
eusilc$austrian<-0
eusilc$austrian[eusilc$pb220a == "AT"]<-1

# Computation, taking sample design into account
eusilc_many_prop <- many_prop(
  eusilc,
  list_vars = c(worker,austrian),
  list_vars_lab = c("% of workers","% of Austrian"),
  facet = rb090,
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "Proportion of workers and Autrian according to gender",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_many_prop$graph

# Results in table format
eusilc_many_prop$tab

many_val_group

Description

Function to compare de proportions/means/medians of a set of several binary/quantitatives variables between different groups, based on complex survey data. It produces a list containing a table, including the confidence intervals of the indicators and a ready-to-be published ggplot graphic. The confidence intervals are taking into account the complex survey design.

Exporting the results to an Excell file is possible.

Usage

many_val_group(
  data,
  group,
  list_vars,
  type,
  list_vars_lab = NULL,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.group = TRUE,
  na.rm.facet = TRUE,
  na.vars = "rm",
  total = TRUE,
  prop_method = "beta",
  position = "dodge",
  show_ci = TRUE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  total_name = NULL,
  scale = NULL,
  digits = 0,
  unit = NULL,
  dec = NULL,
  pal = "OBSS_alt3",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  wrap_width_leg = 25,
  legend_ncol = 4,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  legend_lab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL,
  parallel = NULL
)

many_prop_group(..., type = "prop")

many_median_group(..., type = "median")

many_mean_group(..., type = "mean")

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

group

A variable defining groups to be compared.

list_vars

A vector containing the names of the dummy/quantitative variables on which to compute the proportions/means/medians.

type

"prop" to compute proportions by group ; "mean" to compute means by group ; "median" to compute medians by group.

list_vars_lab

A vector containing the labels of the dummy/quantitative variables to be displayed on the graphic and in the table of result. Default uses the variable names in list_vars.

facet

A variable defining the faceting group.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.group

TRUE if you want to remove observations with NA on the group variable. FALSE if you want to create a group with the NA values for the group variable. Default is TRUE.

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

na.vars

The treatment of NA values in variables (list_vars). "rm" removes NA seperately in each individual variable, "rm.all" removes every individual that has at least one NA in one variable. Default is "rm".

total

TRUE if you want to compute a total, FALSE if you don't. Default is TRUE. Total is not displayed nor computed if position = 'flip'.

prop_method

Type of proportion method used to compute confidence intervals. See survey::svyciprop() for details. Default is beta method.

position

Position adjustment for the ggplot. Default is "dodge". Other possible values are "flip" and "stack". "dodge" means that groups are on the y axe and variables are in differents colors, "flip" means that variables are on the y axe and groups are in differents colors, and "stack" means that groups are on the y axe and variables are stacking with differents colors. The latter is usefull when the variables are component of a broader sum variable (e.g. different sources of income). If position = 'flip', total is not displayed nor computed. If position = "stack", confidence intervals are never shown on the graphic.

show_ci

TRUE if you want to show the error bars on the graphic. FALSE if you do not want to show the error bars. Default is TRUE. If position = "stack", confidence intervals are never shown on the graphic.

show_n

TRUE if you want to show on the graphic the number of observations in the sample for each group and variable. The number can varie between variables if na.vars = "rm". FALSE if you do not want to show this number. Default is FALSE.

show_value

TRUE if you want to show the proportions/means/median for each group and variable on the graphic. FALSE if you do not want to show the proportions/means/medians. Default is TRUE.

show_labs

TRUE if you want to show axes labels. FALSE if you do not want to show any labels on axes. Default is TRUE.

total_name

Name of the total bars on the graphic. Default is Total. Notice that total is not displayed nor computed if position = 'flip'.

scale

Denominator of the proportions. Default is 100 to interpret numbers as percentages. This argument is only used in case of type = "prop".

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is percent for type = "prop" and no unit for type = "mean" or "median".

dec

Decimal mark displayed on the graphic. Default depends on lang: "," for fr and nl ; "." for en.

pal

Colors of the bars. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr.

direction

Direction of the palette color. Default is 1. The opposite direction is -1.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. See colorspace::desaturate() for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. See colorspace::lighten() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. See colorspace::darken() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars. dodge doesn't affect the spaces between sub-groups (group in case of position = 'dodge' or variables in case of position = 'flip'). There is always no space between sub-groups.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels on de Y axe (groups if position = 'dodge' or 'stack', variables if position = 'flip'). Default is 25.

wrap_width_leg

Number of characters before going to the line for the labels the legend (variables if position = 'dodge' or 'stack', groups if position = 'flip'). Default is 25.

legend_ncol

Number maximum of columns in the legend. Default is 4.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays "Proportion :" (if lang = "fr"), "Proportion:" (if lang = "en") or "Aandeel:" (if lang = "nl") folowed by the names of the variables (list_vars). To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to the y variable in the data. Default (ylab = NULL) displays the name of the groups variable (if position = 'dodge' or 'stack') or no Y axe label (if position = 'flip'). To show no Y label, use ylab = "".

legend_lab

Legend (fill) label on the graphic. Default (legend_lab = NULL) displays no legend label (if position = 'dodge' or 'stack') or the name of the groups variable (if position = 'flip'). To show no legend label, use legend_lab = "".

caption

Caption of the graphic.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes two sheets: the table and the graphic.

parallel

TRUE to enable parallel computing. Default is TRUE if replicated weights are used, otherwise default is FALSE.

Value

A list that contains a table and a graphic.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding variables
eusilc$worker <- 0
eusilc$worker[eusilc$pl030 == "1"] <- 1
eusilc$worker[eusilc$pl030 == "2"] <- 1
eusilc$austrian <- 0
eusilc$austrian[eusilc$pb220a == "AT"] <- 1

# Computation, taking sample design into account
eusilc_many_mean_group <- many_mean_group(
  eusilc,
  group = rb090,
  list_vars = c(py010n,py050n,py090n,py100n),
  list_vars_lab = c("Wage","Self-employement income","unemployement benefit","pension"),
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "Average incomes according to gender",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_many_mean_group$graph

# Results in table format
eusilc_many_mean_group$tab

official_pal

Description

A function that allows you to create the palettes of fonctionr.

Usage

official_pal(
  inst,
  n,
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  show_pal = FALSE,
  font = "Gotham Narrow",
  list_pal_names = FALSE
)

Arguments

inst

Name of the palette.

n

Number of colors.

direction

Direction of the palette color. Default is 1. The opposite direction is -1.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation, 0 to no desaturation, and values in between to partial desaturation.

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening.

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening.

show_pal

TRUE to display a graph representing the specified color palette.

font

Font used in the graphic. See load_and_active_fonts() for available fonts.

list_pal_names

TRUE to generate a vector with palette names.

Value

A vector containing hexadecimals color codes.

Examples

official_pal("OBSS", 8, show_pal = TRUE)
official_pal("OBSS_Greens", 8, show_pal = TRUE)
official_pal("OBSS_div_mid4", 7, show_pal = TRUE)
official_pal("OBSS_div_bi3", 8, show_pal = TRUE)
official_pal("IBSA", 4, show_pal = TRUE)
official_pal("ULB", 6, show_pal = TRUE)

pivot_longer_survey

Description

Function to pivot, from wide to long, a dataframe produced by srvyr::summarise() with results by group(s).

Usage

pivot_longer_survey(data, n_groups)

Arguments

data

A dataframe produced by srvyr::summarise() with group(s).

n_groups

Number of groups by which data has been agregated.

Value

A pivoted dataframe

Examples

# Loading data
data(eusilc, package = "laeken")

# Loading srvyr
library(srvyr)

# Making srvyr object
eusilc_srvyr <- as_survey_design(eusilc, ids = db030, strata = db040, weights = rb050)

# computing srvyr result using summarise()
result_srvyr<-eusilc_srvyr |>
  group_by(rb090, pb220a) |> # by sex and nationality
  summarise(mean_eqIncome = survey_mean(eqIncome), mean_age = survey_mean(age))

# Showing the srvyr summirise output
result_srvyr

# Pivoting the out with pivot_longer_survey()
pivoted_result <- pivot_longer_survey(result_srvyr, n_groups = 2)

# Output is pivoted
pivoted_result

prop_group

Description

Function to compare a proportion among different groups based on complex survey data. It produces a list containing a table, including the confidence intervals of the indicators, a ready-to-be published ggplot graphic and a Chi-Square statistical test (using survey::svychisq()).

The confidence intervals and the statistical test are taking into account the complex survey design. In case of facets, the Chi-square test is computed on the total proportion between facets (and not within facets). In case of second group (group.fill), no Chi-square test is computed.

Exporting those results to an Excell file is possible.

Usage

prop_group(
  data,
  group,
  prop_exp,
  group.fill = NULL,
  facet = NULL,
  filter_exp = NULL,
  ...,
  na.rm.group = TRUE,
  na.rm.facet = TRUE,
  na.prop = "rm",
  total = TRUE,
  prop_method = "beta",
  reorder = FALSE,
  show_ci = TRUE,
  show_n = FALSE,
  show_value = TRUE,
  show_labs = TRUE,
  total_name = NULL,
  scale = 100,
  digits = 0,
  unit = "%",
  dec = NULL,
  col = "deepskyblue3",
  pal = "OBSS_Relax",
  direction = 1,
  desaturate = 0,
  lighten = 0,
  darken = 0,
  dodge = 0.9,
  font = "Roboto",
  wrap_width_y = 25,
  wrap_width_leg = 25,
  legend_ncol = 4,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  legend_lab = NULL,
  caption = NULL,
  lang = "fr",
  theme = "fonctionr",
  coef_font = 1,
  export_path = NULL
)

Arguments

data

A dataframe or an object from the survey package or an object from the srvyr package.

group

A variable defining the groups to be compared.

prop_exp

An expression defining the proportion to be computed. Notice that if na.prop = "rm", is.na() is not allowed in this argument. The removal of NA is done before the computation of the proportion. Thus any function that takes into account NA (e.g. %in%) will not work as designed in this argument, unless na.prop = "include".

group.fill

A variable defining a second variable of groups to be compared.

facet

A variable defining the faceting groups.

filter_exp

An expression filtering the data, preserving the design. Notice that filter_exp works as srvyr::filter(): it excludes observations for which filter_exp results into NA. It is often the case when NA is present on one of the filter variables.

...

All options possible in srvyr::as_survey_design().

na.rm.group

TRUE if you want to remove observations with NA on the group and the group.fill variables. FALSE if you want to create a group with the NA values for the group variable and a group.fill with the NA values for the group.fill variable. Default is TRUE.

na.rm.facet

TRUE if you want to remove observations with NA on the facet variable. FALSE if you want to create a facet with the NA values for the facet variable. Default is TRUE.

na.prop

"rm" to remove observations with NA on one of the variables used in prop_exp before computing the proportions, "include" to compute the proportions with the NA in the denominators. Default is "rm". If na.prop = "rm" the function is.na() is not allowed in prop_exp.

total

TRUE if you want to compute a total, FALSE if you don't. The default is TRUE.

prop_method

Type of proportion method used to compute confidence intervals. See survey::svyciprop() for details. Default is beta method.

reorder

TRUE if you want to reorder the groups according to the proportion. NA value, if na.rm.group = FALSE, is not included in the reorder. In case of facets, the groups are reordered based on each median group. Default is FALSE.

show_ci

TRUE if you want to show the error bars on the graphic. FALSE if you don't want to show the error bars. Default is TRUE.

show_n

TRUE if you want to show on the graphic the number of observations in the sample in each group. FALSE if you don't want to show this number. Default is FALSE.

show_value

TRUE if you want to show the proportions in each group on the graphic. FALSE if you don't want to show the proportion. Default is TRUE.

show_labs

TRUE if you want to show axes and legend (in case of a group.fill) labels. FALSE if you don't want to show any labels on axes and legend. Default is TRUE.

total_name

Name of the total displayed on the graphic. Default is "Total" in French and in English and "Totaal" in Dutch.

scale

Denominator of the proportions. Default is 100 to interpret numbers as percentages.

digits

Number of decimal places displayed on the values labels on the graphic. Default is 0.

unit

Unit displayed on the graphic. Default is "%".

dec

Decimal mark displayed on the graphic. Default depends on lang: "," for fr and nl ; "." for en.

col

Color of the bars if there is no group.fill. col must be a R color or an hexadecimal color code. Default is "deepskyblue3". The colors of total and NA group (in case of na.rm.group = FALSE) are always "grey40" and "grey". If there is a group.fill, col has no effect and pal argument should be used instead.

pal

Colors of the bars if there is a group.fill. pal must be vector of R colors or hexadecimal colors or a palette from packages MetBrewer or PrettyCols or a palette from fonctionr. The color of missing values in group.fill (in case of na.rm.group = FALSE) and of the total are always "grey" and "grey40". If there is no group.fill, pal has no effect and col argument should be used instead.

direction

Direction of the palette color. Default is 1. The opposite direction is -1. If there is no group.fill, this argument has no effect.

desaturate

Numeric specifying the amount of desaturation where 1 corresponds to complete desaturation (no colors, grey layers only), 0 to no desaturation, and values in between to partial desaturation. Default is 0. It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::desaturate() for details. If desaturate and lighten/darken arguments are used, lighten/darken is applied in a second time (i.e. on the color transformed by desaturate).

lighten

Numeric specifying the amount of lightening. Negative numbers cause darkening. Value shoud be ranged between -1 (black) and 1 (white). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::lighten() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

darken

Numeric specifying the amount of lightening. Negative numbers cause lightening. Value shoud be ranged between -1 (white) and 1 (black). Default is 0. It doesn't affect the color of NA (in case of na.rm.group = FALSE). It affects only the palette (pal, if there is a second group) and not the monocolor (col, if there is no second group). See colorspace::darken() for details. If both argument ligthen and darken are used (not advised), darken is applied in a second time (i.e. on the color transformed by lighten).

dodge

Width of the bars. Default is 0.9 to let a small space between bars. A value of 1 leads to no space betweens bars. Values higher than 1 are not advised because they cause an overlaping of the bars. dodge doesn't affect the spaces between second groups (group.fill). There is always no space between second groups.

font

Font used in the graphic. See load_and_active_fonts() for available fonts. Default is "Roboto".

wrap_width_y

Number of characters before going to the line for the labels of the groups. Default is 25.

wrap_width_leg

Number of characters before going to the line for the labels of the group.fill. Default is 25.

legend_ncol

Number of columns in the legend. Default is 4.

title

Title of the graphic.

subtitle

Subtitle of the graphic.

xlab

X label on the graphic. As ggplot2::coord_flip() is used in the graphic, xlab refers to the x label on the graphic, after the ggplot2::coord_flip(), and not to the x variable in the data. Default (xlab = NULL) displays "Proportion :" (if lang = "fr"), "Proportion:" (if lang = "en") or "Aandeel:" (if lang = "nl"), followed by the prop_exp argument. To show no X label, use xlab = "".

ylab

Y label on the graphic. As ggplot2::coord_flip() is used in the graphic, ylab refers to the y label on the graphic, after the ggplot2::coord_flip(), and not to the y variable in the data. Default (ylab = NULL) displays the name of the group variable. To show no Y label, use ylab = "".

legend_lab

Legend (fill) label on the graphic. Default (legend_lab = NULL) displays the name of the group.fill variable. To show no legend label, use legend_lab = "".

caption

Caption of the graphic. This caption goes under de default caption showing the result of the Chi-Square test. There is no way of not showing the result of the chi-square test as a caption.

lang

Language of the indications on the graphic. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

theme

Theme of the graphic. Default is "fonctionr". "IWEPS" adds y axis lines and ticks. NULL uses the default grey ggplot2 theme.

coef_font

A multiplier factor for font size of all fonts on the graphic. Default is 1. Usefull when exporting the graphic for a publication (e.g. in a Quarto document).

export_path

Path to export the results in an xlsx file. The file includes three (without group.fill) or two sheets (with a group.fill): the table, the graphic and the Chi-Square statistical test result.

Value

A list that contains a table, a ggplot graphic and, in most cases, a Chi-square statistical test.

Examples

# Loading of data
data(eusilc, package = "laeken")

# Recoding eusilc$pl030 into eusilc$pl030_rec
eusilc$pl030_rec <- NA
eusilc$pl030_rec[eusilc$pl030 == "1"] <- "Working full time"
eusilc$pl030_rec[eusilc$pl030 == "2"] <- "Working part time"
eusilc$pl030_rec[eusilc$pl030 == "3"] <- "Unemployed"
eusilc$pl030_rec[eusilc$pl030 == "4"] <- "Student"
eusilc$pl030_rec[eusilc$pl030 == "5"] <- "Retired"
eusilc$pl030_rec[eusilc$pl030 == "6"] <- "Permanently disabled"
eusilc$pl030_rec[eusilc$pl030 == "7"] <- "Fulfilling domestic tasks"

# Computation, taking sample design into account
eusilc_prop <- prop_group(
  eusilc,
  group = pl030_rec,
  prop_exp = py090n > 0,
  strata = db040,
  ids = db030,
  weight = rb050,
  title = "% of ind. receiving unemployment benefits in their hh",
  subtitle = "Example with austrian SILC data from 'laeken' package"
)

# Results in graph form
eusilc_prop$graph

# Results in table format
eusilc_prop$tab

relab_cut

Description

Function to recode the default labels of a factor created by cut() from base R into more intuitive labels.

Usage

relab_cut(vec, suffix = NULL, right = TRUE, lang = "fr")

Arguments

vec

The vector to be recoded. It should be produced by cut(). Notice that the labels may not include scientific notation. Thus, in cut(), dig.lab argument should be high enough in order to produce labels without scientific notation.

suffix

The suffix to be indicated after the values. Usualy, the unit of the variable will be used (e.g. euros, percents). Default is NULL, for no suffix.

right

TRUE if categories have been created with parameter right = TRUE in cut(). FALSE if categories have been created with parameter right = FALSE in cut(). Default is TRUE.

lang

Language of new labels. Possibilities are "fr" (french), "nl" (dutch) and "en" (english). Default is "fr".

Value

A vector with new labels.

Examples

cut(1:1000, breaks = 5, include.lowest = TRUE, right = FALSE, dig.lab = 4) |>
table()

cut(1:1000, breaks = 5, include.lowest = TRUE, right = FALSE, dig.lab = 4) |>
relab_cut(suffix = "€", right = FALSE) |>
table()

theme_fonctionr

Description

A ggplot theme that is ready to use. It is used by most other functions, but can also be applied to an external ggplot object.

Usage

theme_fonctionr(
  font = "Roboto",
  theme = "fonctionr",
  display = "ggplot",
  grid.lines = "x",
  coef_font = 1
)

Arguments

font

Font used in the graphic. See load_and_active_fonts() for available fonts.

theme

The optionnal theme you want for the graphic. Available themes: "fonctionr" and ⁠“IWEPS”⁠. Default is NULL.

display

The way theme_fonctionr() works on the axis texts: like ggplot2 or ggtext.

grid.lines

Specify major grid lines : "x", "y" or "both". Default is "x".

coef_font

A multiplier factor for font size.

Value

No return value, called for side effects.