| f_boxplot | R Documentation |
Generates boxplots for all numeric variables in a given dataset, grouped by factor variables. The function automatically detects numeric and factor variables. It allows two output formats ('pdf', 'Word') and includes an option to add a general explanation about interpreting boxplots.
f_boxplot(x, ...)
## S3 method for class 'formula'
f_boxplot(formula, data = NULL, ...)
## S3 method for class 'data.frame'
f_boxplot(x, ...)
## S3 method for class 'numeric'
f_boxplot(x, ...)
## S3 method for class 'integer'
f_boxplot(x, ...)
f_boxplot_worker(
formula = NULL,
data,
fancy_names = NULL,
output_type = "pdf",
outliers = TRUE,
coef = 1.5,
limit_columns = 7,
save_as = NULL,
save_in_wdir = FALSE,
close_generated_files = FALSE,
open_generated_files = interactive(),
boxplot_explanation = TRUE,
detect_factors = TRUE,
jitter = FALSE,
width = 8,
height = 7,
units = "in",
res = 300,
las = 2,
color = "rainbow",
boxwidth = NULL,
...
)
x |
A data.frame, formula, or numeric/integer vector (dispatches to the correct method). When a single numeric or integer vector is supplied, it is treated as a single response variable, plotted on the y-axis with the variable name as label, and grouped by a single dummy factor (one box). When several unnamed numeric vectors are supplied (as in base R's |
... |
Further arguments forwarded to |
formula |
A formula specifying the factor to be plotted. More response variables can be added using |
data |
A |
fancy_names |
An optional named vector mapping column names in |
output_type |
Character string, specifying the output format: |
outliers |
Logical. If |
coef |
Numeric. The multiplier for the Interquartile Range (IQR) used for outlier detection. Default |
limit_columns |
Integer or |
save_as |
Character string specifying the output file path (without extension).
If a full path is provided, output is saved to that location.
If only a filename is given, the file is saved in |
save_in_wdir |
Logical. If |
close_generated_files |
Logical. Closes open Excel or Word (NOT pdf) files before writing, depending on the output format. Works on Windows (taskkill), macOS (pkill) and Linux (pkill/soffice). Default |
open_generated_files |
Logical. Whether to open the generated output
files after creation. Defaults to |
boxplot_explanation |
A logical value indicating whether to include an explanation of how to interpret boxplots in the report. Defaults to |
detect_factors |
A logical value indicating whether to automatically detect factor variables in the dataset. Defaults to |
jitter |
A logical value, if |
width |
Numeric, png figure width default |
height |
Numeric, png figure height default |
units |
Character string, png figure units default |
res |
Numeric, png figure resolution default 300 dpi |
las |
An integer ( |
color |
Colour scheme for the boxes. One of: |
boxwidth |
Numeric or |
The function performs the following steps:
Detects numeric and factor variables in the dataset.
Generates boxplots for each numeric variable grouped by each factor variable.
Outputs the report in the specified format ('pdf', 'Word' or 'Rmd').
If output_type = "rmd" is used it is adviced to use it in a chunk with {r, echo=FALSE, results='asis'}
If no factor variables are detected, the function stops with an error message since factors are required for creating boxplots.
This function will plot all numeric and factor candidates, use the function subset() to prepare a selection of columns before submitting to f_boxplot().
Note that there is an optional jitter option to plot all individual data points over the boxplots.
This function requires [Pandoc](https://github.com/jgm/pandoc/releases/tag) (version 1.12.3 or higher), a universal document converter.
Windows: Install Pandoc and ensure the installation folder
(e.g., "C:/Users/your_username/AppData/Local/Pandoc") is added to your system PATH.
macOS: If using Homebrew, Pandoc is typically installed in "/usr/local/bin". Alternatively, download the .pkg installer and verify that the binary's location is in your PATH.
Linux: Install Pandoc through your distribution's package manager (commonly installed in "/usr/bin" or "/usr/local/bin") or manually, and ensure the directory containing Pandoc is in your PATH.
If Pandoc is not found, this function may not work as intended.
The return value depends on output_type:
"pdf" and "word": Writes a report file to save_as (or tempdir() by default) and returns NULL invisibly. The file can optionally be opened with open_generated_files = TRUE.
"png": Writes one PNG file per response x factor combination into the directory given by save_as and returns NULL invisibly.
"rmd": Returns the generated R Markdown content as a single character string (invisibly). No file is written and nothing is printed to the console. The caller can cat() the string, assign it to a variable, or embed it in a larger report (see Examples).
Sander H. van Delden plantmind@proton.me
# Example usage:
data(iris)
new_names = c(
"Sepal.Length" = "Sepal length (cm)" ,
"Sepal.Width" = "Sepal width (cm)",
"Petal.Length" = "Petal length (cm)",
"Petal.Width" = "Petal width (cm)",
"Species" = "Cultivar"
)
# Use the whole data.frame to generate an MS Word report and don't open it.
f_boxplot(iris,
fancy_names = new_names,
output_type = "word"
)
# Use a formula to plot several response parameters (response 1 + response 2 etc)
# and generate a rmd output without boxplot_explanation.
data(mtcars)
f_boxplot(hp + disp ~ gear*cyl,
data=mtcars,
boxplot_explanation = FALSE,
output_type = "word"
)
# Pass a bare numeric vector. Its name is used as the y-axis label
# and as the data_name in the output filename.
set.seed(1)
my_vec <- rnorm(50, mean = 10)
f_boxplot(my_vec, output_type = "png")
# Formula with bare vectors (no data.frame): group hp by cyl.
hp1 <- mtcars$hp
cyl1 <- mtcars$cyl
f_boxplot(hp1 ~ cyl1, output_type = "png")
# Multiple unnamed numeric vectors, base R's boxplot() convention:
# each vector becomes its own box, labelled on the x-axis with its
# original variable name. Use the formula syntax above when you
# instead want to group one response by a factor.
f_boxplot(hp1, cyl1, output_type = "png")
# Capture the R Markdown output as a string and render it inline.
# Use output_type = "rmd" to get the markdown back as a character value
# instead of writing a file. Useful for embedding in a larger knitr document.
rmd <- f_boxplot(iris,
output_type = "rmd",
boxplot_explanation = FALSE,
outliers = FALSE
)
# Display it in the console
cat(rmd)
# ...or splice it into a knitr child chunk with results = "asis":
# ```{r, echo=FALSE, results='asis'}
# cat(rmd)
# ```
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.